【问题标题】:Unable to navigate to next page when clicked on button (JSF)单击按钮时无法导航到下一页(JSF)
【发布时间】:2013-04-19 19:15:22
【问题描述】:

我目前正在学习 jsf。我编写了一个简单的 jsf 应用程序,我的目标是从一个页面导航到另一个页面。但是,我根本无法导航。

在我的代码中有两个jsp文件;一个是createcustomer.jsp,另一个是viewCustomerDetails。用户在createcustomer.jsp 中输入数据并单击按钮后,它应该会移动到下一页(它将在此页面的上一页中显示输入的数据)。

下面是我的代码。谁能找到我哪里出错了?

createCustomer.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f"  uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h"  uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<td>
<h1 style="background-color:blue;color:white;">Create a new Customer</h1>
</td>
</tr>
</table>
<f:view>
<table>
<tr><td>Name:</td><td><h:inputText value="#{customer.name}"></h:inputText></td></tr>
<tr><td>Address:</td><td><h:inputText value="#{customer.address}"></h:inputText></td></tr>
<tr><td>Phone Number:</td><td><h:inputText value="#{customer.phNumber}"></h:inputText></td></tr>
<tr><td>Email Address:</td><td><h:inputText value="#{customer.email_id}"></h:inputText></td></tr>

</table>
<table>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td><h:commandButton value="Create Customer" action="viewCustomerDetails" /></td></tr>
</table>
</f:view>
</body>
</html>


face-config.xml

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

   <managed-bean>
   <managed-bean-name>customer</managed-bean-name>
   <managed-bean-class>com.app.view.Customer</managed-bean-class>
   <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

   <navigation-rule>
   <from-view-id>/createcustomer.jsp</from-view-id>
   <navigation-case>
    <!--  <from-action>#{customer.validate}</from-action>  --> 
   <from-outcome>viewCustomerDetails</from-outcome>
   <to-view-id>/viewCustomerDetails.jsp</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_3_0.xsd" version="3.0">
  <display-name>Customer</display-name>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <description>
    This parameter tells MyFaces if javascript code should be allowed in
    the rendered HTML output.
    If javascript is allowed, command_link anchors will have javascript code
    that submits the corresponding form.
    If javascript is not allowed, the state saving info and nested parameters
    will be added as url parameters.
    Default is 'true'</description>
    <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
    <param-value>true</param-value>
  </context-param>
  <context-param>
    <description>
    If true, rendered HTML code will be formatted, so that it is 'human-readable'
    i.e. additional line separators and whitespace will be written, that do not
    influence the HTML code.
    Default is 'true'</description>
    <param-name>org.apache.myfaces.PRETTY_HTML</param-name>
    <param-value>true</param-value>
  </context-param>
  <context-param>
    <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
    <param-value>false</param-value>
  </context-param>
  <context-param>
    <description>
    If true, a javascript function will be rendered that is able to restore the
    former vertical scroll on every request. Convenient feature if you have pages
    with long lists and you do not want the browser page to always jump to the top
    if you trigger a link or button action that stays on the same page.
    Default is 'false'
</description>
    <param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
    <param-value>true</param-value>
  </context-param>
  <listener>
    <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
  </listener>
</web-app>

【问题讨论】:

  • 您面临什么问题?还值得一提的是您如何定义 web.xml。
  • 我没有遇到任何问题,点击按钮创建客户时页面未提交
  • @developer:stackoverflow.com/questions/2118656/… 的第 1 点回答了您的问题此外,我想指出,自 3 年多前的 JSF 2.0 以来,JSP 已弃用。此外,自 JSF 2.0 隐式导航功能以来,faces-config 中的导航规则已过时。也许您正在阅读糟糕或过时的 JSF 书籍/教程/资源。请确保您基于正确和最新的资源来学习 JSF,否则只会导致头痛和灾难。从这里开始:stackoverflow.com/tags/jsf/info
  • 既然您提到您正在学习使用 JSF,您知道使用 Facelets 吗?认为这值得一看,请注意这只是为了帮助您(与问题没有直接关系)。 Facelets 完全支持 JSF 2.0,而 JSP 不支持。 stackoverflow.com/a/2197189/1336310
  • +1 @Aquillo。我强烈推荐使用 Facelets、JSF 2.2、CDI 和所有最新的 Java EE 附件

标签: jsf commandbutton


【解决方案1】:

显然,根据您的代码片段,您没有定义 &lt;h:form&gt; 标记。 &lt;h:commandButton&gt; 等组件需要在其中定义才能正常运行。将&lt;f:view&gt; 的内容包含在&lt;h:form&gt; 中,然后重试。

【讨论】:

  • 何时添加
    标签,我收到警告为未知标签
  • @developer:转储 JSF 1.x 书籍/教程/资源并寻找 JSF 2.x 的。
  • 我已移至支持 JSF 2.X 的新工作区,但仍收到与上述相同的警告。请建议我解决
猜你喜欢
  • 1970-01-01
  • 2021-06-15
  • 2016-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多