单击按钮(JSF)时无法导航到下一页

时间:2013-04-19 19:15:23

标签: jsf commandbutton

我正在学习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>

1 个答案:

答案 0 :(得分:1)

显然,根据您的代码snipplet,您没有定义<h:form>标记。需要在一个内部定义<h:commandButton>之类的组件才能正常运行。在<f:view>中包含<h:form>的内容,然后重试。

相关问题