- 论坛徽章:
- 0
|
Struts 1 内置了多步提交表单的方案,在 ActionForm 添加一个 page 属性,指定当前是第几步,将 ActionForm 放入 Session 中,或者在下一步表单中使用隐藏属性存放其上一步的表单数据,保证多步表单数据不丢失。Stripes 也提供了一种有效的方式,来处理多步提交。
回到注册程序,在填写注册信息之前,要求用户先阅读相关协议。
创建一个阅读协议 JSP 页面。
JSP Page
Read User License!
License,License,License,License,License,License,
License,License,License,License,License,License,
License,License,License,License,License,License,
License,License,License,License,License,License,
License,License,License,License,License,License,
I have read the license and gree with the content.
在RegisterActionBean中添加一个属性 acceptLicense ,类型为 boolean ,并添加相应的 event 方法。
@Wizard(startEvents = "readLicense")
public class RegisterActionBean extends BaseActionBean {
@Validate(required = true, mask = "[0-9a-zA-Z]{6,20}")
private String username;
@Validate(required = true, minlength = 6, maxlength = 20, mask = "[0-9a-zA-Z]+")
private String password;
@Validate(required = true, converter = EmailTypeConverter.class)
private String email;
@Validate(required = true, expression = "this eq password")
private String confirmPassword;
@Validate(converter = DateTypeConverter.class)
Date birthDate;
@Validate(converter = BooleanTypeConverter.class)
boolean subscriptionEnabled;
@ValidateNestedProperties({
@Validate(field = "zipcode", required = true),
@Validate(field = "addressLine1", required = true),
@Validate(field = "addressLine2", required = true)
})
private Address address;
private boolean acceptLicense = false;
public boolean isAcceptLicense() {
return acceptLicense;
}
public void setAcceptLicense(boolean acceptLicense) {
this.acceptLicense = acceptLicense;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public boolean isSubscriptionEnabled() {
return subscriptionEnabled;
}
public void setSubscriptionEnabled(boolean subscriptionEnabled) {
this.subscriptionEnabled = subscriptionEnabled;
}
public String getConfirmPassword() {
return confirmPassword;
}
public void setConfirmPassword(String confirmPassword) {
this.confirmPassword = confirmPassword;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@DefaultHandler
public Resolution register() {
return new ForwardResolution("/success.jsp");
}
@ValidationMethod(on = "register")
public void userExsited(ValidationErrors errors) {
if ("testuser".equals(username)) {
errors.add("username", new SimpleError("This username is taken , please select a different one."));
}
if (!errors.isEmpty()) {
errors.addGlobalError(new SimpleError("Error ocurs on save. Please fix it firstly."));
}
}
public Resolution readLicense() {
return new ForwardResolution("/readlicense.jsp");
}
@DontValidate
public Resolution back() {
return new ForwardResolution("/readlicense.jsp");
}
@ValidationMethod(on = "prepareRegister")
public void mustAcceptLicense(ValidationErrors errors) {
if (this.acceptLicense == false) {
errors.add("acceptLicense", new SimpleError("You must agree with the content of the license."));
}
if (!errors.isEmpty()) {
errors.addGlobalError(new SimpleError("Error ocurs on save. Please fix it firstly."));
}
}
public Resolution prepareRegister() {
return new ForwardResolution("/register.jsp");
}
}
Stripes 提供了 Wizard Annotation 保证多个页面的表单处理起来像在个页面一样。
在 register.jsp 页面添加一个回退到第一步按钮。
现在运行这个程序来体验一下。
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/1096/showart_1876519.html |
|