springmvc.xml配置文件:
<mvc:interceptors>
<!-- 引用自定义拦截器
path="/user/**/" user是包名
-->
<mvc:interceptor>
<mvc:mapping path="/user/**/" />
<bean class="cn.et.springmvc.lesson04.utils.ModelDriverInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
自定义ModelDriverInterceptor工具类:
package cn.et.springmvc.lesson04.utils;
import java.lang.reflect.Field;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
* 自定义拦截器
* @author Administrator
*
*/
public class ModelDriverInterceptor implements HandlerInterceptor {
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
}
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
// TODO Auto-generated method stub
}
/**
* 拦截器表示在Action被调用之前
* true表示继续调用
* false中断调用
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
HandlerMethod hm = (HandlerMethod)handler;
//获取到当前action实例
Object curObject=hm.getBean();
//获取到Action的结构user属性
Field[] fs=curObject.getClass().getDeclaredFields();
for (Field field : fs) {
//设置权限
field.setAccessible(true);
//获取Action的字段名
String name=field.getName();
//获取字段的实例
Object obj=field.get(curObject);
if(obj==null){
//对字段进行实例化
obj=field.getType().newInstance();
//再把实例化好的字段设置到Action里去
field.set(curObject, obj);
}
//枚举所有的参数名称
//user.userName=zs&user.password=123456
Enumeration<String> enu = request.getParameterNames();
while(enu.hasMoreElements()){
//迭代request里甩有的参数
String paramName=enu.nextElement();
/**
* 参数名为 user.userName进行字段对象的处理
*/
//判断参数名是否以user开头
if(paramName.indexOf(".")>=0 && paramName.startsWith(name)){
//获取到参数的值
String paramValue=request.getParameter(paramName);
//获取 到参数名后面的字段 user.userName userName
String pName=paramName.split("\\.")[1];
//获取到user实例的字段userName
Field ff=obj.getClass().getDeclaredField(pName);
ff.setAccessible(true);
//把属性设置到user实例中
ff.set(obj, paramValue);
}
/**
* 参数名为Action字段的时候进行字段处理
*/
if(paramName.equals(name)){
//
String paramValue=request.getParameter(paramName);
//获取到. userName
Field ff=curObject.getClass().getDeclaredField(paramName);
ff.setAccessible(true);
ff.set(curObject, paramValue);
}
}
}
return true;
}
}
Action类:
package cn.et.springmvc.lesson04;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(value="/user")
public class UserAction {
private UserInfo user;
@RequestMapping(value="/userAction")
public String va(){
System.out.println(123);
return null;
}
public UserInfo getUser() {
return user;
}
public void setUser(UserInfo user) {
this.user = user;
}
}
UserInfo实体类:
package cn.et.springmvc.lesson04;
public class UserInfo {
String userName;
String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="s"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="st"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'form.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="${pageContext.request.contextPath}/user/userAction">
用户名:<input type="text" name="user.userName" /></br>
密 码:<input type="password" name="user.password" /></br>
<input type="submit" value="提交" />
</form>
</body>
</html>