无法从一个jsf页面向前移动到另一个页面

时间:2013-06-18 03:57:45

标签: jsf

我正在尝试构建一个简单的jsf应用程序,其中我将用户ID和名称作为参数并尝试在ManagedBean的帮助下显示在另一个页面上,但到目前为止我还没有成功,并且控制台显示没有错误/ exception,下面是我正在使用的文件列表。

AddUser.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>Add New User Form</title>
</head>
<body>
<f:view>
    <h:form>
        <h:panelGrid border="1" columns="2">
            <h:outputText value="ID"></h:outputText>
            <h:inputText value="#{userBean.id}" required="true">
                <f:validateLongRange minimum="1" maximum="500"/>
            </h:inputText>
            <h:outputText value="Name"></h:outputText>
            <h:inputText value="#{userBean.name}"></h:inputText>
            <h:commandButton action="#{userBean.addUser}"
                value="Add Customer"></h:commandButton>
        </h:panelGrid>
    </h:form>
</f:view>
</body>
</html>

ManagedBean (UserBean)

package com.sapient.bean;

import java.io.Serializable;

public class UserBean implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int id;
    private String name;

    //Action method to add user
    public String addUser() {
        return "success";
    }  
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

LISTUSER

<%@ 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>
    <title>List of Users</title>
</head>
<body>
<f:view>
    <h:form>
        <h:outputText value="User #{userBean.name} is added successfully.">
        </h:outputText>
    </h:form>
</f:view>
</body>
</html>

FacesConfig.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_1_2.xsd"
    version="1.2">
    <managed-bean>
        <managed-bean-name>userBean</managed-bean-name>
        <managed-bean-class>com.sapient.bean.UserBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
    <navigation-rule>
        <display-name>AddUser</display-name>
        <from-view-id>/AddUser.jsp</from-view-id>
        <navigation-case>
            <from-outcome>success</from-outcome>
            <to-view-id>/ListUser.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_2_5.xsd"
    id="WebApp_ID" version="2.5">
  <display-name>Archetype Created Web Application</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>
</web-app>

请帮助,我们将不胜感激任何建议 感谢

1 个答案:

答案 0 :(得分:0)

这是AddUser.jsp

中代码中存在问题的部分
   <h:inputText value="#{userBean.id}" required="true">
                <f:validateLongRange minimum="1" maximum="500"/>
            </h:inputText>


在此,您在required="true"中放置了<h:inputText../>,我确定您没有在ID字段中提供值,而您只是通过仅提供{{ 1}}。那就是为什么你的表格没有提交 解决方案:
现在你有两个解决方案 1)不要改变代码中的单个单词,只需在name文本字段中给出值。(不要认为id)你是谁在0字段中查看作为默认值是将传递所需字段方案的值 2)id移除required="true"部分表单,您会看到它正在运行