spring mvc系列文章 - 拦截器篇
来源:互联网 发布:c语言打印五环 编辑:程序博客网 时间:2024/06/06 10:56
今天给项目加个拦截器,结合之前的系列文章,这里给出两个需要新增的文件。
1 interceptor.java
package com.springmvc.interceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;/** * 拦截器 * * @author jiaojun * */public class StudentInterceptor implements HandlerInterceptor {/** * 在Controller方法后进行拦截 * 当有拦截器抛出异常时,会从当前拦截器往回执行所有拦截器的afterCompletion方法 */ @Overridepublic void afterCompletion(HttpServletRequest arg0,HttpServletResponse arg1, Object arg2, Exception arg3)throws Exception {arg0.setAttribute("end", "终于毕业了");System.out.println("终于毕业了!");}/** * 当Controller执行完毕,Interceptor会调用postHandle来做后置处理 */@Overridepublic void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,Object arg2, ModelAndView arg3) throws Exception {System.out.println("饱经沧桑,苦读诗书!");}/** * 在Controller方法前进行拦截 * 如果返回false * 从当前拦截器往回执行所有拦截器的afterCompletion方法,再退出拦截器链. * 如果返回true * 执行下一个拦截器,直到所有拦截器都执行完毕. * 再运行被拦截的Controller. * 然后进入拦截器链,从最后一个拦截器往回运行所有拦截器的postHandle方法. * 接着依旧是从最后一个拦截器往回执行所有拦截器的afterCompletion方法. */ @Overridepublic boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,Object arg2) throws Exception {arg0.setAttribute("start", "入学登记开始");System.out.println("入学登记开始");return true;}}
2 interceptor 配置文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd" default-autowire="byName"> <!-- 引入配置文件 <bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location"><value>classpath:mysql.properties</value></property></bean>--><!--创建jdbc数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/><property name="password" value="jiaojun"/> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 创建SqlSessionFactory,同时指定数据源 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="dataSource" ref="dataSource" /> </bean> <!--以注解方式扫描Spring Bean --> <context:annotation-config /> <!--配置扫描SpringBean的时候 都需要扫描哪些目录下 --> <context:component-scan base-package="com.springmvc"></context:component-scan> <!-- 可通过注解控制事务 --> <tx:annotation-driven /> <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper --> <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.springmvc.dao" /> </bean> --> <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.springmvc.dao.StudentMapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> <!-- jsp视图配置 --> <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> --> <!-- freemarker视图解析配置 --><bean id="viewResolver"class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"><property name="cache" value="true" /><property name="prefix" value="" /><property name="suffix" value=".ftl" /><property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" /><property name="contentType" value="text/html;charset=UTF-8"></property><property name="requestContextAttribute" value="request" /><property name="exposeSpringMacroHelpers" value="true" /><property name="exposeRequestAttributes" value="true" /><property name="exposeSessionAttributes" value="true" /></bean> <!-- freemark初始化配置项 --><bean id="freemarkerConfig"class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"><property name="templateLoaderPath" value="/WEB-INF/template/" /><property name="freemarkerSettings"><props><!-- 如果模板不经常更新,此属性设置更新延迟时间 --><prop key="template_update_delay">60</prop><prop key="default_encoding">UTF-8</prop><prop key="number_format">#.###</prop><prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop><prop key="classic_compatible">true</prop><prop key="template_exception_handler">ignore</prop></props></property></bean><!-- 拦截器配置项 --><mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.springmvc.interceptor.StudentInterceptor" /> </mvc:interceptor> <!-- ................. 1.拦截器按照先后顺序进行拦截; 2.拦截全部请求时使用"/**"; 3.拦截只针对controller. --> </mvc:interceptors> </beans>
package com.springmvc.action;import java.util.List;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import com.springmvc.domain.Student;import com.springmvc.service.StudentService;@Controller//@RequestMapping(value ="/") public class StudentController { @Resource(name = "studentService") private StudentService studentService; @RequestMapping(value = "/studentController.do") public String getStudent(HttpServletRequest request, HttpServletResponse response) throws Exception { Student student = studentService.queryStudentById(12); student.setId(12); student.setName("wodeguiguai"); //studentService.addStudent(student); // studentService.delStudent(1); student.setName("haohaoxiansheng"); studentService.updateStudent(student); request.setAttribute("student", student); System.out.println("考试,检验你的时刻到了!"); return "test"; } }
输出结果:
入学登记开始
考试,检验你的时刻到了!
饱经沧桑,苦读诗书!
终于毕业了!
源码下载地址:http://download.csdn.net/detail/sl0007/4496354
- spring mvc系列文章 - 拦截器篇
- Spring MVC系列(二):拦截器配置
- Spring MVC 拦截器
- Spring MVC 拦截器
- Spring MVC 拦截器
- spring MVC拦截器
- Spring MVC拦截器
- spring mvc 拦截器
- spring mvc 拦截器
- spring mvc 拦截器
- Spring mvc 拦截器
- spring mvc 拦截器
- spring mvc 拦截器
- spring mvc 拦截器
- Spring MVC 拦截器
- spring mvc拦截器
- spring mvc拦截器
- spring mvc拦截器
- 我升级了
- 汇编语言 -- 04
- mysql order by case when
- sp wp RefBase
- Android开发—数据库应用—手动创建(SQLite)数据库--手动创建数据表(table)
- spring mvc系列文章 - 拦截器篇
- J2SE基础夯实系列之String字符串拼接原理剖析
- SQL Server sa 用户密码的修改方法
- VLC发送接收RTP码流
- 我要学ASP.NET MVC 3.0(十): MVC 3.0 使用 Forms身份验证
- windows后台服务程序范例
- awk正则匹配列中指定的字符
- java 计算PMT公式
- Oracle to_date