2014-12-04 63 views
2

假設我有下面的類第一次提交表單後重新分配值標籤?

public class Account { 

    private Long id; 
    private String username; 
    private String password; 

    // getters + constructor 

} 

下面的動作類

public MyAction extends ActionSupport { 

    private CoreService service; 

    private long id; 
    private String username; 
    private String password; 

    private Account account; 

    public String loadAccount() { 
    account = service.getAccountById(id); 
    return SUCCESS; 
    } 

    public String updateAccount() { 
    service.updateAccount(account); 
    return SUCCESS; 
    } 
    // getters + setters 
} 

我使用loadAccount()方法來填充表單字段。

我的形式:

<form action="update_account" method="post"> 
     <label for="username">New username</label> 
     <input type="text" name="username" value="${account.username}" id="username" placeholder="Enter name" required> 

     <label for="password">New password</label> 
     <input type="password" name="password" id="password" placeholder="Enter new password" required> 
</form> 

現在,假設用戶將寫下一個新的用戶名,但驗證失敗。 「用戶名」文本字段中的值將重置爲帳戶的用戶名。我想改變我的程序,並使其初始加載帳戶的用戶名,但如果驗證失敗,它會加載最後一次輸入。

+0

你只需要改變一個表達式後將其設置方法的代碼,那麼它會保留最後的值。 – 2014-12-04 17:24:14

+0

它最初是否會加載我帳戶的用戶名? – 2014-12-04 17:28:31

+0

是的,如果你把它設置在現場。 – 2014-12-04 17:30:07

回答

1

更改爲顯示領域

<input type="text" name="username" value="${username}" id="username" placeholder="Enter name" required> 

然後加載

public String loadAccount() { 
    account = service.getAccountById(id); 
    setUsername(account.getUserName()); 
    return SUCCESS; 
} 
相關問題