当前位置:编程学习 > JAVA >>

体验Spring3 MVC,替换Struts2

Java的WEB框架中,Struts2应该是最著名的,不过最近试了试Spring3 MVC,感觉好爽啊,几乎像ASP.Net MVC3一样舒服,以后就用它了。简单记录一下过程,没有技术含量。
1、准备包
下载的是spring framework 3.2.0,从中抽取以下jar到工程的WEB-INF/lib下:
spring-beans-3.2.0.RELEASE.jar 
spring-context-3.2.0.RELEASE.jar 
spring-core-3.2.0.RELEASE.jar 
spring-expression-3.2.0.RELEASE.jar 
spring-web-3.2.0.RELEASE.jar 
spring-webmvc-3.2.0.RELEASE.jar 
另外还需要几个第三方jar包,记录日志和处理json:
commons-logging-1.1.1.jar 
jackson-core-als-1.9.11.jar 
jackson-mapper-asl-1.9.11.jar 
 
2、WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                             http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
         id="WebApp_ID" version="2.5"> 
 
    <!--站点名--> 
    <display-name>mvc</display-name> 
   
    <!--指定spring配置文件--> 
    <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>/WEB-INF/spring-servlet.xml</param-value> 
    </context-param> 
  
    <listener>      
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      
    </listener>  
   
    <servlet> 
        <!--servlet名字,随意--> 
        <servlet-name>spring</servlet-name>      
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      
        <load-on-startup>1</load-on-startup>      
    </servlet>      
 
    <servlet-mapping> 
        <!--servlet名字--> 
        <servlet-name>spring</servlet-name>    
        <!--拦截所有请求,对静态文件会有问题,在spring-servlet.xml中解决--> 
        <url-pattern>/</url-pattern>      
    </servlet-mapping>  
   
    <welcome-file-list> 
        <welcome-file>index.htm</welcome-file> 
        <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 
 
3、WEB-INF/spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:p="http://www.springframework.org/schema/p" 
 xmlns:mvc="http://www.springframework.org/schema/mvc" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context.xsd 
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
       
     <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射--> 
     <mvc:annotation-driven /> 
 
     <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean --> 
     <context:component-scan base-package="com.test.mvc.web" /> 
 
     <!-- 对模型视图名称的解析,在WEB-INF/jsp目录下找对应的jsp文件 --> 
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> 
      
    <!--放过/scripts下的静态文件--> 
    <mvc:resources mapping="/scripts/**" location="/scripts/" /> 
</beans> 
 
4、WEB-INF/applicationContext.xml
spring的配置文件,由于我们不使用它的其它功能,暂时放个空的就好了。
<?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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
            http://www.springframework.org/schema/util 
            http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 
</beans> 
 
5、写Controller
package com.test.mvc.web; 
 
import org.springframework.stereotype.*; 
import org.springframework.web.bind.annotation.*; 
import org.springframework.web.servlet.*; 
 
/** 
 * 控制器,用Controller注解 
 */ 
@Controller 
public class HomeController { 
 
    /** 
     * 映射到/welcome 
     */ 
    @RequestMapping(value = "/welcome") 
    public ModelAndView welcome(){ 
         
  &n
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,