2011-03-07 109 views
0

我有一个表,其中包含用户的配置文件,下面我给出了一个用户可以更新他/她的配置文件的按钮(编辑配置文件),当用户单击编辑配置文件时,将显示一个新的页面包含用户的所有信息,并从数据库填充。struts2编辑页面验证?

我现在面临的问题是,验证不在这里发生在此页面中,假设某个用户删除其用户名和尝试更新他的个人资料,应该显示*必须填写用户名 ,而不是它抛出一些错误,我填充验证没有发生在这里,因为数据来自数据库,但我不知道,任何人都可以帮助我。

+0

显示动作和jsp。 – Quaternion 2011-03-07 14:58:26

+0

您使用的是什么Struts2验证方法? 'validate()'方法或XML驱动验证? – 2011-03-07 16:32:46

回答

0

由于您没有发布任何代码,我不确定您使用的验证方法是什么,或者您尝试了什么。以下是使用validate()方法处理表单验证的示例操作。

我原本忘记提及您需要更改表单,以便提交给“myAction!submit”而不仅仅是“myAction”。

public class MyAction extends ActionSupport { 
    /** 
    * Your profile, as loaded from the database. 
    */ 
    private Profile profile; 

    /** 
    * SkipValidation is required so that the validate() method is not invoked 
    * prior to the execute() method. Otherwise, all fields would be blank and 
    * we would end up showing validation errors on all fields. 
    */ 
    @SkipValidation 
    @Override 
    public String execute() throws Exception { 
     return INPUT; 
    } 

    @Override 
    public void validate() { 
     if (StringUtils.isEmpty(profile.getUsername())) { 
      addFieldError("profile.username", "User Name is required."); 
     } 

     // place any additional validations here... 
     // you can and should create convenience methods for performing validations. 
    } 

    public String submit() throws Exception { 
     // persist the changes and return an appropriate response 
     // e.g., redirect somewhere 
     return SUCCESS; 
    } 
}