使用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 ,然后直接返回響應文本即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM