将GET查询参数设置为托管属性,
faces-config.xml以便您无需手动收集它们:
<managed-bean> <managed-bean-name>forward</managed-bean-name> <managed-bean-class>com.example.ForwardBean</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <managed-property> <property-name>action</property-name> <value>#{param.action}</value> </managed-property> <managed-property> <property-name>actionParam</property-name> <value>#{param.actionParam}</value> </managed-property></managed-bean>这样,请求
forward.jsf?action=outcome1&actionParam=123将使JSF将
action和
actionParam参数设置为
action和的
actionParam属性
ForwardBean。
创建一个小的视图
forward.xhtml(如此之小,它在默认响应缓冲(通常2KB适合),这样它就可以通过navigationhandler被重置了,否则,你已经增加了servletcontainer的配置响应缓冲区),它调用bean方法上
beforePhase的的
f:view:
<!DOCTYPE html><html xmlns:f="http://java.sun.com/jsf/core"> <f:view beforePhase="#{forward.navigate}" /></html>该
ForwardBean可以是这样的:
public class ForwardBean { private String action; private String actionParam; public void navigate(PhaseEvent event) { FacesContext facesContext = FacesContext.getCurrentInstance(); String outcome = action; // Do your thing? facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, null, outcome); } // Add/generate the usual boilerplate.}在
navigation-rule不言自明的(注意,
<redirect/>这将做分录
ExternalContext#redirect(),而不是
ExternalContext#dispatch()在幕后):
<navigation-rule> <navigation-case> <from-outcome>outcome1</from-outcome> <to-view-id>/outcome1.xhtml</to-view-id> <redirect /> </navigation-case> <navigation-case> <from-outcome>outcome2</from-outcome> <to-view-id>/outcome2.xhtml</to-view-id> <redirect /> </navigation-case></navigation-rule>
另一种方法是
forward.xhtml用作
<!DOCTYPE html><html>#{forward}</html>并更新
navigate()要在其上调用的方法
@PostConstruct(将在bean的构造和所有托管属性设置之后调用):
@PostConstructpublic void navigate() { // ...}它具有相同的效果,但是视图方面并不是真正的自我记录。它基本上所做的就是打印
ForwardBean#toString()(并在不存在时隐式构造bean)。
对于JSF2用户,请注意,有一种使用传递参数的更简洁的方法,
<f:viewParam>以及一种通过处理重定向/导航的更可靠的方法
<f:eventtype="preRenderView">。



