【问题标题】:JSF 2 form action navigation not workingJSF 2 表单操作导航不起作用
【发布时间】:2012-07-17 12:45:59
【问题描述】:

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

但是,如果我点击“添加”按钮,我的 web 应用程序会尝试导航到页面:“//pages/index.xhtml”,但没有找到任何东西......

有什么想法吗??我几乎遵循了教程,但看不到我哪里弄错了.. 我已将命令按钮映射到 ManagedBean 方法“addUser”。 addUser 方法返回“success”,我已经设置“success”去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>

用户MB:

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

【问题讨论】:

  • 点击提交时会发生什么?
  • 它尝试转到“//pages/index.xhtml”,然后是 404,因为它找不到名为“pages”的服务器...
  • 双“//”是因为欢迎文件中的“/”开头,应该是“pages/index.xhtml”。但即使这样也不会阻止结果的呈现。检查 success.xhtml 文件的文件扩展名或其位置。

标签: java jsf-2 primefaces


【解决方案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>

【讨论】:

  • "from-view-id 'pages/index.xhtml' 引用的资源不是以'/'开头的,这个会为你添加,但应该更正。" -> 看起来我不允许这样做...
  • -1 View ID 字符串以“/”开头,即使您不添加一个 JSF 也会自动添加它。
猜你喜欢
  • 2011-11-26
  • 1970-01-01
  • 2011-03-30
  • 2016-11-04
  • 2011-06-06
  • 1970-01-01
  • 2011-05-17
  • 2014-07-14
  • 1970-01-01
相关资源
最近更新 更多