uot;/>)。这种JSTL格式标签还支持可用于Struts中的消息资源绑定。
Web程序从Struts向Stripes框架的移植(2)
时间:2011-01-02 天极 朱先忠
(三) 表单
我的移植的最有意义的部分是去掉了我的Struts Action表单,这是在Action类中进行的,要求大量的XML标记和冗长的转换,如下例所示:
<form-bean name="employeeUpdateForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="employeeid" type="java.lang.Long" />
<form-property name="firstname" type="java.lang.String" />
<form-property name="lastname" type="java.lang.String" />
<form-property name="phone" type="java.lang.String" />
<form-property name="email" type="java.lang.String" />
<form-property name="phone" type="java.lang.String" />
<form-property name="socialsecurity" type="java.lang.String" />
<form-property name="birthdate" type="java.lang.String" />
<form-property name="salary " type="java.lang.String" />
</form-bean>
public ActionForward executeAction(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
Employee employee=new Employee();
DynaValidatorForm eaf = (DynaValidatorForm) form;
employee.setFirstname(eaf.getString("firstname"));
employee.setLastname(eaf.getString("lastname"));
employee.setPhone (eaf.getString("phone"));
employee.setEmail (eaf.getString("email"));
employee.setEmployeeid ((Integer)eaf.get("employeeid"));
employee.setSocialsecurity(Long.parseLong(eaf.getString("socialsecurity")));
employee.setBirthdate(MyUtils.convertStringToDate(eaf.getString("birthdate")));
employee.setSalary(MyUtils.convertStringToBigDecimal(eaf.getString("salary")));
EmployeeDAOService.updateEmployee(employee);
return new ActionForward(mapping.getForward());
}
另一方面,Stripes表单处理允许你把你的域对象用作一个表单使用:
public class UpdateEmployeeActionBean implements ActionBean {
private ActionBeanContext context;
private Employee employee;
public ActionBeanContext getContext() {
return context;
}
public void setContext(ActionBeanContext context) {
this.context = context;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public Employee getEmployee() {
return this.employee;
}
@DefaultHandler
public Resolution update() {
EmployeeDAOService.updateEmployee(employee);
return new ForwardResolution("/employees/updateEmployee.jsp");
}
}
在大多数情况中,我能够嵌入我的域对象的一个副本作为我的Stripes ActionBean类的一个属性并且仅包括涉及把该对象移入/移出我的持久层的代码部分。我把所有表单处理都交给了Struts Action来实现,包括初始化配置、把表单强制转换成适当的类以及从域对象中来回转换数据(约30%的代码是在大多数Action类中实现的)。在Stripes中并不需要这样。
|