0

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

添加用户.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>

托管豆 (用户豆)

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;
    }
}

列表用户

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

请帮助,任何意见将不胜感激。
谢谢

4

1 回答 1

0

这是您的代码中存在的问题部分AddUser.jsp

   <h:inputText value="#{userBean.id}" required="true">
                <f:validateLongRange minimum="1" maximum="500"/>
            </h:inputText>


在这里你放了一个required="true"<h:inputText../>我很确定你没有在 id 字段中给出值,而你只是通过只提交来提交表单name。所以这就是为什么你的表单没有提交
解决方案:
现在你有两个解决方案
1) Don '甚至不更改代码中的一个单词,只在id文本字段中给出值。(不要认为0)您在字段中看到id的默认值是一个将通过必填字段场景的值
2)在此处删除 tharequired="true"部分表单<h:inputText value="#{userBean.id}" required="true">并你会看到它在运行

于 2013-06-18T10:45:52.000 回答