ognl.OgnlException:setProperty 的目标为空...? [英] ognl.OgnlException: target is null for setProperty...?
问题描述
我是 Struts 2 的新手.我正在创建一个演示 Web 应用程序,它允许用户在 jsp 上提交员工详细信息并在下一个 jsp 上显示它们.代码如下:
I'm new to Struts 2. I'm creating a demo web application which allows a user to submit employee details on a jsp and show them on the next jsp. Following is the code:
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>
动作类
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;
}
}
在上面的action类中,Employee是一个单独的模型类,具有以下属性:
In the above action class, Employee is a separate model class with the following attributes:
String employeeID;
String name;
String department;
以下是我添加员工的 JSP 页面:
Following is my JSP page to add an employee:
<%@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>
当我点击提交按钮时填写员工详细信息后,我在employeeDetails()方法中得到一个空指针异常:
After filling in the employee details when I click on the Submit button, I get a nullpointer exception in the employeeDetails() method:
java.lang.NullPointerException
com.webapp.test.action.MyAction.employeeDetails(MyAction.java:19)
并且服务器控制台显示以下异常:
and the server console displays the following exceptions:
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)
请解释问题是什么以及如何从 JSP 填充员工模型对象.我不想在 Action 类中创建 getter setter 方法,也不想使用 ModelDriven 接口.
Please explain what is the problem and how can I fill the employee model object from the JSP. I do not want to create getter setter methods in the Action class and also I do not want to use the ModelDriven interface.
推荐答案
MyAction<中缺少
Employee
对象引用 emp
的 getter setter 方法/code> 类.
The getter setter methods for the Employee
object reference emp
were missing in the MyAction
class.
创建方法后问题解决.
谢谢!!!
这篇关于ognl.OgnlException:setProperty 的目标为空...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!