配置springmvc 拦截器配置有哪几个步骤

  使用springMVC也可以代替struts2,当然只是代替业务分发的功能,struts2的一些其他功能它是没有的,不然要struts2有什么用。  下面我用springMVC代替struts2去整合hibernate实现简单的员工查询功能。  使用springMVC有两个配置文件需要配置,一个是applicationContext.xml、另一个是web.xml,在applicationContext.xml里面配置事务管理器以及属性注入等。web.xml里面要添加一个springMVC的servlet的注册和映射(DispatcherServlet),这个servlet是springMVC的核心控制器,专门处理各个请求的,然后根据相应的参数分发给相应的业务控制器处理,业务控制器处理完之后就会返回一字符串给核心控制器,核心控制器再根据该字符串重定向或者转发到相应的页面。还必须给该核心控制器建一个配置文件,其形式为:核心控制器servlet名-servlet.xml,如springMVC-servlet.xml.该配置文件放在WEB-INF下面。&applicationContext.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:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"&
&bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&
&property name="configLocation"
value="classpath:hibernate.cfg.xml"&
&/property&
&bean id="DeptDAO" class="com.dao.DeptDAO"&
&property name="sessionFactory"&
&ref bean="sessionFactory" /&
&/property&
&bean id="EmpDAO" class="com.dao.EmpDAO"&
&property name="sessionFactory"&
&ref bean="sessionFactory" /&
&/property&
&bean id="empService" class="com.service.EmpService"&
&property name="deptDAO" ref="DeptDAO"&&/property&
&property name="empDAO" ref="EmpDAO"&&/property&
&!-- 事务管理器 --&
&bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&
&property name="sessionFactory" ref="sessionFactory"&&/property&
&!-- 事务属性 --&
&tx:advice id="mytx" transaction-manager="transactionManager"&
&tx:attributes&
&tx:method name="*"/&
&/tx:attributes&
&/tx:advice&
&!-- 织入 --&
&aop:config&
&aop:advisor advice-ref="mytx" pointcut="execution(* com.service.*.*(..))"/&
&/aop:config&
web.xml的内容如下:
&?xml version="1.0" encoding="UTF-8"?&
&web-app version="2.5" xmlns="/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="/xml/ns/javaee
/xml/ns/javaee/web-app_2_5.xsd"&
&!-- 指定另一个配置的路径 --&
&context-param&
&param-name&contextConfigLocation&/param-name&
&param-value&/WEB-INF/classes/app*.xml&/param-value&
&/context-param&
&!-- 控制session开关的过滤器 --&
&filter-name&openSession&/filter-name&
&filter-class&org.springframework.orm.hibernate3.support.OpenSessionInViewFilter&/filter-class&
&filter-mapping&
&filter-name&openSession&/filter-name&
&url-pattern&/*&/url-pattern&
&/filter-mapping&
&!-- 加载applicationContext.xml --&
&listener&
&listener-class&org.springframework.web.context.ContextLoaderListener&/listener-class&
&/listener&
&!-- springMVC 注册和映射 --&
&servlet-name&springMVC&/servlet-name&
&servlet-class&org.springframework.web.servlet.DispatcherServlet&/servlet-class&
&/servlet&
&servlet-mapping&
&servlet-name&springMVC&/servlet-name&
&url-pattern&*.do&/url-pattern&
&/servlet-mapping&
&welcome-file-list&
&welcome-file&index.jsp&/welcome-file&
&/welcome-file-list&
&login-config&
&auth-method&BASIC&/auth-method&
&/login-config&
&/web-app&
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:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"&
&!-- 指定自动扫描路径 --&
&context:component-scan base-package="com.action"&&/context:component-scan&
EmpAction类代码如下:
package com.
import java.util.L
import javax.servlet.http.HttpServletR
import javax.servlet.http.HttpServletR
import org.springframework.beans.factory.annotation.A
import org.springframework.stereotype.C
import org.springframework.web.bind.annotation.RequestM
import com.service.EmpS
@Controller
@RequestMapping("/emp.do")
public class EmpAction {
@Autowired
private EmpService empS
@RequestMapping(params="p=getAll")
public String getAll(HttpServletRequest request,HttpServletResponse response){
List list = empService.getAllEmp();
request.setAttribute("list", list);
return "/WEB-INF/view/show.jsp";
@Controller指该方法是一个业务控制器;@RequestMapping("/emp.do")是指请求emp.do则核心控制器就会分发给该业务控制器去处理;@RequestMapping(params="p=getAll")是指当请求参数为p=getAll时调用业务控制器的这个方法;将"/WEB-INF/view/show.jsp"返回给核心控制器,核心控制器再转发到WEB-INF/view/show.jsp页面去显示所有员工信息。
springMVC与struts2的区别:1. 机制:spring mvc的入口是servlet,而struts2是filter,这样就导致了二者的机制不同。
2. 性能:spring会稍微比struts快。spring mvc是基于方法的设计,而sturts是基于类,每次发一次请求都会实例一个action,每个action都会被注入属性,而spring基于方法,粒度更细,但要小心把握像在servlet控制数据一样。spring3 mvc是方法级别的拦截,拦截到方法后根据参数上的注解,把request数据注入进去,在spring3 mvc中,一个方法对应一个request上下文。而struts2框架是类级别的拦截,每次来了请求就创建一个Action,然后调用setter getter方法把request中的数据注入;struts2实际上是通过setter getter方法与request打交道的;struts2中,一个Action对象对应一个request上下文。
3. 参数传递:struts是在接受参数的时候,可以用属性来接受参数,这就说明参数是让多个方法共享的。
4. 设计思想上:struts更加符合oop的编程思想, spring就比较谨慎,在servlet上扩展。
5. intercepter的实现机制:struts有以自己的interceptor机制,spring mvc用的是独立的AOP方式。这样导致struts的配置文件量还是比spring mvc大,虽然struts的配置能继承,所以我觉得论使用上来讲,spring mvc使用更加简洁,开发效率Spring MVC确实比struts2高。spring mvc是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应,所以说从架构本身上spring3 mvc就容易实现restful url。struts2是类级别的拦截,一个类对应一个request上下文;实现restful url要费劲,因为struts2 action的一个方法可以对应一个url;而其类属性却被所有方法共享,这也就无法用注解或其他方式标识其所属方法了。spring3 mvc的方法之间基本上独立的,独享request response数据,请求数据通过参数获取,处理结果通过ModelMap交回给框架方法之间不共享变量,而struts2搞的就比较乱,虽然方法之间也是独立的,但其所有Action变量是共享的,这不会影响程序运行,却给我们编码,读程序时带来麻烦。
6. 另外,spring3 mvc的验证也是一个亮点,支持JSR303,处理ajax的请求更是方便,只需一个注解@ResponseBody ,然后直接返回响应文本即可。
阅读(...) 评论()温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
年轻不是借口,坚持才是真理~~~
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
阅读(6044)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'springMVC记录系统日志的几种方式',
blogAbstract:'&&&&&&& 在项目开发中往往需要记录一些用户操作的系统日志到数据库,而不仅仅是记录在文件中或者log4j上。&&&& 第一种是最简单最原始也是最繁琐最笨的办法:即每个需要记录的操作入口方法中去调用新增日志的接口。&&&& 第二种是采用spring的拦截器进行方法拦截:&&&& 建立一个拦截器:&&&&&& &mvc:interceptor& &&& &&& && &mvc:mapping path=\"/**\"/&&&& &&& && &bean class=\"com.dw..interceptor.LogInterceptor\"&&/bean&&&& &&& &/mvc:interceptor& ',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:7,
publishTime:1,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'年轻不是借口,坚持才是真理~~~',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}SpringMVC配置和常用Controller方法 - 简书
SpringMVC配置和常用Controller方法
常用的映射处理器配置与常用Controller的方法
&!--可以显示的定义HandleMapping,以bean的name来做映射处理器--&
&bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"&&/bean&
&!--简单的HanndleMapping映射处理器--&
&bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&
&property name="mappings"&
&prop key="/hello.do"&controllerId&/prop&
&/property&
&bean class=".controller.ControllerDemo" id="controllerId" &&/bean&
&!--控制器的类名映射处理器,注意访问时类名首字母小写.do--&
&bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"&
&!--接收参数控制器,这里可以接收一个对象 --&
&bean id="commController" name="/comm.do" class=".controller.ControllerDemo"&
&property name="commanClass" value=".controller.Person"&&/property&
&!--表单控制器--&
&bean id="formCointroller" class=".controller.ControllerDemo" name="/controllerDemo2.do"&
&property name="commanClass" value=".controller.Person"&&/property&
&property name="formView" value=""&&/property&
&property name="successView" value=""&&/property&
@InitBinder
public void binder(ServletRequestDataBinder binder) {
//绑定时间类型的属性编辑器
binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
Controoler方法
*接收一个Person对象,前端页面传递过来的参数,对应Person的属性
@RequestMapping(value = "/toPerson1",method = RequestMethod.POST)
public String toPerson1(Person person) {
Map&String,Object& map = new HashMap&String,Object&();
map.put("person",map);
return "index";
* 数据和视图一块返回
* @param person
@RequestMapping(value = "/toPerson3",method = RequestMethod.POST)
public ModelAndView toPerson3(Person person) {
Map&String,Object& map = new HashMap&String, Object&();
map.put("person",person);
//相当于request.setAttribute("person",person);
//return "index";
return new ModelAndView("index",map);
* 数据和视图是分离的,这样写最好
* @param person
* @param model
@RequestMapping(value = "/toPerson4", method = RequestMethod.POST)
public String toPerson4(Person person,Model model) {
model.addAttribute("person",person);
//相当于request.setAttribute("person",person);
return "index";
* 在参数中直接定义map,可以将数据封装成model
* @param person
* @param map
@RequestMapping(value = "/toPerson5",method = RequestMethod.POST)
public String toPerson5(Person person,Map&String,Object& map) {
map.put("person",person);
return "index";
* 在参数列表中定义HttpServletResponse,用来返回ajax的结果
* @param person
* @param response
@RequestMapping(value = "/toAjax.do",method = RequestMethod.POST)
public void toAjax(Person person, HttpServletResponse response) {
response.getWriter().write(person.getName());
} catch (IOException e) {
e.printStackTrace();
* 直接在参数列表定义PrintWriter返回ajax的结果,建议使用
* @param person
* @param out
@RequestMapping(value = "/toAjax1.do",method = RequestMethod.POST)
public void toAjax1(Person person, PrintWriter out) {
out.write(person.getName());
* 内部重定向,也叫做客户端重定向,浏览器的地址栏会发生变化,不需要namespace命名空间
@RequestMapping(value = "/redirectAjax.do")
public String redirectAjax() {
return "redirect:ajax.do";
* Controller之间的重定向,必须加上命名空间,也必须加上/表示退回到项目的根路径,如果没有加上的话,则会自动追加
@RequestMapping(value = "redirectAjax1.do")
public String redirectAjax1() {
return "redirect:/text1/text1.do";SSI框架搭建
SpringMVC3.1.2+Spring3.1.2+Mybatis3.2.6
编号:SSI-SMVC3-S3-I3
版本:V1.0
级别:公开
编写时间:
1导言........................................................................................................................................1
1.1目的.............................................................................................................................1
1.2范围.............................................................................................................................1
1.3说明.............................................................................................................................12搭建SpringMVC.....................................................................................................................2
2.1搭建所需jar包...........................................................................................................2
2.2其他依赖包.................................................................................................................3
2.3搭建步骤.....................................................................................................................4
2.3.1创建项目..........................................................................................................4
2.3.2导入jar包........................................................................................................6
2.3.3配置web.xml...................................................................................................7
2.3.4配置spring-servlet.xml....................................................................................9
2.3.5配置applicationContext.xml..........................................................................10
2.3.6配置log4j.properties.....................................................................................103整合mybatis........................................................................................................................11
3.1整合所需jar包.........................................................................................................11
3.2其他依赖包...............................................................................................................11
3.3整合步骤...................................................................................................................11
3.3.1导入jar包......................................................................................................11
3.3.2配置config.properties...................................................................................12
3.3.3配置spring-dataSource.xml...........................................................................12
3.3.4配置applicationContext.xml..........................................................................15
3.3.5配置mybatis-config.xml................................................................................16
3.3.6创建实体model.............................................................................................17
3.3.7创建实例化dao.............................................................................................19
3.3.8创建业务服务service....................................................................................21
3.3.9创建控制层controller...................................................................................23
3.3.10页面代码......................................................................................................28
3.3.11启动项目......................................................................................................37
看过本文章的还看过。。。
2 范围本次框架搭建的版本是 springmvc2+spring2+mybatis6,数据 库采用的是 mysql,在 eclipse 开发工具下搭建直接搭建的 web 项目,页面采用 .........
ssm 三大框架整合详细教程 (spring+springmvc+mybatis)使用 ssm ( spring 、 springmvc 和 mybatis )已经有三个多月了,项目在技术上已经没有什么难点了 ,基于现.........
ssm框架——详细整合教程(spring+springmvc+mybatis)_互联网_it计算机_专业资料。ssm框架——详细整合教程(spring+springmvc+mybatis) .........
spring-mvc+mybatis框架集成开发流程v0_计算机软件及应用_it计算机_专业资料。spring-mvc+mybatis框架集成开发流程v0 文档贡献者 zhaoqi_qin 贡献于2014-08-........
springmvc +mybatis最全的配置_计算机软件及应用_it计算机_专业资料。springmvc+mybatis框架配置 (内含日志+多数据源配置+事务配置+maven配置) .........
springmvc+mybatis+jquery开发框架_计算机软件及应用_it计算机_专业资料。springmvc+spring3+mybatis+jquery的开发框架说明,以及规范的系统开发流程说明?.........
spring mvc 3 + mybatis 零基础搭建web开发框架_计算机软件及应用_it...xmlconfigspringmvc-servlet.xml<param-
优点:.........
最新框架整合struts3+spring1+mybatis2_计算机软件及应用_it计算机_专业资料。最新框架整合struts3+spring1+mybatis2 .........
spring + springmvc + mybatis框架搭建步骤_计算机软件及应用_it计算机_专业资料。spring + springmvc + mybatis 框架搭建步骤 新建 web 工程。 导入相关的.........
ssm 三大框架整合详细教程 (spring+springmvc+mybatis)使用 ssm ( spring 、 springmvc 和 mybatis )已经有三个多月了,项目在技术上已经没有什么难点了 ,基于现.........
springmvc+spring+mybatis框架配置详细步骤(eclipse普通版)_计算机软件及应用_it计算机_专业资料。本次框架搭建的版本是springmvc2+spring2+mybatis........
ssm框架——详细整合教程(spring+springmvc+mybatis)_互联网_it计算机_专业资料。ssm框架——详细整合教程(spring+springmvc+mybatis) .........
springmvc +mybatis最全的配置_计算机软件及应用_it计算机_专业资料。springmvc+mybatis框架配置 (内含日志+多数据源配置+事务配置+maven配置) .........
spring-mvc+mybatis框架集成开发流程v0_计算机软件及应用_it计算机_专业资料。spring-mvc+mybatis框架集成开发流程v0 文档贡献者 zhaoqi_qin 贡献于2014-08-........
2 范围本次框架搭建的版本是 springmvc2+spring2+mybatis6,数据 库采用的是 mysql,在 eclipse 开发工具下搭建直接搭建的 web 项目,页面采用 .........
springmvc+mybatis+jquery开发框架_计算机软件及应用_it计算机_专业资料。springmvc+spring3+mybatis+jquery的开发框架说明,以及规范的系统开发流程说明?.........
spring boot 集成mybatis_计算机软件及应用_it计算机_专业资料。spring boot 集成mybatis spring boot 集成 mybatis 在集成 mybatis 前,我们先配置一个 druid 数据.........
spring boot 入门 spring boot 是 spring 社区较新的一个项目。 该项目的... spring boot 集成 mybatis
spring boot 集成 druid druid 有很多个.........
spring boot application properties配置详解_计算机软件及应用_it计算机_专业资料。# spring config (configfileapplicationlistener) spring.config.name= # config .........
springmybatis整合文档_互联网_it计算机_专业资料。spring mybatis整合详解mybatis 整合 spring 开发环境: system:windows webbrowser:ie6+、firefox3+ javaee server.........
springmvc +mybatis最全的配置_计算机软件及应用_it计算机_专业资料。springmvc+mybatis框架配置 (内含日志+多数据源配置+事务配置+maven配置) .........
spring boot_电子电路_工程科技_专业资料。值得使用的 spring boot 2013 年 12 月 12 日,spring 发布了 0 版本。这个本来只是作为 java 平台上 的控制反转.........
spring boot+spring security 4 配置整合实例本例所覆盖的内容:
使用 spring security 管理用户身份认证、登录退出 用户密码加密及验证 采用数据库.........
本书以丰富的实例,介绍了如何使用spring boot开发框架进行基础应用和分布式应用等方面的开发,并且介绍了如何使用spring boot开发的应用搭建一个高性能的服务平台,同时.........
spring+mybatis 整合教程 一、添加需要的 jar 包 1 加入 spring jar 包 (注意: 在这里需要添加 springx 以上版本, spring5 不支持 mybatis 整合) 我在.........
ssm 三大框架整合详细教程 (spring+springmvc+mybatis)使用 ssm ( spring 、 springmvc 和 mybatis )已经有三个多月了,项目在技术上已经没有什么难点了 ,基于现.........
spring与mybatis三种常用整合方法_计算机软件及应用_it计算机_专业资料。spring与mybatis三种常用整合方法今日推荐 78份文档 不小心又胖了 胖女人必看 .........
ssm框架 详细整合教程(spring+springmvc+mybatis)_电子电路_工程科技_专业资料。[置顶] ssm 框架——详细整合教程 (spring+springmvc+mybatis)分类: j2ee2014-........
最新框架整合struts3+spring1+mybatis2_计算机软件及应用_it计算机_专业资料。最新框架整合struts3+spring1+mybatis2 .........
spring+springmvc+mybatis的整合_计算机软件及应用_it计算机_专业资料。ssm 框架——详细整合教程(spring+springmvc+mybatis)最近在学习 spring+springmvc+mybatis 的.........
第二章 入门 mybatis-spring 帮助了你的 mybatis 代码和 spring 进行无缝整合。使用这个类库中的类,spring 将会为你 加载必要的 mybatis 工厂和 session 类。 .........}

我要回帖

更多关于 配置spring的步骤 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信