2012-10-08 68 views
0

當我點擊搜索按鈕,我試圖讓我的index.xhtml帶我到另一個頁面w /結果。我正在使用primefaces JSF。JSF導航案例不導航

我的問題是,我不能讓它去下一頁。它調用我的searchController beans findItem方法,但結果頁面不會改變。它只停留在index.xhtml頁面上。

任何人有想法嗎?

@ManagedBean(name="sController") 
@RequestScoped 
public class SController implements Serializable { 
    private static final long serialVersionUID = 1L; 

    public SController() { } 

    public String findItem() { 
     System.out.println("findItem called!"); 

     return "success"; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getName() { 
     return name; 
    } 
} 

這是我的faces-config.xml。

<?xml version="1.0" encoding="UTF-8"?> 
<faces-config 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" 
    version="2.0"> 

<navigation-rule> 
    <from-view-id>/index.xhtml</from-view-id> 
    <navigation-case> 
     <from-action>#{sController.findItem}</from-action> 
     <from-outcome>success</from-outcome> 
     <to-view-id>/items.xhtml</to-view-id> 
    </navigation-case> 
</navigation-rule> 
</faces-config> 

而這裏是我的index.xhtml。

<f:view> 
<h:form> 
    <p:panel id="search" header="Search Item"> 
     <p:panelGrid columns="2" cellpadding="5"> 

      <h:outputLabel value="Name" for="name"/> 
      <p:inputText id="name" value="#{sController.name}"/> 

     </p:panelGrid> 
    </p:panel> 

    <p:commandButton value="Search" 
     actionListener="#{sController.findItem}"/> 
</h:form> 
</f:view> 
+0

我只是發現了隱式導航和現在的工作。所以我刪除了我的faces-config.xml文件。 – Ender

回答

3

這是一種解決方法。對於別人誰可能會訪問這個頁面同樣的問題的利益,你的頁面無法導航,因爲你使用的actionListener屬性不應該有使用的方法表達(僅ActionListener類型的方法是允許存在)。使用該方法代替命令按鈕的action屬性。

正確實現接口ActionListener的方法返回類型爲void,因此不會隱式執行任何基於JSF的導航。然而,返回類型String的常規方法表達式是用於基於JSF的導航的。

2
  1. 刪除您面臨-config文件(正如你所指出)
  2. 用行動代替的ActionListener:

    <p:commandButton value="Search" action="#{sController.findItem}"/>

  3. 追加faces-redirect=true您返回的字符串來重定向到成功頁數:

    public String findItem() { 
        System.out.println("findItem called!"); 
    
        return "success?faces-redirect=true"; 
    }