spring mvc 有两种配置:(1)注解;(2)基于xml(schema)。
公司要求使用xml方式的,其实在我看来注解更方便;
如何使用xml配置spring mvc的拦截器呢?
场景:比如用户通过浏览器访问我的网站时,我需要判断用户是否已经登录过,若登录过,则允许访问,否则自动跳转到登陆页面。
spring mvc中拦截器类需要继承HandlerInterceptorAdapter 类
代码如下:
package com.yj.mvc.controler.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; public class HandlerInterceptor1 extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("===========HandlerInterceptor1 preHandle"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("===========HandlerInterceptor1 postHandle"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("===========HandlerInterceptor1 afterCompletion"); } }
比较重要的方法时preHandle,如果返回false,就请求不会往下传递,就会拒绝请求。所以该方法可用于进行访问控制。
控制器代码如下:
package com.yj.mvc.controler; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import org.springframework.web.servlet.view.json.MappingJacksonJsonView; public class DemoController implements Controller { private String name; private String age; public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("request.getRequestURL():"+request.getRequestURL()); System.out.println("request.getServletPath():"+request.getServletPath()); Map root = new HashMap(); ModelAndView modelAndView =new ModelAndView(new MappingJacksonJsonView(), root); return modelAndView; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
spring mvc配置文件:springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- HandlerAdapter --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> <!-- ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="viewNames"> <list> <value>org.springframework.web.servlet.view.json.MappingJacksonJsonView </value> </list> </property> </bean> <mvc:resources mapping="/resource/**" location="/WEB-INF/resource/" /> </beans>
web.xml配置如下 :
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name></display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:beans.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
beans.xml内容:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd" default-lazy-init="false"> <!-- url映射列表,所有控制层Controller的访问路径在这里定义 --> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="demo.action">demoController</prop> <prop key="demo222.action">demoController</prop> </props> </property> <property name="interceptors"> <list> <ref bean="handlerInterceptor2" /> </list> </property> </bean> <!-- 控制器的父类 --> <bean id="demoController" class="com.yj.mvc.controler.DemoController"> </bean> <!-- 系统业务参数 --> <!-- <bean name="businessParamViewConf" class="com.tdr.otp.ats.conf.BusinessParamViewConf" --> <!-- parent="baseController" > --> <!-- <property name="businessParamViewService"> --> <!-- <ref bean="businessParamViewService" /> --> <!-- </property> --> <!-- </bean> --> <bean id="handlerInterceptor2" class="com.yj.mvc.controler.interceptor.HandlerInterceptor1" /> </beans>
启动服务器后,在浏览器中访问:http://localhost:8080/springmvc_xml/demo.action
后台打印:
===========HandlerInterceptor1 preHandle
request.getRequestURL():http://localhost:8080/springmvc_xml/demo.action
request.getServletPath():/demo.action
===========HandlerInterceptor1 postHandle
===========HandlerInterceptor1 afterCompletion
项目使用maven构建
IDE:eclipse
项目结构:
源代码见附件springmvc_xml.zip
相关推荐
九、spring mvc 中的拦截器: 十、spring mvc 如何使用拦截器? 十一、spring mvc 如何实现全局的异常处理? 十二、spring mvc 如何把全局异常记录到日志中? 十三、如何给spring3 MVC中的Action做JUnit单元测试? ...
九、spring mvc 中的拦截器: 十、spring mvc 如何使用拦截器? 十一、spring mvc 如何实现全局的异常处理? 十二、spring mvc 如何把全局异常记录到日志中? 十三、如何给spring3 MVC中的Action做JUnit单元测试? ...
九、spring mvc 中的拦截器: 十、spring mvc 如何使用拦截器? 十一、spring mvc 如何实现全局的异常处理? 十二、spring mvc 如何把全局异常记录到日志中? 十三、如何给spring3 MVC中的Action做JUnit单元测试? ...
Spring mvc 配置: WEB/INF/springmvc-servlet.xml//编码过滤器//静态资源忽略//AOP代理方式设定//拦截器配置//cglib动态代理
后台全注解式的开发(除了必要的spring和hibernate的xml配置以外) 03. 后台通过自定义注解结合一个访问拦截器实现整个系统的权限控制 04. 系统前台采用全采用的Html+jQuery开发 05. 系统前台与后台的交互全部...
九、 spring mvc 中的拦截器: 十、 spring mvc 如何使用拦截器 十一、 spring mvc 如何实现全局的异常处理 十二、 spring mvc 如何把全局异常记录到日志中 十三、 如何给spring3 MVC中的Action做JUnit单元测试 十四...
一、 XML,使用tx标签配置拦截器实现事务 一、 Annotation方式 以下所使用环境为Spring4.0.3、Hibernate4.3.5
基于spring2.5的采用XML配置的spring MVC项目 基于spring2.5注解实现的spring MVC项目 基于spring 3.0项目开发实例 基于spring 3.0mvc 框架的文件上传实现 Spring中的拦截器
* 提供了强大的拦截器机制,简化了安全和认证机制的实现。 结论 Spring MVC 框架提供了一个强大、灵活的 Web 应用程序开发框架。通过理解 Spring MVC 的工作原理和组件的角色,可以更好地使用 Spring MVC 框架开发...
spring mvc的学习笔记,主要讲解了基于xml的配置,和如何增加spring mvc拦截器,如何返回json报文
1,搭建SpringMVC 2,加载properties配置文件 3,集成mybatis 4,配置事务 5,另一种配置springMVC的方式 6,自定义(扩展)mvc配置 7,解决@ResponseBody return String的中文乱码问题 8,配置静态资源映射...11,配置拦截器
4、拦截器可以利用依赖注入,因此在spring框架程序中,优先拦截器 5、拦截器是包裹在过滤器中使用的。 复习 converter 转换器 i18n struts2 spring MVC 拦截器 interceptor 过滤器 filter web.xml ...
1,搭建SpringMVC 2,加载properties配置文件 3,集成mybatis 4,配置事务 5,另一种配置springMVC的方式 6,自定义(扩展)mvc配置 7,解决@ResponseBody return String的中文乱码问题 8,配置静态资源映射...11,配置拦截器
完成了一个名为HplussApp的String MVC应用程序,可帮助您在公司的库存中查找产品。... 前端:JSP,CSS,Spring表单标签。后端:Java,SpringBoot,Spring MVC,JPA,...使用拦截器。定义主题。 Spring MVC REST控制器。
DispatcherServlet是前置控制器,配置在web.xml文件中的。拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据某某规则分发到目标Controller(我们写的Action)来处理。 “某某规则”:是根据...
4.4.2 拦截器配置 89 4.4.3 @ControllerAdvice 91 4.4.4 其他配置 94 4.5 Spring MVC 的高级配置 98 4.5.1 文件上传配置 98 4.5.2 自定义HttpMessageConverter 101 4.5.3 服务器端推送技术 106 4.6 Spring MVC 的...
拦截器配置 @ControllerAdivce @ExceptionHandler @InitBinder @ModelAttribute 其他配置 ViewController 路径匹配参数配置 WebMvcConfigurerAdapter WebMvcConfigurer ...
4.4.2 拦截器配置 89 4.4.3 @ControllerAdvice 91 4.4.4 其他配置 94 4.5 Spring MVC 的高级配置 98 4.5.1 文件上传配置 98 4.5.2 自定义HttpMessageConverter 101 4.5.3 服务器端推送技术 106 4.6 Spring MVC 的...