繁体   English   中英

JSF 2表单操作导航不起作用

[英]JSF 2 form action navigation not working

我刚刚使用Primefaces测试了JSF 2,并且正在建立一个基本的测试表单。 目的是提交表单,然后将用户发送到列出所有条目的页面。

但是,如果我单击“添加”按钮,则会发生的所有事情是我的Web应用程序尝试导航到页面:“ // pages / index.xhtml”,但找不到任何内容...

有任何想法吗?? 我已经按照教程学习了很多,但是看不到我错了。.我已经将命令按钮映射到ManagedBean方法“ addUser”。 addUser方法返回“成功”,并且我已经设置“成功”以访问success.xhtml。

我已经花了3个小时来调试它,而且我快要疯了,所以任何帮助都将得到极大的应用...

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

生成的HTML

我认为您必须这样写:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM