Controller包
-----------------------------------HelloController------------------
package Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/hello.do")
public String hello() {
System.out.println("hello方法");
return "hello";
}
/**
*这个方法,拦截器不会拦截 ,
*除非配置中加两个**
*<mvc:mapping path="/**"/>
*/
@RequestMapping("/demo/hello.do")
public String hello2() {
System.out.println("hello2()方法");
return "hello";
}
}
===================interceptors包 HandlerInterceptor类=====================================
package interceptors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
*拦截器
*
*/
public class SomeInterceptor implements HandlerInterceptor{
/**
* 前端控制器收到请求之后,会先调用preHandle的方法,如果该方法返回值为true,
* 表示继续向后调用,否则请求处理结束
* preHandle方法的第三个参数Object,是表示处理器的方法对象。
* (此处的对象指的是hello()方法。在反射机制中,可以用一个对象表示)
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("这是preHandle方法");
return true;
}
/**
* 处理器Controller的方法执行完毕,准备返回ModelAndView给前端控制器之前
* 执行postHandle方法
*/
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("postHandle()方法");
}
/**
* 整个请求已经处理完毕,执行的方法。
* Exception ex是处理器方法运行时抛出的异常,
*
*/
public void afterCompletion(
HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex)
throws Exception {
System.out.println("afterCompletion()方法");
}
}
------------------------------------------------spring-mvc.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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 配置组件扫描,会扫描Controller下面所有的包,子包下的所有类,当有发现类上有注解,会当成bean来管理 -->
<context:component-scan base-package="Controller"></context:component-scan>
<!--组件扫描只管4个注解(service,conponent,controller ,repostly).RequestMapping属于springmvc特定的注解,需要配置mvc注解扫描
不然RequestMapping注解就不起作用了 -->
<mvc:annotation-driven/>
<!-- 配置视图解析器 。prefix表示前缀-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置拦截器 ,如果有多个拦截器,则依据配置的先后顺序拦截-->
<mvc:interceptors>
<mvc:interceptor>
<!-- 要拦截的路径,由path决定 ,如果要拦截多层次的请求加两个*-->
<mvc:mapping path="/**"/>
<!-- 拦截器类 -->
<bean class="interceptors.SomeInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>springcase-springmvc03</display-name>
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- DispatcherServlet的初始化方法会启动spring
容器,所以需要配置初始化参数来指定spring配置文件的位置
如果没有配置初始化参数,则会找WEB-INF/*-servlet.xml ,*与前端控制器的name一样 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
====================hello.jsp=====================
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>first spring-mvc</title>
</head>
<body style="font-size: 30px;font-style: italic;">
Hello,SpringMVC!
</body>
</html>