W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
JSF提供了導(dǎo)航解析選項(xiàng),即使托管bean不同的方法返回相同的視圖名稱。
我們可以在faces-config.xml文件中定義視圖頁(yè)面名稱。
例如,來(lái)自UserBean托管bean的以下兩個(gè)方法返回相同的結(jié)果。
@ManagedBean @SessionScoped public class UserBean implements Serializable { private static final long serialVersionUID = 1L; public String processPage1(){ return "success"; } public String processPage2(){ return "success"; }
要解析視圖,請(qǐng)?jiān)趂aces-config.xml中定義以下導(dǎo)航規(guī)則
<navigation-rule> <from-view-id>home.xhtml</from-view-id> <navigation-case> <from-action>#{userBean.processPage1}</from-action> <from-outcome>success</from-outcome> <to-view-id>page1.jsf</to-view-id> </navigation-case> <navigation-case> <from-action>#{userBean.processPage2}</from-action> <from-outcome>success</from-outcome> <to-view-id>page2.jsf</to-view-id> </navigation-case> </navigation-rule>
以下JSF頁(yè)面從命令按鈕調(diào)用用戶bean方法。
<h:form> <h:commandButton action="#{userBean.processPage1}" value="Page1" /> <h:commandButton action="#{userBean.processPage2}" value="Page2" /> </h:form>
這里,當(dāng)點(diǎn)擊Page1按鈕。
調(diào)用 userBean.processPage1(),將返回視圖成功。
JSF將視圖名稱解析為當(dāng)前目錄中相應(yīng)的視圖文件page1.xhtml 。
以下代碼來(lái)自page1.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 page1.xhtml</h2> </h:body> </html>
以下代碼來(lái)自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="#{userBean.processPage1}" value="Page1" /> <h:commandButton action="#{userBean.processPage2}" value="Page2" /> </h:form> </h:body> </html>
下面的代碼來(lái)自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 String processPage1(){ return "success"; } public String processPage2(){ return "success"; } }
以下代碼來(lái)自page2.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 page2.xhtml</h2> </h:body> </html>下載 Form-Action-Navigation.zip
將生成的WAR文件從目標(biāo)文件夾復(fù)制到Tomcat部署文件夾,并運(yùn)行Tomcat-Install-folder/bin/startup.bat。
Tomcat完成啟動(dòng)后,在瀏覽器地址欄中鍵入以下URL。
http://localhost:8080/simple-webapp/start.xhtml
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: