使用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的内容如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:util="http://www.springframework.org/schema/util"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:aop="http://www.springframework.org/schema/aop"
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xmlns:context="http://www.springframework.org/schema/context"
 9     xsi:schemaLocation="
10     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
11     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
12     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
13     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
14       http://www.springframework.org/schema/context
15     http://www.springframework.org/schema/context/spring-context-2.5.xsd">
16
17     <bean id="sessionFactory"
18         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
19         <property name="configLocation"
20             value="classpath:hibernate.cfg.xml">
21         </property>
22     </bean>
23
24     <bean id="DeptDAO" class="com.dao.DeptDAO">
25         <property name="sessionFactory">
26             <ref bean="sessionFactory" />
27         </property>
28     </bean>
29     <bean id="EmpDAO" class="com.dao.EmpDAO">
30         <property name="sessionFactory">
31             <ref bean="sessionFactory" />
32         </property>
33     </bean>
34     <bean id="empService" class="com.service.EmpService">
35         <property name="deptDAO" ref="DeptDAO"></property>
36         <property name="empDAO" ref="EmpDAO"></property>
37     </bean>
38
39     <!-- 事务管理器 -->
40     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
41         <property name="sessionFactory" ref="sessionFactory"></property>
42     </bean>
43     <!-- 事务属性 -->
44     <tx:advice id="mytx" transaction-manager="transactionManager">
45         <tx:attributes>
46             <tx:method name="*"/>
47         </tx:attributes>
48     </tx:advice>
49     <!-- 织入 -->
50     <aop:config>
51         <aop:advisor advice-ref="mytx" pointcut="execution(* com.service.*.*(..))"/>
52     </aop:config>
53
54 </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 ,然后直接返回响应文本即可。

时间: 2025-01-01 10:44:51

使用springMVC的详细步骤的相关文章

我的Springmvc开发详细步骤

项目开始前的一些准备 step1 新建web project step2 新建用户库,后面会使用 注意:项目里面不仅需要spring的包,还必须导入commons-logging-jar包,不然项目运行会报错 结果如下: step3 为项目添加spring容器 next 请注意,这里有一个 No jar/zip file contained in selected libraries的提醒,我还没有找到原因,但是只需要再手动的将下载的jar包导入到lib文件夹下,就可以继续后面的步骤 next

springMVC的详细步骤配置

使用springMVC也可以代替struts2,当然只是代替业务分发的功能,struts2的一些其他功能它是没有的,不然要struts2有什么用. 下面我用springMVC代替struts2去整合hibernate实现简单的员工查询功能. 使用springMVC有两个配置文件需要配置,一个是applicationContext.xml.另一个是web.xml,在 applicationContext.xml里面配置事务管理器以及属性注入等.web.xml里面要添加一个springMVC的ser

Oracle11g安装详细步骤

Oracle11g安装详细步骤,详见附件↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

VS2012/13本地发布网站详细步骤(可带数据库)

VS发布网站详细步骤 要在本地(自己的额电脑上)发布网站,首先你必须要搭建一个IIS服务器(具体搭建方法可参考我的上一篇博客).下面是具体步骤: 1.打开你的VS2012网站项目,右键点击项目>菜单中 重新生成一下网站项目:再次点击右键>发布 2.弹出网站发布设置面板,点击<新建..>,创建新的发布配置文件(配置文件可以随便写自己命名): 3.点击下一步:在发布方法中选"文件系统",这样我们可以发布到自己指定的本机文件上. 4.选择网站要发布的物理地址(可以使C

在CentOS6.4中安装配置LAMP环境的详细步骤

原文:在CentOS6.4中安装配置LAMP环境的详细步骤 本文详细介绍了CentOS6.4系统中安装LAMP服务并对其进行配置的过程,即安装Apache+PHP+Mysql,参照了网上大神的设置,其他Linux发行系统可以参考~ 在本文中部分命令操作需要root权限,输入‘su -’命令后输入密码即可切换root身份. 一.修改设置对安装做准备 1. 防火墙设置 设置/etc/sysconfig/iptables文件允许80端口和3306端口.因为80端口是http协议所使用的端口,如果防火墙

oracle 11g R2 64位 安装详细步骤

(oracle 11g R2 64位 + PLSQLDeveloper安装说明以及PLSQL Developer+ ORCALE11-instantclient-basic-win32-11.2.0.1.0连接oracle 11g R2 64位详细步骤) 第一步: 准备工具 1.oracle 11g R2 64位安装包 2.PLSQL Developer v11.0.2.1766 官方中文版(内含PLSQL Developer安装程序.汉化程序.PLSQL Developer注册程序.PLSQL

iOS 真机调试(史上最详细步骤解析,hmt精心打造)

/*************************************************************1********************************************************************/ /*************************************************************2******************************************************

FAILOVER详细步骤

FAILOVER详细步骤 1.Flush主库任何未传输的redo到目标备库 如果primary可以mount,则可以flush任何主库的未传输redo到备库,如果操作成功返回,则可以保证failover的零数据丢失. 1)确保standby数据库启用日志应用 2)mount primary数据库,执行以下SQL语句,命令等待standby应用redo完成后返回. SQL> alter system flush redo to target_db_unique_name; 如果成功返回,则转第5步

请问android使用友盟分享,分享到微信好友和朋友圈详细步骤是什么,我现在分享之后没结果。

============问题描述============ 请问android使用友盟分享,分享到微信好友和朋友圈详细步骤是什么,我现在分享之后没结果.弹出来图片大小超过32kb,然后分享中 就没反应了 ============解决方案1============ 引用 楼主 zpq19870824 的回复: 请问android使用友盟分享,分享到微信好友和朋友圈详细步骤是什么,我现在分享之后没结果.弹出来图片大小超过32kb,然后分享中 就没反应了 我也不知道,帮你顶一下吧...