SpringMVC自定义拦截器——实现自动装配Action字段
2017-06-09 14:22
363 查看
springmvc.xml配置文件:
自定义ModelDriverInterceptor工具类:
Action类:
UserInfo实体类:
jsp:
<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>
相关文章推荐
- SpringMVC自定义拦截器——实现自动装配Action字段
- springMvc拦截器实现操作日志的自动持久层
- springmvc自定义注解拦截器方式实现注解功能拦截器的配置
- springmvc 自定义拦截器实现未登录用户的拦截
- springmvc拦截器实现自动登录
- SpringMVC自定义注解和拦截器实现控制器访问次数限制
- springMVC系列(六)——拦截器实现自定义注解接口访问限制
- 拦截器springmvc防止表单重复提交【3】3秒后自动跳回首页【重点明白如何跳转到各自需要的页面没有实现 但是有思路】
- android之自定义ViewGroup和自动换行的布局的实现
- android之自定义ViewGroup和自动换行的布局的实现
- 2个堆栈实现自定义队列的入队出队方法 - 栈容量默认自动扩充
- android之自定义ViewGroup和自动换行的布局的实现
- javascript实现拼图游戏(支持自定义上传图片,后台自动分割)
- android之自定义ViewGroup和自动换行的布局的实现
- php 自定义函数实现博客文章预览,无乱码,自动补齐HTML标签
- SpringMVC 拦截器实现分析
- Struts2 自定义拦截器 实现简单权限检查
- struts2自定义拦截器实现的分析说明附带代码
- 自定义Behavior 实现Listbox自动滚动到选中项
- silverlight 自定义用户控件,实现邮箱地址输入自动提示邮箱域名