I need to change the context of my site by using parameter sended by client.
For example, if I call http://localhost:8084/JSF/
I load the usual index.xhtml
with the "Homepage" page on the content
template (as default).
But, if I call http://localhost:8084/JSF/index.xhtml?page=profile
, I need a sort of switch in the index.xhtml
, and include/insert the profile template (or a page that define profile) in my content
area.
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.
Which servlet i need to use? Or i need to create my own Servlet to do this?
Cheers
UPDATE (added after BalusC's suggestion)
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;
}
}
template.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title><ui:insert name="title">Facelets Template</ui:insert></title>
</h:head>
<h:body>
<ui:insert name="login_homepage">Box Content Here</ui:insert>
<ui:insert name="content_homepage">Box Content Here</ui:insert>
</h:body>
</html>
index.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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">
<ui:define name="title">
// title
</ui:define>
<ui:define name="login_homepage">
// login
</ui:define>
<ui:include src="#{selector.page}.xhtml" />
<ui:define name="content_homepage">
// content
</ui:define>
</ui:composition>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
profile.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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">
<h2>PROFILE</h2>
</ui:composition>