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> <bean id="DeptDAO" class="com.dao.DeptDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="EmpDAO" class="com.dao.EmpDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="empService" class="com.service.EmpService">
<property name="deptDAO" ref="DeptDAO"></property>
<property name="empDAO" ref="EmpDAO"></property>
</bean> <!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务属性 -->
<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> </beans>
web.xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/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>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<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>
<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>
</beans>
EmpAction类代码如下:
package com.action; import java.util.List; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import com.service.EmpService; @Controller
@RequestMapping("/emp.do")
public class EmpAction {
@Autowired
private EmpService empService; @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 ,然后直接返回响应文本即可。
来源:http://www.cnblogs.com/liuling/archive/2013/02/07/sdfasdf.html
参考:http://blog.csdn.net/zuxianghaung/article/details/6649269
springMVC的详细步骤配置的更多相关文章
- 配置Jenkins的slave节点的详细步骤适合windows等其他平台(转)
@ 新建一个slave节点在Jenkins服务器上 1,进入Jenkins的主界面,进入“Manage Jenkins” 页面: 2,点击如下图中的“Manage Nodes”: 3,进入页面后点 ...
- 配置WebLogic的详细步骤
配置WebLogic的详细步骤 1.安装好WebLogic后,进入配置阶段,点击"Getting started with WebLogic Server 10.3.6" 2.进入 ...
- Windows Server 2016 配置 IIS 的详细步骤
Ø 简介 本文主要记录 Windows Server 2016 环境下,安装配置 IIS 的详细步骤.需要说明的是,在选择"功能"或"角色服务"时不建议将所有 ...
- 配置Jenkins的slave节点的详细步骤适合windows等其他平台
@ 新建一个slave节点在Jenkins服务器上 1,进入Jenkins的主界面,进入"Manage Jenkins" 页面: 2,点击如下图中的"Manage N ...
- 在linux环境下为eclipse配置jdk以及Tomcat服务(附图解详细步骤)
环境:jdk8,Tomcat7,eclipse 需要先在linux上安装好对应的软件及java环境,如果还没有安装的,可以先去看我之前写的两篇博客:ubuntu/linux中安装Tomcat(附图解详 ...
- linux下vsftpd的安装及配置使用详细步骤(推荐)
vsftpd 是“very secure FTP daemon”的缩写,安全性是它的一个最大的特点. vsftpd 是一个 UNIX 类操作系统上运行的服务器的名字,它可以运行在诸如 Linux.BS ...
- OPGL+GLFW+GLEW配置详细步骤
转载自:https://blog.csdn.net/weixin_40921421/article/details/80211813 本文设计的工具包: 链接:https://pan.baidu.co ...
- OPGL+VS2017+GLFW+GLEW配置详细步骤
OPGL+VS2017+GLFW+GLEW配置详细步骤: https://blog.csdn.net/weixin_40921421/article/details/80211813 原博客地址:ht ...
- VS2017配置opencv-4.2.0详细步骤
VS2017配置opencv-4.2.0详细步骤 1.下载opencv的安装包并解压.下载网址https://sourceforge.net/projects/opencvlibrary/ 图1 ...
随机推荐
- python列表推导式详解
推导式是Python中很强大的.很受欢迎的特性,具有语言简洁,简化代码,速度快等优点.推导式包括:1.列表推导式2.字典推导式3.集合推导式4.嵌套列表推导式注意: 字典和集合推导是最近才加入到Pyt ...
- [HDOJ5510]Bazinga(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 普通集合会tle,换高贵的并查集. #include <algorithm> #in ...
- core--线程池
对于服务器-客户端这种架构的软件,通常客户端的数据来自于服务器,如何让一个服务器进程,来满足多个客户端程序的数据请求?一种简单的方法就是,每当一个客户请求来领,服务器就为该客户端创建一个线程.当有10 ...
- Caused by: 元素类型为 "package" 的内容必须匹配 "(result-types?,interceptors?,default-interceptor-ref?,default-action-ref?,default-class-ref?,global-results?,global-exception-mappings?,action*)"
Caused by: 元素类型为 "package" 的内容必须匹配 "(result-types?,interceptors?,default-interceptor- ...
- volley(4) 请求参数:data:[ { bar_remain:XX , bar_code:"XX" , bar_id: XX}], method:GET
1. 来自于WHCombineBatchFragment.java 2.部分代码 ).).).).port + Url.LABELPRINT + "?data="+strPrint ...
- appserver配置虚拟主机
1, apach配置文件开启虚拟主机服务(C:\AppServ\Apache2.2\conf\httpd.conf)大概561行的位置 # Virtual hostsInclude conf/extr ...
- 每天一个Linux命令(3): cd
Linux cd 命令可以说是Linux中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用 cd 命令上的. 所以,学习Linux 常用命令,首先就要学好 cd 命令的使用方法技巧. 1. ...
- php的setcookie
不同浏览器对cookie的原理不同,导致cookie的过期时间有些模糊. 经测试:火狐浏览器的cookie过期时间设置是根据增量原则.服务器端设置time()+num,或者time()-num,传递到 ...
- SkinPP for VC
1.下载文件:SkinPPWTL.h,SkinPPWTL.dll,SkinPPWTL.lib以及Skin++皮肤库: 2.新建一个工程,如:基于多文档的工程,名为:MySkin: 3.将下载的Skin ...
- vector & array
private static const NUM_LOOPS:int = 15; public function VectorTest():void { var vector:Vector.<i ...