Struts2的参数二次绑定有什么作用?什么意思?有没有小例子?
------解决方案--------------------
http://blog.csdn.net/zzycgfans/article/details/5788024
------解决方案--------------------
action实现接口com.opensymphony.xwork2.Preparable
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.log4j.Logger;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.zglv.base.model.Person;
public class RegisterAction extends ActionSupport implements Preparable {
/**
*
*/
private static final long serialVersionUID = 8602551280879236965L;
private Logger logger = Logger.getLogger(this.getClass());
private Person person;
@Override
public String execute() throws Exception{
logger.info(person.toString());
return SUCCESS;
}
@Override
public void validate() {
if (person != null) {
if (StringUtils.isBlank(person.getFirstName())) {
addFieldError("person.firstName", "first name is required.");
}
if (person.getAge() < 18) {
addFieldError("person.age", "too younge, less than 18");
}
}
}
public void prepare() {
person = new Person();
person.setAge(40);
person.setEmail("email");
person.setFirstName("first name");
person.setLastName("lastName");
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
person类
public class Person {
private String firstName;
private String lastName;
private String email;
private int age;
假如对应的界面有两个属性
<form style="width: 50%;" action="register.action" method="post">
<fieldset>
<lable>firstName</lable>
<input type="text" name="person.firstName" >
<br>
<lable>age</lable>
<input type="text" name="person.age" >
<br>
<input type="submit" name="" value="submit"/>
</fieldset>
</form>
前台输入参数的时候,只输入了firstName和age属性,请求注册action后,经过prepare方法后,我们把person的属性全部赋值,然后方法execute中可观察到对象person中的属性是综合了前台和后台的属性。即前台传入的参数把person对象对应的属性覆盖了,而前台没有传过来的参数保持不变。这样就实现了参数的二次绑定。