JSF - 获取url参数并选择页面 - Servlet?

标签 jsf servlets jsf-2 facelets

我需要使用客户端发送的参数来更改我的站点的上下文。

例如,如果我调用 http://localhost:8084/JSF/我加载通常的index.xhtmlcontent 上的“主页”页面模板(默认)。 但是,如果我调用 http://localhost:8084/JSF/index.xhtml?page=profile ,我需要在 index.xhtml 中的一种开关, 并在我的 content 中包含/插入配置文件模板(或定义配置文件的页面)区域。

我认为我需要管理一个 servlet 来执行此操作,因为我认为我不能在我的 index.xhtml 中创建一种 swith。所以我想我需要加载一些模板而不是另一个。

我需要使用哪个 servlet?或者我需要创建自己的 Servlet 来执行此操作?

干杯

更新(根据 BalusC 的建议添加)

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>

最佳答案

请求参数可以通过 @ManagedProperty 在 JSF bean 中设置.

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

(这基本上是在 bean 构建之后直接执行 bean.setPage(request.getParameter("page")))

您可以在 Facelets 中使用 EL <ui:include> .

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

(如果 bean.getPage() 返回 profile ,则该值将最终为 profile.xhtml 并相应地包含在内)

不需要旧的 servlet :)


更新:您将注释设置在错误的位置。它应该看起来像这样,与我原来的答案一模一样:

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 name 因为默认值已经是第一个字符小写的类名(与您手动指定的完全相同)。我还添加了 @RequestScoped用于指定 bean 范围的注释。我还小写了包名,因为根据标准 Java Naming Conventions,包名中不允许使用大写字母。 .

整个<managed-bean>faces-config.xmlentirely superfluous使用新的 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打开它.

关于JSF - 获取url参数并选择页面 - Servlet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4267553/

相关文章:

java - 使用 servlet 加载的图像始终显示红十字

jsf - 标题中带有垂直文本的数据表

jsf - 条件渲染 f :selectItem possible problems

jsf - 重置 JSF 支持 Bean( View 或 session 范围)

jsf - 在 JSF 数据表中显示 Hibernate/JPA 结果会导致 : java. lang.NumberFormatException:对于输入字符串: "[propertyname]"

java - 如何通过ArrayList获取所有同名的html输入值到servlet

JavaScript 没有返回 false

java - JSF2 将字符串列表转换为 String[]

java - 使用 Ajax 加载 Richfaces modalPanel

org.apache.coyote.tomcat5.CoyoteResponseFacade.setBufferSize 处的 java.lang.IllegalStateException