<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SEO | LogikDevelopment</title>
	<atom:link href="https://www.logikdev.com/tag/seo/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.logikdev.com</link>
	<description>&#34;Il n&#039;y a pas de problème, il n&#039;y a que des solutions. L&#039;esprit de l&#039;homme invente ensuite le problème.&#34; André Gide</description>
	<lastBuildDate>Tue, 25 Apr 2017 13:57:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.4</generator>
	<item>
		<title>How to personalise the URLs with Faces Navigation?</title>
		<link>https://www.logikdev.com/2010/03/24/how-to-personalise-the-urls-with-faces-navigation/</link>
					<comments>https://www.logikdev.com/2010/03/24/how-to-personalise-the-urls-with-faces-navigation/#comments</comments>
		
		<dc:creator><![CDATA[smoreau]]></dc:creator>
		<pubDate>Wed, 24 Mar 2010 20:01:29 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[EL expressions]]></category>
		<category><![CDATA[Faces Navigation]]></category>
		<category><![CDATA[JSF]]></category>
		<category><![CDATA[SEO]]></category>
		<category><![CDATA[view handler]]></category>
		<guid isPermaLink="false">https://www.logikdev.com/?p=418</guid>

					<description><![CDATA[<p>This is an important question if you want to have search engine friendly URLs on your website! But unfortunately, the solution is not straightforward with JavaServer Faces (JSF). 🙁 First of all, for security reason JSF doesn&#8217;t allow you to use the GET method for your forms. I didn&#8217;t really understand why but this is [&#8230;]</p>
<p>The post <a href="https://www.logikdev.com/2010/03/24/how-to-personalise-the-urls-with-faces-navigation/">How to personalise the URLs with Faces Navigation?</a> first appeared on <a href="https://www.logikdev.com">LogikDevelopment</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>This is an important question if you want to have search engine friendly URLs on your website!<br />
But unfortunately, the solution is not straightforward with JavaServer Faces (JSF). <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f641.png" alt="🙁" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>First of all, for security reason JSF doesn&#8217;t allow you to use the GET method for your forms. I didn&#8217;t really understand why but this is very (too) restrictive!<br />
Can you imagine Google doing the same thing? We would have the same URL for every search terms <code>http://www.google.co.uk/search</code> instead of something like <code>http://www.google.co.uk/search?hl=en&amp;safe=off&amp;esrch=FT1&amp;q=test&amp;meta=&amp;aq=f&amp;aqi=g10&amp;aql=&amp;oq=&amp;gs_rfai=</code>.<br />
It wouldn&#8217;t be very easy to share a search page with a friend if Google was using the POST method. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>So the question is how to get around this JSF limitation?</p>
<p>Let&#8217;s take a look at how would look our <code>faces-navigation.xml</code> file for a search page:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;navigation-case&gt;
    &lt;from-action&gt;#{searchBean.searchAction}&lt;/from-action&gt;
    &lt;from-outcome&gt;success&lt;/from-outcome&gt;
    &lt;to-view-id&gt;/search.xhtml&lt;/to-view-id&gt;
    &lt;redirect /&gt;
&lt;/navigation-case&gt;
</pre>
<p>In this example, all the JSF elements calling the action <code>searchBean.searchAction</code> will be redirected to the <code>search.xhtml</code> page.<br />
But, how are we going to get the search parameters into the URL?</p>
<p>Ideally, it would be great to be able to do something like the following:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;navigation-case&gt;
    &lt;from-action&gt;#{searchBean.searchAction}&lt;/from-action&gt;
    &lt;from-outcome&gt;success&lt;/from-outcome&gt;
    &lt;to-view-id&gt;/search.xhtml?q=#{param.q}&lt;/to-view-id&gt;
    &lt;redirect /&gt;
&lt;/navigation-case&gt;
</pre>
<p>This solution would allow us to inject EL expressions into the URL before the page is redirected to the destination page.<br />
In order to do this, we need to create our own view handler and register it to our application.  <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f60e.png" alt="😎" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>The code below is the view handler class which also includes some comments:</p>
<pre class="brush: java; title: ; notranslate">
package com.logikdev.gui.handler;

import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.ViewHandler;
import javax.faces.context.FacesContext;

import com.sun.facelets.FaceletViewHandler;

/**
 * Overrides the Facelet view handler to support EL expressions in URLs.
 * @author Stéphane Moreau
 */
public class DynamicViewHandler extends FaceletViewHandler {

	public DynamicViewHandler(ViewHandler parent) {
		super(parent);
	}
	
	/* (non-Javadoc)
	 * @see com.sun.facelets.FaceletViewHandler#getActionURL(javax.faces.context.FacesContext, java.lang.String)
	 */
	@Override
	public String getActionURL(FacesContext context, String viewId) {
		String queryString = null;

		// Replace the EL expressions in the URL
		ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory();
		ValueExpression valueExpression = expressionFactory.createValueExpression(context.getELContext(), viewId, String.class);
		String result = (String) valueExpression.getValue(context.getELContext());
		
		// Separate the query string from the URL
		int dotIndex = result.lastIndexOf('.');
		int questionMarkIndex = result.indexOf('?');
		if (questionMarkIndex != -1) {
			queryString = result.substring(questionMarkIndex, dotIndex);
			viewId = result.substring(0, questionMarkIndex) + result.substring(dotIndex);
		}
		
		// Call the parent without the query string
		result = super.getActionURL(context, viewId);
		
		// Put back the query string at the end of the URL
		if (queryString != null) {
			result += queryString;
		}
	        
		return result;
	}

}
</pre>
<p>And the following is the code to put in the <code>faces-config.xml</code> file in order to register the newly created view handler to the application:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;application&gt;
	&lt;variable-resolver&gt;org.springframework.web.jsf.DelegatingVariableResolver&lt;/variable-resolver&gt;
	&lt;view-handler&gt;com.logikdev.gui.handler.DynamicViewHandler&lt;/view-handler&gt;
&lt;/application&gt;
</pre>
<p><strong>One last thing!</strong><br />
This view handler also has a limitation which I wasn&#8217;t able to fix. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f641.png" alt="🙁" class="wp-smiley" style="height: 1em; max-height: 1em;" /> The file extension has to ALWAYS be placed at the end of the <code>to-view-id</code> URL! The view handler will then put it back before the question mark.<br />
For example:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;navigation-case&gt;
	&lt;from-action&gt;#{searchBean.searchAction}&lt;/from-action&gt;
	&lt;from-outcome&gt;success&lt;/from-outcome&gt;
	&lt;!-- The extension has to be at the end --&gt;
	&lt;to-view-id&gt;/search?q=#{param.q}.xhtml&lt;/to-view-id&gt;
	&lt;redirect /&gt;
&lt;/navigation-case&gt;
</pre>
<p>If you perform a search on &#8216;jsf&#8217; with the above navigation rule, the user will be redirected to the page <code>/search.xhtml?q=jsf</code>.</p><p>The post <a href="https://www.logikdev.com/2010/03/24/how-to-personalise-the-urls-with-faces-navigation/">How to personalise the URLs with Faces Navigation?</a> first appeared on <a href="https://www.logikdev.com">LogikDevelopment</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.logikdev.com/2010/03/24/how-to-personalise-the-urls-with-faces-navigation/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
