Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Filter forwarding and JSF

843844May 26 2009
Hi,

In my application I use a Filter to forward URLs to a shared resource (i.e. a Servlet, JSP page, etc). The filter inserts parameters into the request scope, which the shared resource picks up to configure itself with.

This following shows the fragment that does the forwarding:
Map<String, Object> parameters = URL.getParameters();
for (Map.Entry<String, Object>  item : parameters.entrySet()) {
	request.setAttribute(item.getKey(), item.getValue());
}

String realUrl = "/" + URL.getForwardUrl();			
request.getRequestDispatcher(realUrl).forward(request, response);
Using a redirect instead of an internal forward would be an alternative option, but in that case I can't put anything in the request scope. I could use request parameters, but these would show up in the browser's address bar, which is not desirable.

The problem now is that this didn't work with JSF. JSF forms would be rendered with an action attribute pointing to the forwarded resource (reverse transformed to correspond with my configured JSF mapping). This forwarded resource however should not be exposed to the user, who should only see the URL by which the page was originally requested.

I thus decorated the ViewHandler to return an action url that basically corresponds to request.getRequestURI():
public String getActionURL(FacesContext context, String viewId) {
		
	String menuOriginalUrl = (String)context.getExternalContext().getRequestMap().get("menu.originalUrl");
	if (menuOriginalUrl != null) {
		return menuOriginalUrl;
	}		
	return super.getActionURL(context, viewId);
}
"menu.originalUrl" is inserted in the request scope by the Filter shown earlier:
Map<String, Object> parameters = URL.getParameters();
for (Map.Entry<String, Object>  item : parameters.entrySet()) {
	request.setAttribute(item.getKey(), item.getValue());
}
String realUrl = "/" + URL.getForwardUrl();
			
request.setAttribute("menu.originalUrl", request.getRequestURI()); // added 
		
request.getRequestDispatcher(realUrl).forward(request, response);
I tested this and it seems to work, but I wonder whether I haven't overlooked anything. The default ViewHandler implementation does a fairly complicated reverse transformation from the viewId back to a URL, but here I'm just grabbing the request URI instead. I would like my forwarding mechanism to last and not cause any problems later for the other developers in my team because I have overlooked something.

Is my solution a correct one, or are there more things I have to decorate or override to get this working correctly in every situation in JSF?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 23 2009
Added on May 26 2009
0 comments
298 views