无法将 jsp 表单值设置为 class 变量

Unable to set jsp form values to class variables

当我尝试从 class 中获取在 jsp 中设置的值时,显示为 null。

在开发模式下观察到以下错误:

ERROR ParametersInterceptor Developer Notification (set struts.devMode to false to disable this message): Unexpected Exception caught setting 'name' on 'class org.ravi.EmployeeAction: Error setting expression 'name' with value 't'

下面是我的各个页面

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.devMode" value="true" />
    <package name="default" extends="struts-default">
        <action name="addEmployeeAction" class="org.ravi.EmployeeAction">
            <interceptor-ref name="params" />
            <interceptor-ref name="modelDriven"/>
            <result name="success">/Add.jsp</result>
        </action>
        <action name="EmployeeAction" class="org.ravi.EmployeeAction" method="execute">
            <interceptor-ref name="params" />
            <interceptor-ref name="modelDriven"/>
            <result name="success">/index.jsp</result>
        </action>
    </package>
</struts>

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.util.*,java.io.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>

    <s:form action="Add.jsp" name="addForm">
        <table border="1" cellpadding="5">
            <tr>
                <th>Select</th>
                <th>EmpID</th>
                <th>Name</th>
                <th>City</th>
                <th>DoB</th>
            </tr>

            <tr>
                <th><input type="radio" name="record"
                    onClick="radioValidate(this, 'record')" value="%{var}">
                </th>
                <th><s:property value="empid"/></th>
                <th><s:property value="name"/></th>
                <th><s:property value="city"/></th>
                <th><s:property value="dob"/></th>
    </s:form>
</body>
</html>

Add.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h1>Add New User</h1>
    <s:form action="EmployeeAction" >
    <s:textfield label="Emp Id" name="empid" />
    <s:textfield label="Name" name="name" />
    <s:textfield label="City" name="city" />
    <s:textfield label="DoB" name="dob" />          
    <s:submit />
    </s:form>
</body>
</html>

EmployeeAction.java

package org.ravi;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class EmployeeAction extends ActionSupport implements ModelDriven<Employee> {
    private static final long serialVersionUID = -8136507522861159378L;

    private Employee employee=new Employee();

    public Employee getEmployee()
    {
        return employee;

    }

    public void setEmployee(Employee employee)
    {
        this.employee=employee;
    }

    public String execute() throws Exception 
    {
        return SUCCESS;
    }

    @Override
    public Employee getModel() 
    {
        return employee;
    }
}

Employee.java

package org.ravi;

import java.io.Serializable;

public class Employee implements Serializable{

     static final long serialVersionUID = 1L;
     private String empid;
     private String name;
     private String city;
     private String dob;

    public void setempid(String empid) {
        this.empid = empid;
    }

    public void setname(String name) {
        this.name = name;
    }

    public void setcity(String city) {
        this.city = city;
    }

    public void setdob(String dob) {
        this.dob = dob;
    }

    public String getempid() {
        return this.empid;
    }

    public String getname() {
        return this.name;
    }

    public String getcity() {
        return this.city;
    }

    public String getdob() {
        return this.dob;
    }


}

你犯了几个错误,最糟糕的是

  • 你是手动生成getter和setter(很多无用的工作),而且你也做错了:变量名首字母必须大写:

    setName( instead of setname( for variable name.

    应该(为了简单和一致)也为你的变量的每个单词做一个以上的单词:

    setEmpId( for variable empId.

    您还应该考虑尽可能避免冗余。如果你在 class Employee 上有一个 ID 字段,只需将其命名为 id,而不是 empId... 如果它在 Emp 中,很明显它是 emp id 而不是什么东西否则 id.

  • 只有享受痛苦才使用ModelDriven。换个用途,当面对一群饥饿的流浪狗时,它就像你口袋里的一根香肠一样有用。

  • 使用 HTML5 DTD <!DOCTYPE html> 如果你的目标是旧浏览器,现在不需要使用 4.01

  • 永远不要像在第一种形式中那样直接调用 JSP,始终先通过操作。

从这里开始。还有很多。


必读

   public void setname(String name) {
        this.name = name;
    }

您需要正确命名所有这些,即 setName(String name) setEmpId 等也是如此。 Struts 找不到您的 getters/setters 因为他们不遵循命名约定。