Validation struts2编辑页面验证?
我有一个表,其中包含用户的配置文件,下面我提供了一个按钮(编辑配置文件),用户可以通过该按钮更新其配置文件,当用户单击编辑配置文件时,将显示一个新页面,其中包含用户的所有信息,并从数据库中填充 我面临的问题是,在这个页面中没有进行验证,比如说一个用户删除了他的用户名并试图更新他的个人资料,它应该显示*用户名是必需的Validation struts2编辑页面验证?,validation,struts2,Validation,Struts2,我有一个表,其中包含用户的配置文件,下面我提供了一个按钮(编辑配置文件),用户可以通过该按钮更新其配置文件,当用户单击编辑配置文件时,将显示一个新页面,其中包含用户的所有信息,并从数据库中填充 我面临的问题是,在这个页面中没有进行验证,比如说一个用户删除了他的用户名并试图更新他的个人资料,它应该显示*用户名是必需的 它不是抛出一些错误,而是没有进行填充验证,因为数据来自数据库,但我不确定,有人能在这方面帮助我吗 由于您没有发布任何代码,我不确定您使用的验证方法或您尝试过的验证方法。下面是一个使用
它不是抛出一些错误,而是没有进行填充验证,因为数据来自数据库,但我不确定,有人能在这方面帮助我吗 由于您没有发布任何代码,我不确定您使用的验证方法或您尝试过的验证方法。下面是一个使用
validate()
方法处理表单验证的示例操作
我最初忘记提到,您需要更改表单,以便表单提交到“myAction!submit”,而不仅仅是“myAction”
您使用的Struts2验证方法是什么?
validate()
方法还是XML驱动的验证?
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;
}
}