日期:2014-05-18 浏览次数:20841 次
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;
}
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>