W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
使用托管bean,我們可以基于條件控制導(dǎo)航。
例如,以下代碼顯示如何為不同的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作為請求參數(shù)。
<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>
這里點(diǎn)擊“Page1"按鈕
JSF將創(chuàng)建一個參數(shù)為pageId = 1的請求。然后,JSF將這個參數(shù)傳遞給navigationController的managed屬性pageId。
之后, navigationController.showPage()被調(diào)用,將檢查pageId后返回view作為page1 。
JSF然后將視圖名稱 page1 解析為 page1.xhtml 擴(kuò)展名,并在當(dāng)前目錄中找到相應(yīng)的視圖文件 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文件從目標(biāo)文件夾復(fù)制到Tomcat部署文件夾,并運(yùn)行Tomcat-Install-folder/bin/startup.bat。
Tomcat完成啟動后,在瀏覽器地址欄中鍵入以下URL。
http://localhost:8080/simple-webapp/start.xhtml
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: