2012-07-17 56 views
2

我只是用Primefaces測試JSF 2並設置了一個基本的測試表單。 目標是提交表單,然後將用戶發送到列出所有條目的頁面。JSF 2表單操作導航不起作用

但這一切,如果我按一下出現這種情況的「添加」按鈕,我的web應用程序試圖轉到頁:「//pages/index.xhtml」,並沒有什麼發現?

任何想法? ?我幾乎跟着教程,但不能看到我錯在哪裏.. 我已經將commandbutton映射到ManagedBean方法「addUser」。 addUser方法返回「成功」,我已經設置「成功」去success.xhtml ..

我花了3小時現在調試,我即將發瘋,所以任何幫助將大大appriciated ...

的index.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui"> 
    <h:head><title>Welcome to OTV_JSF_Spring_Hibernate_Project</title></h:head> 
    <h:body> 
     <h:form> 
      <table> 
       <tr>...</tr> 
       <tr> 
       <td><p:commandButton id="addUser" value="Add" action="#{userMB.addUser}" ajax="false"/></td> 
       <td><p:commandButton id="reset" value="Reset" action="#{userMB.reset}" ajax="false"/></td> 
       </tr> 
      </table> 
     </h:form> 
    </h:body> 
</html> 

userMB:

@ManagedBean(name = "userMB") 
@RequestScoped 
public class UserManagedBean implements Serializable { 
    private static final String SUCCESS = "success"; 
    private static final String ERROR = "error"; 
    /* .... */ 

    public String addUser() { 
     return SUCCESS; 
    } 

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"> 

<!-- JSF and Spring are integrated --> 
<application> 
    <el-resolver> 
     org.springframework.web.jsf.el.SpringBeanFacesELResolver 
    </el-resolver> 
</application> 

<!-- configuration of navigation rules --> 
<navigation-rule> 
    <from-view-id>/pages/index.xhtml</from-view-id> 
    <navigation-case> 
     <from-outcome>success</from-outcome> 
     <to-view-id>/pages/success.xhtml</to-view-id> 
    </navigation-case> 
    <navigation-case> 
     <from-outcome>error</from-outcome> 
     <to-view-id>/pages/error.xhtml</to-view-id> 
    </navigation-case> 
</navigation-rule> 

</faces-config> 

web.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" 
    version="2.5"> 

<display-name>OTV_JSF_PrimeFaces_Spring_Hibernate</display-name> 

<!-- Spring Context Configuration' s Path definition --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/applicationContext.xml 
    </param-value> 
</context-param> 

<!-- The Bootstrap listener to start up and shut down Spring's root WebApplicationContext. It is registered to Servlet Container --> 
<listener> 
    <listener-class> 
     org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener> 
<listener> 
    <listener-class> 
     org.springframework.web.context.request.RequestContextListener 
    </listener-class> 
</listener> 

<!-- Project Stage Level --> 
<context-param> 
    <param-name>javax.faces.PROJECT_STAGE</param-name> 
    <param-value>Development</param-value> 
</context-param> 

<!-- Welcome Page --> 
<welcome-file-list> 
    <welcome-file>/pages/index.xhtml</welcome-file> 
</welcome-file-list> 

<!-- JSF Servlet is defined to container --> 
<servlet> 
    <servlet-name>Faces Servlet</servlet-name> 
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<!-- Mapping with servlet and url for the http requests. --> 
<servlet-mapping> 
    <servlet-name>Faces Servlet</servlet-name> 
    <url-pattern>*.jsf</url-pattern> 
</servlet-mapping> 
<servlet-mapping> 
    <servlet-name>Faces Servlet</servlet-name> 
    <url-pattern>*.faces</url-pattern> 
</servlet-mapping> 
<servlet-mapping> 
    <servlet-name>Faces Servlet</servlet-name> 
    <url-pattern>*.xhtml</url-pattern> 
</servlet-mapping> 
<servlet-mapping> 
    <servlet-name>Faces Servlet</servlet-name> 
    <url-pattern>/faces/*</url-pattern> 
</servlet-mapping> 
</web-app> 

Generated HTML

+0

會發生什麼,當你點擊提交? – 2012-07-17 12:52:48

+0

它嘗試去「//pages/index.xhtml」,然後404,因爲它找不到名爲「pages」的服務器... – user829237 2012-07-17 12:56:21

+0

雙「//」是因爲歡迎開始的「/」文件,它應該是「pages/index.xhtml」。但即使這樣也不能阻止結果的呈現。檢查您的success.xhtml文件或其位置的文件擴展名。 – Ravi 2012-07-18 04:01:24

回答

1

我認爲你必須這樣寫:

<?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"> 

    <!-- JSF and Spring are integrated --> 
    <application> 
     <el-resolver> 
      org.springframework.web.jsf.el.SpringBeanFacesELResolver 
     </el-resolver> 
    </application> 

    <!-- configuration of navigation rules --> 
    <navigation-rule> 
     <from-view-id>pages/index.xhtml</from-view-id> 
     <navigation-case> 
      <from-outcome>success</from-outcome> 
      <to-view-id>pages/success.xhtml</to-view-id> 
     </navigation-case> 
     <navigation-case> 
      <from-outcome>error</from-outcome> 
      <to-view-id>pages/error.xhtml</to-view-id> 
     </navigation-case> 
    </navigation-rule> 

</faces-config> 
+0

「from-view-id引用的資源'pages/index.xhtml'不以'/'開頭,這將會爲您添加,但應該更正。」 - >看起來像我不允許這樣做...... – user829237 2012-07-17 12:59:10

+0

-1視圖ID字符串以「/」開頭,即使您不添加一個JSF也會自動添加它。 – Ravi 2012-07-18 03:58:12