簡體   English   中英

CommandButton在JSF中不起作用?

[英]CommandButton not working in JSF?

我有一個使用JSF Framework的Maven / Web應用程序。

這是按鈕的代碼:

<h:form>
     <h:commandButton id="button1" value="Sign in" class="btn btn-info navbar-btn navbar-right"/> 
</h:form>

這是來自導航文件( faces-configx.xml ):

<navigation-case>
            <from-outcome>button1</from-outcome>
            <to-view-id>/pages/signin.xhtml</to-view-id>
</navigation-case>

當我單擊按鈕時,它不起作用,也沒有帶我進入signin.xhtml頁面。 這是為什么? 我的代碼有什么問題?

您正在使用Post-Redirect-Get模式。 在執行此操作時,JSF生命周期(尤其是在第5階段的Invoke Application中)將檢查操作的返回是否為空字符串,因為您尚未在<h:commandButton>定義action屬性的輸出:

<h:commandButton id="button1" value="Sign in" action="" />
                                              ^-------^
                                              not written, JSF assumes this

JSF將在faces-config.xml中搜索此結果並觸發導航規則(如果存在)。

考慮到這一點,您有3種選擇:

  1. 將結果設置為有效的navitagion規則:

     <navigation-rule> <from-view-id>*</from-view-id> <navigation-case> <from-outcome>signin</from-outcome> <to-view-id>logout.xhtml</to-view-id> </navigation-case> </navigation-rule> 

    在Facelets中:

     <h:commandButton value="Sign in" action="signin" /> 
  2. 創建一個適當的托管Bean,該托管Bean的方法返回一個與適當的導航用例匹配的String:

     @ManagedBean @RequestScoped public class FooBean { public String signin() { return "signin"; } } 

    在Facelets中:

     <h:commandButton value="Sign in" action="#{fooBean.signin}" /> 
  3. 對於這種情況,更合適的解決方案是:不要使用Post-Redirect-Get模式,而是通過<h:button>使用直接導航(Redirect-Get)(不需要任何形式)。

     <h:button value="Sign in" outcome="signin" /> 

    上面的代碼假定signin.xhtml文件位於放置此代碼的文件的相同路徑中。 這是:

     - webapp <-- root folder of the web pages of the app - another_folder <-- some folder + file.xhtml <-- file that contains <h:button> + signin.xhtml <-- file to navigate 

我相信您沒有正確使用commandButton。

我認為這個問題可以幫助您:

h:button和h:commandButton之間的區別

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM