2011-03-07 110 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; 
    } 
}