ognl.OgnlException:setProperty的target为null ...?

n_g*_*n_g 6 java struts2

我是Struts 2的新手.我正在创建一个演示Web应用程序,它允许用户在jsp上提交员工详细信息并在下一个jsp上显示它们.以下是代码:

在struts.xml

<struts>
    <constant name="struts.custom.i18n.resources" value="ApplicationResources" />

    <!-- Configuration for the default package. -->
    <package name="default" extends="struts-default" namespace="/">

        <action name="empDetails" class="com.webapp.test.action.MyAction"
            method="employeeDetails">
            <result name="success">jsp/employeeDetails.jsp</result>
        </action>

        <action name="addEmployee" class="com.webapp.test.action.MyAction"
            method="addEmployee">
            <result name="success">jsp/addEmployee.jsp</result>
        </action>
    </package>
</struts>
Run Code Online (Sandbox Code Playgroud)

动作类

public class MyAction extends ActionSupport{

    private static final long serialVersionUID = 1L;

    private Employee emp = null; 

    public String addEmployee(){
        System.out.println("In addEmployee");
        return SUCCESS;
    }

    public String employeeDetails(){
        System.out.println("In employeeDetails");
        System.out.println("Employee ID: "+emp.getEmployeeID());
        return SUCCESS;
    }
}
Run Code Online (Sandbox Code Playgroud)

在上面的操作类中,Employee是一个单独的模型类,具有以下属性:

String employeeID;
String name;
String department;
Run Code Online (Sandbox Code Playgroud)

以下是我添加员工的JSP页面:

<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Employee</title>
</head>
<body>
<s:form action="empDetails">
<s:textfield name="emp.employeeID" label="Employee ID"></s:textfield>
<s:textfield name="emp.name" label="Name"></s:textfield>
<s:textfield name="emp.department" label="Department"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

单击Submit按钮后填写员工详细信息后,我在employeeDetails()方法中得到一个nullpointer异常:

java.lang.NullPointerException
    com.webapp.test.action.MyAction.employeeDetails(MyAction.java:19)
Run Code Online (Sandbox Code Playgroud)

并且服务器控制台显示以下异常:

ognl.OgnlException: target is null for setProperty(null, "department", [Ljava.lang.String;@acd4c9)
.....
ognl.OgnlException: target is null for setProperty(null, "employeeID", [Ljava.lang.String;@c5dd98)
....
ognl.OgnlException: target is null for setProperty(null, "name", [Ljava.lang.String;@5753b0)
Run Code Online (Sandbox Code Playgroud)

请解释问题是什么以及如何从JSP填充员工模型对象.我不想在Action类中创建getter setter方法,也不想使用ModelDriven接口.

n_g*_*n_g 6

类中缺少Employee对象引用的getter setter方法.empMyAction

创建方法后问题已解决.

谢谢!!!