Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF - Get url parameter and choose the page - Servlet?
    text
    copied!<p>I need to change the context of my site by using parameter sended by client. </p> <p>For example, if I call <code>http://localhost:8084/JSF/</code> I load the usual <code>index.xhtml</code> with the "Homepage" page on the <code>content</code> template (as default). But, if I call <code>http://localhost:8084/JSF/index.xhtml?page=profile</code>, I need a sort of switch in the <code>index.xhtml</code>, and include/insert the profile template (or a page that define profile) in my <code>content</code> area. </p> <p>I think i need to manage a servlet to do it, because i don't think i can create a sort of swith in my index.xhtml. So i think i need to load some template instead of another.</p> <p>Which servlet i need to use? Or i need to create my own Servlet to do this?</p> <p>Cheers</p> <p><strong>UPDATE (added after BalusC's suggestion)</strong></p> <pre><code>package Beans; import javax.faces.bean.ManagedProperty; import javax.faces.bean.ManagedBean; @ManagedBean(name="selector") @ManagedProperty(value="#{param.page}") public class Selector { private String page; public String getPage() { return page; } public void setPage(String page) { this.page = page; } } </code></pre> <p><code>template.xhtml</code></p> <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"&gt; &lt;h:head&gt; &lt;title&gt;&lt;ui:insert name="title"&gt;Facelets Template&lt;/ui:insert&gt;&lt;/title&gt; &lt;/h:head&gt; &lt;h:body&gt; &lt;ui:insert name="login_homepage"&gt;Box Content Here&lt;/ui:insert&gt; &lt;ui:insert name="content_homepage"&gt;Box Content Here&lt;/ui:insert&gt; &lt;/h:body&gt; &lt;/html&gt; </code></pre> <p><code>index.xhtml</code></p> <pre><code> &lt;?xml version='1.0' encoding='UTF-8' ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;ui:composition template="./template.xhtml" xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"&gt; &lt;ui:define name="title"&gt; // title &lt;/ui:define&gt; &lt;ui:define name="login_homepage"&gt; // login &lt;/ui:define&gt; &lt;ui:include src="#{selector.page}.xhtml" /&gt; &lt;ui:define name="content_homepage"&gt; // content &lt;/ui:define&gt; &lt;/ui:composition&gt; </code></pre> <p><code>web.xml</code></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt; &lt;context-param&gt; &lt;param-name&gt;javax.faces.PROJECT_STAGE&lt;/param-name&gt; &lt;param-value&gt;Development&lt;/param-value&gt; &lt;/context-param&gt; &lt;servlet&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;url-pattern&gt;/faces/*&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;session-config&gt; &lt;session-timeout&gt; 30 &lt;/session-timeout&gt; &lt;/session-config&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;faces/index.xhtml&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; &lt;/web-app&gt; </code></pre> <p><code>profile.xhtml</code></p> <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;ui:composition xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets"&gt; &lt;h2&gt;PROFILE&lt;/h2&gt; &lt;/ui:composition&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload