今天重新回顾了一下struts2框架,因为之前学习时,并未深入,导致遇到了一个动态方法调用和动态校验时,遇到多个input视图导致校验方法无法区分input,一开始我以为struts2会在配置文件中提供区分input视图的配置区分,最好找来找去也没找到,最后查看源码分析得到:inputResultName,这个是校验视图参数,我们只要修改这个参数就可以动态指定校验方法的input视图,但是我们不可能去直接修改源码吧,知道了这个,解决方案就出来了
分析过程:
action继承ActionSupport,但是,我们查看ActionSupport源码的validate方法,
package com.opensymphony.xwork2.ActionSupport
public void validate() {
}
发现什么也没有,但是我们看到了input这个参数,点开一看,这个是定义在action中的常量
package com.opensymphony.xwork2.Action;
public static final String INPUT = "input";
无法修改怎么班,
那就换条路,看看拦截器中是否调用过这个方法,找到,
public class DefaultWorkflowInterceptor extends MethodFilterInterceptor
修改过程,参看这个博文就好:
http://blog.csdn.net/a385833253/article/details/6914746
但是这样做太麻烦,struts2早就为我们考虑到了这个问题,使用注解:
@InputConfig(resultName=”regist”) 在相应的被校验方法上指定视图就好
如:
package com.leige.web.action;
import com.leige.domain.User;
import com.leige.exception.UserException;
import com.leige.service.UserService;
import com.leige.util.MD5Util;
import com.leige.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
public class UserAction extends ActionSupport implements ModelDriven<User> {
//用户接收页面参数
private User user=new User();
private static final long serialVersionUID = 4520512456046636470L;
/* service由spring注入*/
UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
@Override
public User getModel() {
return user;
}
/**
* 校验注册方法
*/
public void validateRegist() {
String username=user.getUsername();
//校验用户名,
if(StringUtil.isEmpty(username)){
this.addFieldError("username", "用户名不可为空");
}else if(!username.matches("\\w{3,15}")){
this.addFieldError("username", "用户名不合法,必须在3-15位之间");
}
//校验密码
String pwd=user.getPwd();
if(StringUtil.isEmpty(pwd)){
this.addFieldError("pwd", "密码不可为空");
}else if(!pwd.matches("\\w{3,15}")){
this.addFieldError("pwd", "密码不合法,必须在3-15位之间");
}else{//加密
user.setPwd(MD5Util.encode(pwd.trim()));
}
/* private String email;// 邮箱
private String phone;//电话
*/
//校验邮箱
String email=user.getEmail();
if(StringUtil.isEmpty(email)){
this.addFieldError("email", "邮箱不可为空");
}else if(!email.matches("[a-zA-Z0-9]+@[a-zA-Z0-9]+(\\.[a-zA-Z]{2,3}){1,3}")){
this.addFieldError("email", "邮箱不合法");
}
//校验电话
String phone=user.getPhone();
if(StringUtil.isEmpty(phone)){
this.addFieldError("phone", "电话不可为空");
}else if(!phone.matches("1[358]\\d{9}")){
this.addFieldError("phone", "电话不合法");
}
}
/**
* @return 注册方法
*/
@InputConfig(resultName="regist")
public String regist(){
try {
userService.regist(user);
//注册成功返回注首页
ActionContext.getContext().put("msg", "注册成功");
return "success";
} catch (UserException e) {
ActionContext.getContext().put("msg", e.getMessage());
//不成功返回注册页面
return "regist";
}
}
/**
* @return 登录方法
*/
@InputConfig(resultName="login")
public String login(){
try {
userService.login(user);
ActionContext.getContext().put("msg", "登录成功");
//登录成功返回首页
return "success";
} catch (UserException e) {
ActionContext.getContext().put("msg", e.getMessage());
//登录成功返回登录页面
return "login";
}
}
public void validateLogin() {
String username=user.getUsername();
//校验用户名
if(StringUtil.isEmpty(username)){
this.addFieldError("username", "用户名不可为空");
}else if(!username.matches("\\w{3,15}")){
this.addFieldError("username", "用户名不合法,必须在3-15位之间");
}
//校验密码
String pwd=user.getPwd();
if(StringUtil.isEmpty(pwd)){
this.addFieldError("pwd", "密码不可为空");
}else if(!pwd.matches("\\w{3,15}")){
this.addFieldError("pwd", "密码不合法,必须在3-15位之间");
}
}
/* public static void main(String[] args) {
System.out.println("[email protected]".matches("[a-zA-Z0-9]+@[a-zA-Z0-9]+(\\.[a-zA-Z]{2,3}){1,3}"));
String[] s={"1"};
Object obj=s;
System.out.println(s);
}*/
}
这样就可以实现多个校验方法对应多个input视图了,xml中不必须配置input
当然你如果使用一个action对应一个方法就没有这种问题,但是无疑增加了很多代码