JSF教程 – JSF条件导航示例
使用托管bean,我们可以基于条件控制导航。
例如,以下代码显示如何为不同的pageId值返回不同的页面名称。
@ManagedBean(name = "navigationController", eager = true) @RequestScoped public class NavigationController implements Serializable { //read value from request parameter pageId @ManagedProperty(value="#{param.pageId}") private String pageId; //if pageId is 1 show page1.xhtml, //if pageId is 2 show page2.xhtml //else show default.xhtml public String showPage(){ if(pageId == null){ return "default"; } if(pageId.equals("1")){ return "page1"; }else if(pageId.equals("2")){ return "page2"; }else{ return "default"; } } }
在JSF UI组件中将pageId作为请求参数。
<h:form> <h:commandLink action="#{navigationController.showPage}" value="Page1"> <f:param name="pageId" value="1" /> </h:commandLink> <h:commandLink action="#{navigationController.showPage}" value="Page2"> <f:param name="pageId" value="2" /> </h:commandLink> <h:commandLink action="#{navigationController.showPage}" value="Home"> <f:param name="pageId" value="3" /> </h:commandLink> </h:form>
这里点击“Page1″按钮
JSF将创建一个参数为pageId = 1的请求。然后,JSF将这个参数传递给navigationController的managed属性pageId。
之后, navigationController.showPage()被调用,将检查pageId后返回view作为page1 。
JSF然后将视图名称 page1 解析为 page1.xhtml 扩展名,并在当前目录中找到相应的视图文件 page1.xhtml 。
例子
以下代码来自register.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:h="http://java.sun.com/jsf/html"> <h:body> <h2>This is resgister.xhtml</h2> </h:body> </html>
以下代码来自ordermore.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:h="http://java.sun.com/jsf/html"> <h:body> <h2>This is ordermore.xhtml</h2> </h:body> </html>
以下代码来自start.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:h="http://java.sun.com/jsf/html"> <h:body> <h2>This is start.xhtml</h2> <h:form> <h:commandButton action="payment" value="Payment" /> </h:form> </h:body> </html>
下面的代码来自UserBean.java。
package cn.w3cschool.common; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import java.io.Serializable; @ManagedBean @SessionScoped public class UserBean implements Serializable { private static final long serialVersionUID = 1L; public boolean registerCompleted = true; public int orderQty = 99; public int getOrderQty() { return orderQty; } public void setOrderQty(int orderQty) { this.orderQty = orderQty; } public boolean isRegisterCompleted() { return registerCompleted; } public void setRegisterCompleted(boolean registerCompleted) { this.registerCompleted = registerCompleted; } }
以下代码来自payment.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:h="http://java.sun.com/jsf/html"> <h:body> <h2>This is payment.xhtml</h2> </h:body> </html>
下载 Conditional-Navigation.zip
运行
将生成的WAR文件从目标文件夹复制到Tomcat部署文件夹,并运行Tomcat-Install-folder/bin/startup.bat。
Tomcat完成启动后,在浏览器地址栏中键入以下URL。
http://localhost:8080/simple-webapp/start.xhtml