W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
我們還可以在托管bean中定義一個(gè)方法來(lái)返回視圖名稱。
下面的代碼定義了一個(gè)名為NavigationController的托管bean一個(gè)名為moveToPage1()的方法。moveToPage1()返回頁(yè)面名稱。
@ManagedBean(name = "navigationController", eager = true) @RequestScoped public class NavigationController implements Serializable { public String moveToPage1(){ return "page1"; } }
我們?cè)?code> h:commandButton 中調(diào)用moveToPage1()方法動(dòng)作屬性。
這里當(dāng)點(diǎn)擊Page1按鈕時(shí),JSF會(huì)解析視圖名稱,page1作為page1.xhtml擴(kuò)展,并找到相應(yīng)的視圖文件page1.xhtml在當(dāng)前目錄中。
<h:form> <h3>Using Managed Bean</h3> <h:commandButton action="#{navigationController.moveToPage1}" value="Page1" /> </h:form>
下面的代碼來(lái)自UserBean.java。
package cn.w3cschool.common; import java.io.Serializable; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean(name="user") @SessionScoped public class UserBean implements Serializable{ private static final long serialVersionUID = 1L; public String moveToPage1(){ return "demo"; } public String moveToPage2(){ return "page2"; } }
以下代碼來(lái)自demo.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:form> <h:commandButton action="page2" value="Move to page2.xhtml" /> <h:commandButton action="#{user.moveToPage2}" value="Move to page2.xhtml by managed bean" /> </h:form> </h:body> </html>
以下代碼來(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:form> <h:commandButton action="demo" value="Move to demo.xhtml" /> <h:commandButton action="#{user.moveToPage1}" value="Move to demo.xhtml by managed bean" /> </h:form> </h:body> </html>下載 Implicit-Navigation.zip
將生成的WAR文件從目標(biāo)文件夾復(fù)制到Tomcat部署文件夾,并運(yùn)行Tomcat-Install-folder/bin/startup.bat。
Tomcat完成啟動(dòng)后,在瀏覽器地址欄中鍵入以下URL。
http://localhost:8080/simple-webapp/demo.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)系方式:
更多建議: