mar*_*zzz 3 jsf servlets facelets jsf-2
我需要使用客户端发送的参数来更改我的网站的上下文.
例如,如果我打电话给http://localhost:8084/JSF/我加载模板index.xhtml上的"主页"页面content(默认情况下).但是,如果我打电话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;
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
请求参数可以在JSF bean中设置@ManagedProperty.
@ManagedProperty(value="#{param.page}")
private String page;
Run Code Online (Sandbox Code Playgroud)
(这基本上bean.setPage(request.getParameter("page"))直接在bean的构造之后)
您可以在Facelets中使用EL <ui:include>.
<ui:include src="#{bean.page}.xhtml" />
Run Code Online (Sandbox Code Playgroud)
(如果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;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,我省略了@ManagedBean名称,因为默认值已经是第一个字符小写的类名(与您手动指定的完全相同).我还添加了@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>
Run Code Online (Sandbox Code Playgroud)
(只要没有这样的文件<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>
Run Code Online (Sandbox Code Playgroud)
假设FacesServlet正在监听url-pattern的*.xhtml和这两个文件都在同一个文件夹中,以打开它index.xhtml?page=include.
| 归档时间: |
|
| 查看次数: |
9434 次 |
| 最近记录: |