JSF - Get url parameter and select page - Servlet?

I need to change the context of my site using the parameter assigned by the client.

For example, if I call http://localhost:8084/JSF/, I load the regular one index.xhtmlusing the "Home" page in the template content(default). But, if I call http://localhost:8084/JSF/index.xhtml?page=profile, I need some kind of switch in index.xhtml, and also include / insert the profile template (or the page that defines the profile) in my area content.

It seems to me that I need to manage the servlet in order to do this, because I don't think I can create some kind of swith in my index.xhtml. Therefore, I think I need to load some kind of template instead of another.

Which servlet do I need to use? Or do I need to create my own servlet to do this?

Greetings

UPDATE (added after BalusC proposal)

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>
+3
source share
3 answers

Request parameters are configured in the JSF bean on @ManagedProperty.

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

(this is basically a bean.setPage(request.getParameter("page"))immediately after building the bean)

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

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

(if it bean.getPage()returns profile, the value will turn out as profile.xhtmland accordingly included)

No need for legacy servlets :)


Update : You set the annotation in the wrong place. This should look like 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;
    }

}

, @ManagedBean, 1- ( , ). @RequestScoped, bean. , Java.

<managed-bean> faces-config.xml JSF 2.0. . .


2: index.xhtml :

<!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>

(<c:catch> FileNotFoundException , )

include.xhtml :

<!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>

, FacesServlet url-pattern *.xhtml, , index.xhtml?page=include.

+9

:

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

:

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

The problem is that if you have a commandButton in a dynamically included file, commandButton will not work. An action procedure is never called. I am still trying to find a solution to this, even with Mojarra 2.1.

0
source

Source: https://habr.com/ru/post/1776700/


All Articles