Reputation: 642
Assume I have the following class
public class Account {
private Long id;
private String username;
private String password;
// getters + constructor
}
The following action class
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
}
I use the loadAccount() method to populate the form's fields.
And my form:
<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>
Now, assume the user will write down a new username but the validation fails. The value inside 'username' textfield will be reset to account's username. I want to change my program and make it initially load account's username but then if validation fails it will load the last input.
Upvotes: 2
Views: 33
Reputation: 1
Change the code for displaying a field
<input type="text" name="username" value="${username}" id="username" placeholder="Enter name" required>
then a method to set it after loading
public String loadAccount() {
account = service.getAccountById(id);
setUsername(account.getUserName());
return SUCCESS;
}
Upvotes: 1