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
项目结构: