I want to implement a custom ViewHandler. Currently I'm only forwarding all calls to the default Viewhandler, but if I enable this ViewHandler in my faces-config.xml, the preRenderView event type (and maybe other functionality) is broken. Does anybody no what I'm doing wrong?
I'm using JBoss AS 7.1.1.
Thanks.
public class ReverseProxyViewHandler extends ViewHandler {
ViewHandler defaultHandler;
public ReverseProxyViewHandler(ViewHandler defaultHandler) {
this.defaultHandler = defaultHandler;
}
#Override
public Locale calculateLocale(FacesContext context) {
return defaultHandler.calculateLocale(context);
}
#Override
public String calculateRenderKitId(FacesContext context) {
return defaultHandler.calculateRenderKitId(context);
}
#Override
public UIViewRoot createView(FacesContext context, String viewId) {
return defaultHandler.createView(context, viewId);
}
#Override
public String getActionURL(FacesContext context, String path) {
return defaultHandler.getActionURL(context, path);
}
#Override
public String getResourceURL(FacesContext context, String path) {
return defaultHandler.getResourceURL(context, path);
}
#Override
public void renderView(FacesContext context, UIViewRoot viewToRender) throws IOException, FacesException {
defaultHandler.renderView(context, viewToRender);
}
#Override
public UIViewRoot restoreView(FacesContext context, String viewId) {
return defaultHandler.restoreView(context, viewId);
}
#Override
public void writeState(FacesContext context) throws IOException {
defaultHandler.writeState(context);
}
}
I had the same error, but by extending the ViewHandlerWrapper instead of the the ViewHandler I got it to work.
Here is an example of my CustomViewHandler (not a complete example, but was built within our app as a proof of concept) :
import javax.faces.application.ViewExpiredException;
import javax.faces.application.ViewHandler;
import javax.faces.application.ViewHandlerWrapper;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import org.apache.log4j.Logger;
public class CustomViewHandler extends ViewHandlerWrapper {
private ViewHandler wrapped;
private static Logger LOGGER = Logger.getLogger(CustomViewHandler.class);
public CustomViewHandler(ViewHandler wrapped) {
LOGGER.info("CustomViewHandler.CustomViewHandler():wrapped View Handler:"+wrapped.getClass());
this.wrapped = wrapped;
}
#Override
public UIViewRoot restoreView(FacesContext context, String viewId) {
UIViewRoot root;
try {
LOGGER.info("restoring view : " + viewId);
root = wrapped.restoreView(context, viewId);
} catch (ViewExpiredException e) {
LOGGER.error("View Expired : " + e.getMessage() + " -> recreating");
root = wrapped.createView(context, viewId);
}
return root;
}
#Override
public ViewHandler getWrapped() {
return wrapped;
}
}
If you want a more complete and functional custom view handler, look at the OmniFaces RestorableViewHandler. Source code is available on that page.
From Java Doc
javax.faces.application.ViewHandlerWrapper
Provides a simple implementation of ViewHandler that can be subclassed by developers >wishing to provide specialized behavior to an existing ViewHandler instance. The default >implementation of all methods is to call through to the wrapped ViewHandler.
Usage: extend this class and override getWrapped to return the instance we are wrapping.
Related
I am working on an application where I would like to include dynamic XHTML content from a stream. To handle this I wrote a taghandler extension which dumps the dynamic XHTML content to output component as
UIOutput htmlChild = (UIOutput) ctx.getFacesContext().getApplication().createComponent(UIOutput.COMPONENT_TYPE);
htmlChild.setValue(new String(outputStream.toByteArray(), "utf-8"));
This works fine for XHTML content which has no JSF tags. If I have JSF tags in my dynamic XHTML content like <h:inputText value="#{bean.item}"/>, then they're printed as plain text. I want them to render as input fields. How can I achieve this?
Essentially, you should be using an <ui:include> in combination with a custom ResourceHandler which is able to return the resource in flavor of an URL. So when having an OutputStream, you should really be writing it to a (temp) file so that you can get an URL out of it.
E.g.
<ui:include src="/dynamic.xhtml" />
with
public class DynamicResourceHandler extends ResourceHandlerWrapper {
private ResourceHandler wrapped;
public DynamicResourceHandler(ResourceHandler wrapped) {
this.wrapped = wrapped;
}
#Override
public ViewResource createViewResource(FacesContext context, String resourceName) {
if (resourceName.equals("/dynamic.xhtml")) {
try {
File file = File.createTempFile("dynamic-", ".xhtml");
try (Writer writer = new FileWriter(file)) {
writer
.append("<ui:composition")
.append(" xmlns:ui='http://java.sun.com/jsf/facelets'")
.append(" xmlns:h='http://java.sun.com/jsf/html'")
.append(">")
.append("<p>Hello from a dynamic include!</p>")
.append("<p>The below should render as a real input field:</p>")
.append("<p><h:inputText /></p>")
.append("</ui:composition>");
}
final URL url = file.toURI().toURL();
return new ViewResource(){
#Override
public URL getURL() {
return url;
}
};
}
catch (IOException e) {
throw new FacesException(e);
}
}
return super.createViewResource(context, resourceName);
}
#Override
public ResourceHandler getWrapped() {
return wrapped;
}
}
(warning: basic kickoff example! this creates a new temp file on every request, a reuse/cache system should be invented on your own)
which is registered in faces-config.xml as follows
<application>
<resource-handler>com.example.DynamicResourceHandler</resource-handler>
</application>
Note: all of above is JSF 2.2 targeted. For JSF 2.0/2.1 users stumbling upon this answer, you should use ResourceResolver instead for which an example is available in this answer: Obtaining Facelets templates/files from an external filesystem or database. Important note: ResourceResolver is deprecated in JSF 2.2 in favor of ResourceHandler#createViewResource().
My solution for JSF 2.2 and custom URLStream Handler
public class DatabaseResourceHandlerWrapper extends ResourceHandlerWrapper {
private ResourceHandler wrapped;
#Inject
UserSessionBean userBeean;
public DatabaseResourceHandlerWrapper(ResourceHandler wrapped) {
this.wrapped = wrapped;
}
#Override
public Resource createResource(String resourceName, String libraryName) {
return super.createResource(resourceName, libraryName); //To change body of generated methods, choose Tools | Templates.
}
#Override
public ViewResource createViewResource(FacesContext context, String resourceName) {
if (resourceName.startsWith("/dynamic.xhtml?")) {
try {
String query = resourceName.substring("/dynamic.xhtml?".length());
Map<String, String> params = splitQuery(query);
//do some query to get content
String content = "<ui:composition"
+ " xmlns='http://www.w3.org/1999/xhtml' xmlns:ui='http://java.sun.com/jsf/facelets'"
+ " xmlns:h='http://java.sun.com/jsf/html'> MY CONTENT"
+ "</ui:composition>";
final URL url = new URL(null, "string://helloworld", new MyCustomHandler(content));
return new ViewResource() {
#Override
public URL getURL() {
return url;
}
};
} catch (IOException e) {
throw new FacesException(e);
}
}
return super.createViewResource(context, resourceName);
}
public static Map<String, String> splitQuery(String query) throws UnsupportedEncodingException {
Map<String, String> params = new LinkedHashMap<>();
String[] pairs = query.split("&");
for (String pair : pairs) {
int idx = pair.indexOf("=");
params.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
}
return params;
}
#Override
public ResourceHandler getWrapped() {
return wrapped;
}
static class MyCustomHandler extends URLStreamHandler {
private String content;
public MyCustomHandler(String content) {
this.content = content;
}
#Override
protected URLConnection openConnection(URL u) throws IOException {
return new UserURLConnection(u, content);
}
private static class UserURLConnection extends URLConnection {
private String content;
public UserURLConnection(URL url, String content) {
super(url);
this.content = content;
}
#Override
public void connect() throws IOException {
}
#Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(content.getBytes("UTF-8"));
}
}
}
}
I'm migrating a JSF 1.2 project to JSF 2 and PrimeFaces 6 with Ultima layout.
When using Ultima layout, I get the below exception:
SimpleJSFNavigationHandler cannot be cast to javax.faces.application.ConfigurableNavigationHandler.
How to fix it?
Below is the SimpleJSFNavigationHandler.
import java.io.IOException;
import javax.faces.FacesException;
import javax.faces.application.NavigationHandler;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.springframework.web.jsf.DecoratingNavigationHandler;
public class SimpleJSFNavigationHandler extends DecoratingNavigationHandler {
public static final String DEFAULT_REDIRECT_PREFIX = "redirect:";
public static final String DEFAULT_FORWARD_PREFIX = "/";
private String redirectPrefix = DEFAULT_REDIRECT_PREFIX;
private String forwardPrefix = DEFAULT_FORWARD_PREFIX;
public SimpleJSFNavigationHandler() {
}
public SimpleJSFNavigationHandler(NavigationHandler originalNavigationHandler) {
super(originalNavigationHandler);
}
#Override
public void handleNavigation(FacesContext facesContext, String fromAction, String outcome, NavigationHandler originalNavigationHandler) {
if (outcome != null && outcome.startsWith(redirectPrefix)) {
ExternalContext externalContext = facesContext.getExternalContext();
ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
String url = outcome.substring(redirectPrefix.length());
String urlParams="";
if(url.indexOf("?")>0)
urlParams = url.substring(url.indexOf("?"));
String redirectPath = viewHandler.getActionURL(facesContext, url);
try {
//System.out.println("MMMMMMMMMMMMMMMM:::::::::::::::::" + urlParams);
externalContext.redirect(externalContext.encodeActionURL(redirectPath+urlParams));
} catch (IOException e) {
throw new FacesException(e.getMessage(), e);
}
facesContext.responseComplete();
} else if (outcome != null && outcome.startsWith(forwardPrefix)) {
ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
//create new view
String newViewId = outcome.substring(forwardPrefix.length());
if (newViewId.length()>0 && newViewId.charAt(0)!='/') {
newViewId = "/" + newViewId;
}
UIViewRoot viewRoot = viewHandler.createView(facesContext, newViewId);
viewRoot.setViewId(newViewId);
facesContext.setViewRoot(viewRoot);
facesContext.renderResponse();
} else {
callNextHandlerInChain(facesContext, fromAction, outcome, originalNavigationHandler);
}
}
}
You don't need Spring's DecoratingNavigationHandler. You can just use JSF's own ConfigurableNavigationHandlerWrapper.
public class SimpleJSFNavigationHandler extends ConfigurableNavigationHandlerWrapper {
private ConfigurableNavigationHandler wrapped;
public SimpleJSFNavigationHandler(ConfigurableNavigationHandler wrapped) {
this.wrapped = wrapped;
}
#Override
public void handleNavigation(FacesContext facesContext, String fromAction, String outcome) {
if (...) {
// Your original code here.
} else if (...) {
// Your original code here.
} else {
// Update only the last else part as below.
getWrapped().handleNavigation(facesContext, fromAction, outcome);
}
}
#Override
public ConfigurableNavigationHandler getWrapped() {
return wrapped;
}
}
In the upcoming JSF 2.3 this can even be further simplified as per spec issue 1429 which should further reduce boilerplate code in FacesWrapper implementations.
public class SimpleJSFNavigationHandler extends ConfigurableNavigationHandlerWrapper {
public SimpleJSFNavigationHandler(ConfigurableNavigationHandler wrapped) {
super(wrapped);
}
#Override
public void handleNavigation(FacesContext facesContext, String fromAction, String outcome) {
if (...) {
// Your original code here.
} else if (...) {
// Your original code here.
} else {
// Update only the last else part as below.
getWrapped().handleNavigation(facesContext, fromAction, outcome);
}
}
}
hazelcast configuration for the map is
<map name="test">
<max-idle-seconds>120</max-idle-seconds>
<entry-listeners>
<entry-listener include-value="true" local="false">com.test.listener.SessionListener</entry-listener>
</entry-listeners>
</map>
I have a listener configured for the evict action.
Listener is not able to catch the evict action consistently .
Hazelcast Version : 3.6.5
Listener Class Implemetation:
public class SessionListener implements EntryListener<String, Object> {
#Override
public void entryEvicted(EntryEvent<String, Object> evictData) {
try {
Session sessionObjValue = (Session) evictData.getOldValue();
String sessionId = sessionObjValue.getSessionId();
String userName = sessionObjValue.getUsername();
JSONObject inputJSON = new JSONObject();
inputJSON.put(Constants.SESSIONID, sessionId);
inputJSON.put(Constants.USER_NAME, userName);
//Operations to be performed based on the JSON Value
} catch (Exception exception) {
LOGGER.logDebug(Constants.ERROR, methodName, exception.toString());
}
}
Below are the recommendations:
Include Eviction policy configurations in your map config. Right now eviction is happening only based on max-idle-seconds.
Implement all the methods from EntryListener interface which inturn extends other interfaces.
Implement EntryExpiredListener listener also, to catch the expiry events explicitly though evict event also will be called during expiry.
Sample code:
public class MapEntryListernerTest implements EntryListener, EntryExpiredListener {
#Override
public void entryAdded(EntryEvent event) {
}
#Override
public void entryEvicted(EntryEvent event) {
}
#Override
public void entryRemoved(EntryEvent event) {
}
#Override
public void entryUpdated(EntryEvent event) {
}
#Override
public void mapCleared(MapEvent event) {
}
#Override
public void mapEvicted(MapEvent event) {
}
#Override
public void entryExpired(EntryEvent event) {
}
}
I wrote myself a custom NavigationHandler very similar to following one but just using a stack to save the history:
http://jsfatwork.irian.at/book_de/custom_component.html#!idx:/custom_component.html:fig:backnavigationhandler-code
public class HistoryNavigationHandler extends NavigationHandler
{
private final NavigationHandler navigationHandler;
private final Stack<String> outcomes;
public HistoryNavigationHandler(final NavigationHandler navigationHandler)
{
this.navigationHandler = navigationHandler;
this.outcomes = new Stack<String>();
}
#Override
public void handleNavigation(final FacesContext context, final String fromAction, final String outcome)
{
if (outcome != null)
{
if (outcome.equals("back"))
{
final String lastViewId = this.outcomes.pop();
final ViewHandler viewHandler = context.getApplication().getViewHandler();
final UIViewRoot viewRoot = viewHandler.createView(context, lastViewId);
context.setViewRoot(viewRoot);
context.renderResponse();
return;
}
else
{
this.outcomes.push(context.getViewRoot().getViewId());
}
}
this.navigationHandler.handleNavigation(context, fromAction, outcome);
}
}
Registering this one in the faces-config.xml:
<navigation-handler>
package.HistoryNavigationHandler
</navigation-handler>
Results in following log warning and a message where a previously working link was present:
Warning: jsf.outcome.target.invalid.navigationhandler.type
Something like: this link is disabled because a navigation case could not be matched
What is the problem?
Since JSF 2, the NavigationHandler has been replaced by ConfigurableNavigationHandler. All JSF 2 specific tags/components like <h:link> and so on are relying on it. The NavigationHandler is kept for backwards compatibility.
Here's a kickoff example how to properly extend ConfigurableNavigationHandler:
public class HistoryNavigationHandler extends ConfigurableNavigationHandler {
private NavigationHandler wrapped;
public HistoryNavigationHandler(NavigationHandler wrapped) {
this.wrapped = wrapped;
}
#Override
public void handleNavigation(FacesContext context, String from, String outcome) {
// TODO: Do your job here.
wrapped.handleNavigation(context, from, outcome);
}
#Override
public NavigationCase getNavigationCase(FacesContext context, String fromAction, String outcome) {
return (wrapped instanceof ConfigurableNavigationHandler)
? ((ConfigurableNavigationHandler) wrapped).getNavigationCase(context, fromAction, outcome)
: null;
}
#Override
public Map<String, Set<NavigationCase>> getNavigationCases() {
return (wrapped instanceof ConfigurableNavigationHandler)
? ((ConfigurableNavigationHandler) wrapped).getNavigationCases()
: null;
}
}
I have tryed to build a Java Class in JSf witch adds a view with a Pager to an XPage
Im Using a UiDataview in this simple example but my problem is that the Pager witch is added to the result is never displayed in my Xpage. anyone an idea what i have to do?
public class MainLibcontrol extends UIComponentBase implements FacesComponent {
private static final String RENDERER_TYPE = "de.my.MainLibcontrol";
private static final String COMPONENT_FAMILY = "de.my";
public MainLibcontrol() {
setRendererType(RENDERER_TYPE);
}
#Override
public String getFamily() {
return COMPONENT_FAMILY;
}
#SuppressWarnings("unchecked")
public void initBeforeContents(FacesContext arg0) throws FacesException {
try {
UIDataView viewtable = new UIDataView();
viewtable.setColumnTitles(true);
CategoryColumn categoryColumn = new CategoryColumn();
categoryColumn.setComponent(viewtable);
categoryColumn.setColumnName("form");
categoryColumn.setColumnTitle("form");
categoryColumn.setContentType("text");
viewtable.addCategoryColumn(categoryColumn);
DominoViewData data = new DominoViewData();
data.setComponent(viewtable);
data.setViewName("142342");
data.setVar("view2");
viewtable.setData(data);
viewtable.setId("dataView1");
viewtable.setRows(3);
SummaryColumn summaryColumn = new SummaryColumn();
summaryColumn.setComponent(viewtable);
summaryColumn.setColumnName("5");
summaryColumn.setColumnTitle("5");
viewtable.setSummaryColumn(summaryColumn);
XspPager pager = new XspPager();
pager.setPartialRefresh(true);
pager.setLayout("Previous Group Next");
pager.setId("pager1");
viewtable.getChildren().add(pager);
this.getChildren().add(viewtable);
} catch (Exception e) {
e.printStackTrace();
}
}
public void buildContents(FacesContext arg0, FacesComponentBuilder arg1) throws FacesException {
.....
}
public void initAfterContents(FacesContext arg0) throws FacesException {
....
}
}
I haven't tried this out, but I would imagine you want to add it as a facet of the viewTable not as a child.
so your line should be
viewtable.getFacets().put("headerPager", pager);