struts2 edit page validation?
I have a table which contains the profile of the user and below that i have given a button(Edit Profile) through which the user can update his/her profile, when a user click the Edit Profile a new page will be displayed which contains all the information of the user and it is populated from the database.
The problem i am facing is that validation is not happening here in this page, say a user delete his user name 开发者_Python百科and tries to update his profile it should show * User Name is required instead of that it is throwing some error, i fill validation is not happening here because the data are coming from the database but i am not sure, can anyone help me in this.
Since you didn't post any code, I'm not sure what method of validation you are using or what you have tried. Here's an example action using the validate()
method to handle form validation.
I originally forgot to mention that you will need to change for form so that it submits to "myAction!submit" rather than just "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;
}
}
精彩评论