markzzz
markzzz

Reputation: 47945

JSF - Get url parameter and choose the page - Servlet?

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>

Upvotes: 3

Views: 9948

Answers (3)

BalusC
BalusC

Reputation: 1108632

Request parameters are settable in JSF bean by @ManagedProperty.

@ManagedProperty(value="#{param.page}")
private String page;

(this does basically a bean.setPage(request.getParameter("page")) directly after bean's construction)

You can use EL in Facelets <ui:include>.

<ui:include src="#{bean.page}.xhtml" />

(if bean.getPage() returns profile, the value would end up as profile.xhtml and included accordingly)

No need for legacy servlets :)


Update: you've set the annotation at the wrong place. It should look like this, exactly as in my original answer:

package beans;

import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class Selector {

    @ManagedProperty(value="#{param.page}")
    private String page;

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

}

Note that I omitted the @ManagedBean name since the default value is already the classname with 1st character lowercased (which is exactly the same as you specified manually). I also added the @RequestScoped annotation to specify the bean scope. I also lowercased the packagename since uppercases are disallowed in package name as per standard Java Naming Conventions.

The whole <managed-bean> in faces-config.xml is entirely superfluous with the new JSF 2.0 annotations. You're basically duplicating it. Remove it.


Update 2: the index.xhtml should look like this

<!DOCTYPE html>
<html lang="en"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:c="http://java.sun.com/jsp/jstl/core">
    <h:head>
        <title>Include demo</title>
    </h:head>
    <h:body>
        <h1>This is the index page</h1>
        <c:catch>
            <ui:include src="#{selector.page}.xhtml" />
        </c:catch>
    </h:body>
</html>

(the <c:catch> is there to suppress the FileNotFoundException whenever there's no such file)

The include.xhtml should look like this:

<!DOCTYPE html>
<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>Include page content</h2>
</ui:composition>

Assuming that FacesServlet is listening on url-pattern of *.xhtml and the both files are in the same folder, open it by index.xhtml?page=include.

Upvotes: 9

Oversteer
Oversteer

Reputation: 1828

The problem is that if you have a commandButton in the dynamically included file, the commandButton won't work. The action routine is never called. I am still trying to find a solution to this, even with Mojarra 2.1.

Upvotes: 0

hibbelig
hibbelig

Reputation: 538

In the updated question, the following lines are out of order:

@ManagedBean(name="selector")
@ManagedProperty(value="#{param.page}")
public class Selector {
    private String page;

It should be this:

@ManagedBean(name="selector")
public class Selector {
    @ManagedProperty(value="#{param.page}")
    private String page;

Upvotes: 0

Related Questions