使用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的内容如下: <?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aophttp://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"> <beanid="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <propertyname="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> <beanid="DeptDAO"class="com.dao.DeptDAO"> <propertyname="sessionFactory"> <refbean="sessionFactory"/> </property> </bean> <beanid="EmpDAO"class="com.dao.EmpDAO"> <propertyname="sessionFactory"> <refbean="sessionFactory"/> </property> </bean> <beanid="empService"class="com.service.EmpService"> <propertyname="deptDAO"ref="DeptDAO"></property> <propertyname="empDAO"ref="EmpDAO"></property> </bean> <!--事务管理器--> <beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <propertyname="sessionFactory"ref="sessionFactory"></property> </bean> <!--事务属性--> <tx:adviceid="mytx"transaction-manager="transactionManager"> <tx:attributes> <tx:methodname="*"/> </tx:attributes> </tx:advice> <!--织入--> <aop:config> <aop:advisoradvice-ref="mytx"pointcut="execution(*com.service.*.*(..))"/> </aop:config> </beans>web.xml的内容如下:
<?xmlversion="1.0"encoding="UTF-8"?> <web-appversion="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/javaeehttp://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的内容如下:
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aophttp://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-scanbase-package="com.action"></context:component-scan> </beans>EmpAction类代码如下:
packagecom.action; importjava.util.List; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.stereotype.Controller; importorg.springframework.web.bind.annotation.RequestMapping; importcom.service.EmpService; @Controller @RequestMapping("/emp.do") publicclassEmpAction{ @Autowired privateEmpServiceempService; @RequestMapping(params="p=getAll") publicStringgetAll(HttpServletRequestrequest,HttpServletResponseresponse){ Listlist=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的区别:
机制:springmvc的入口是servlet,而struts2是filter,这样就导致了二者的机制不同。
性能:spring会稍微比struts快。springmvc是基于方法的设计,而sturts是基于类,每次发一次请求都会实例一个action,每个action都会被注入属性,而spring基于方法,粒度更细,但要小心把握像在servlet控制数据一样。spring3mvc是方法级别的拦截,拦截到方法后根据参数上的注解,把request数据注入进去,在spring3mvc中,一个方法对应一个request上下文。而struts2框架是类级别的拦截,每次来了请求就创建一个Action,然后调用settergetter方法把request中的数据注入;struts2实际上是通过settergetter方法与request打交道的;struts2中,一个Action对象对应一个request上下文。
参数传递:struts是在接受参数的时候,可以用属性来接受参数,这就说明参数是让多个方法共享的。
设计思想上:struts更加符合oop的编程思想,spring就比较谨慎,在servlet上扩展。
intercepter的实现机制:struts有以自己的interceptor机制,springmvc用的是独立的AOP方式。这样导致struts的配置文件量还是比springmvc大,虽然struts的配置能继承,所以我觉得论使用上来讲,springmvc使用更加简洁,开发效率SpringMVC确实比struts2高。springmvc是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应,所以说从架构本身上spring3mvc就容易实现restfulurl。struts2是类级别的拦截,一个类对应一个request上下文;实现restfulurl要费劲,因为struts2action的一个方法可以对应一个url;而其类属性却被所有方法共享,这也就无法用注解或其他方式标识其所属方法了。spring3mvc的方法之间基本上独立的,独享requestresponse数据,请求数据通过参数获取,处理结果通过ModelMap交回给框架方法之间不共享变量,而struts2搞的就比较乱,虽然方法之间也是独立的,但其所有Action变量是共享的,这不会影响程序运行,却给我们编码,读程序时带来麻烦。
另外,spring3mvc的验证也是一个亮点,支持JSR303,处理ajax的请求更是方便,只需一个注解@ResponseBody,然后直接返回响应文本即可。
本文内容总结:
原文链接:https://www.cnblogs.com/liuling/archive/2013/02/07/sdfasdf.html