免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1496 | 回复: 0
打印 上一主题 下一主题

Stripes tips(13)- 多页面表单处理 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-23 18:37 |只看该作者 |倒序浏览

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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP