今天做struts2和spring集成的时候遇到一个问题,问题的现象是:在struts中用一个属性来收集form表单中的数据,这个属性是实体类对象,发现该实体类对象必须实例化一下,否则的话报空指针异常。
jsp页面:
<form action="save" method="post">
事由:
<input name="coucher.event" type="text"></input>
<input type="submit" value="保存"/>
</form>
struts2配置文件:
<action name="save" class="claimvoucher" method="save">
<result name="success">index.jsp</result>
</action>
action类代码:
private Claimvoucher coucher;
public Claimvoucher getCoucher() {
return coucher;
}
public void setCoucher(Claimvoucher coucher) {
this.coucher = coucher;
}
public String save(){
System.out.println(coucher.getEvent());
return SUCCESS;
}
Claimvoucher实体类有两个属性:
private Employee employee;
private String event;
spring配置文件
<bean id="employee" class="com.bdqn.hr.action.EmployeeAction" scope="prototype">
</bean>
<bean id="claimvoucher" class="com.bdqn.hr.action.ClaimvoucherAction" scope="prototype">
</bean>
后来发现跟spring配置文件中的id="employee"的bean有关系,因为Claimvoucher这个实体类中有一个属性为employee,和spring的bean的id重复了。不知道原因是什么,解决这个问题花费了半天。特记录一下。如果命名规范一些就不会出现这个问题了。