OCPSoft.com - Simple SolutionsCommunity Documentation

Chapter 7. Outbound URL-rewriting

7.1. Rewriting URLs in Java
7.2. * Rewriting URLs in JSF views
7.3. Disabling outbound-rewriting for a URL-mapping

Outbound URL-rewriting in provides natural integration with most existing URL components (including all of those from the JavaServer Faces framework.) When PrettyFaces is installed, any URL passed into HttpServletRequest.encodeRedirectURL(String url) will be processed by PrettyFaces outbound URL-rewriting.

Given the following URL-mapping, we can render a pretty URL simply by invoking HttpServletResponse.encodeRedirectURL(String url):

<url-mapping id="viewCategory">
	<pattern value="/store/#{ cat : bean.category }/" />
	<view-id value="/faces/shop/store.jsf" />
</url-mapping>

For example:

HttpServletResponse response = getHttpServletResponse();
String rewrittenURL = response.encodeRedirectURL("/faces/shop/store.jsf?cat=shoes&lang=en_US")';

Or if using JSF:

String rewrittenURL = FacesContext.getCurrentInstance().getExternalContext()
			.encodeResourceURL("/faces/shop/store.jsf?cat=shoes&lang=en_US");

Will produce the following output URL:

/store/shoes/?lang=en_US

Notice that even though we did not define a managed query-parameter, the resulting URL still contains the 'lang' parameter. This is because PrettyFaces only rewrites the named path-parameters defined in the URL-pattern; all other query-parameters are simply left unchanged.

Given the following URL-mapping, some of our JSF URLs will automatically be rewritten:

<url-mapping id="viewCategory">
	<pattern value="/store/#{ cat : bean.category }/" />
	<view-id value="/faces/shop/store.jsf" />
</url-mapping>

For example:

<h:link outcome="/faces/shop/store.jsf" value="View category: Shoes>
	<f:param name="cat" value="shoes" />
	<f:param name="lang" value="en_US" />
</h:link>

And:

<h:link outcome="pretty:viewCategory" value="View category: Shoes>
	<f:param name="cat" value="shoes" />
	<f:param name="lang" value="en_US" />
</h:link>

Will both produce the same output URL:

/store/shoes/?lang=en_US

Notice that even though we did not define a managed query-parameter, the resulting URL still contains the 'lang' parameter. This is because PrettyFaces only rewrites the named path-parameters defined in the URL-pattern; all other query-parameters are simply left unchanged.

Tip

Notice that all <f:param name="" value=""> elements contain both a name and a value attribute. These are required, since (unlike the <pretty:link> component,) <h:link> does not accept un-named parameters, even when passing a "pretty:mappingId" as the outcome.

This is due to the fact that PrettyFaces integration occurs **after** the original URL has already been rendered by the <h:link> component, intercepting the URL at the externalContext.encodeRedirectURL step, explained above (in the section "Rewriting URLs in Java".)

Outbound URL-rewriting may be disabled on an individual mapping basis, by using the attribute outbound="false".

<url-mapping id="viewCategory" outbound="false">
	...
</url-mapping>