zoukankan      html  css  js  c++  java
  • Spring3.X 配置Spring MVC 配置

    导论:

    什么是Spring MVC?

    Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。

    Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还是 Struts 这样的 Web 框架。通过策略接口,Spring 框架是高度可配置的,而且包含多种视图技术,例如  JavaServer Pages(JSP)技术、Velocity、Tiles、iText 和 POI。Spring MVC 框架并不知道使用的视图,所以不会强迫您只使用 JSP 技术。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。

    Spring MVC 框架的优点:

    Lifecycle for overriding binding, validation, etc.;易于同其它View框架(Titles等)无缝集成,采用IOC便于测试。

     

      它是一个典型的教科书式的mvc构架,而不像struts等都是变种或者不是完全基于mvc系统的框架,对于初学者或者想了解mvc的人来说我觉得 spring是最好的,它的实现就是教科书!第二它和tapestry一样是一个纯正的servlet系统,这也是它和tapestry相比 struts所没有的优势。而且框架本身有代码,而且看起来也不费劲比较简单可以理解。

     

     

            本文笔者为一等一的菜鸟,也是刚刚学习Spring MVC 框架,自己通过学习别人的经验搭建起来的Spring MVC的框架,写下来供大家学习和分享,水平太差,如有错误,希望大家指出,纠正自己,也是帮助别人。本文适合刚刚学习的人阅读,大牛们就不要笑话了。

     

    1.环境说明:

    MyEclipse for Spring 10.0  官网下载地址:http://www.myeclipseide.com/(有时候打不开) 那就去电驴上找找吧

    jdk1.7.0_03 下载 http://www.oracle.com/technetwork/java/javase/downloads/index.html

    apache-tomcat-7.0.23 下载 http://tomcat.apache.org/

     

    2.jar 包说明:

    我想搭建的是 Spring +Jpa+Spring MVC,所有将其支持的jar都加入进去了。

    红框为Spring MVC 支持的jar包,还需要Spring core 

    加入方法多样,MyEclipse for Spring 中 右键项目名称——》MyEclipse -》可以加入spring  JPA  Spring web 等支持的jar包。

     

    3.配置基于注解的Spring MVC

    web.xml 配置:

     

    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app version="2.5"   
    3.     xmlns="http://java.sun.com/xml/ns/javaee"   
    4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
    7.   <display-name>base</display-name>  
    8.   <!-- 配置spring配置文件 -->  
    9.   <context-param>  
    10.     <param-name>contextConfigLocation</param-name>  
    11.     <param-value>/WEB-INF/classes/resources/spring/spring-*.xml</param-value>  
    12.   </context-param>  
    13.     <!-- 配置 springMVC-->    
    14.      <servlet>  
    15.         <servlet-name>springmvc</servlet-name>  
    16.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    17.         <init-param>  
    18.           <param-name>contextConfigLocation</param-name>  
    19.           <param-value>/WEB-INF/classes/resources/spring/spring-mvc-servlet.xml</param-value>  
    20.         </init-param>  
    21.         <load-on-startup>1</load-on-startup>  
    22.   </servlet>  
    23.    <servlet-mapping>  
    24.     <servlet-name>springmvc</servlet-name>  
    25.     <url-pattern>/</url-pattern>  
    26.   </servlet-mapping>  
    27.   <!-- end 配置spring MVC -->  
    28.   <welcome-file-list>  
    29.     <welcome-file>index.jsp</welcome-file>  
    30.   </welcome-file-list>  
    31. </web-app>  
    <?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">
      <display-name>base</display-name>
      <!-- 配置spring配置文件 -->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/resources/spring/spring-*.xml</param-value>
      </context-param>
      	<!-- 配置 springMVC-->	
    	 <servlet>
    	    <servlet-name>springmvc</servlet-name>
    	    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    	    <init-param>
    	      <param-name>contextConfigLocation</param-name>
    	      <param-value>/WEB-INF/classes/resources/spring/spring-mvc-servlet.xml</param-value>
    	    </init-param>
    	    <load-on-startup>1</load-on-startup>
      </servlet>
       <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
      <!-- end 配置spring MVC -->
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    

     

    项目的所有关于spring 的配置都放在class/resources/spring/ 下面。且都已spring-*开头,所以,当初始化的时候加载所有spring的配置。所有的"/" mapping加入拦截。

    关于springmvc的配置名称为spring-mvc-servlet.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" xmlns:p="http://www.springframework.org/schema/p"  
    4.     xmlns:context="http://www.springframework.org/schema/context"  
    5.     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"  
    6.     xmlns:tx="http://www.springframework.org/schema/tx"  
    7.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    9.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
    10.         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd  
    11.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"  
    12.     default-autowire="byName">  
    13.     <mvc:annotation-driven />  
    14.       
    15.     <!-- start开启注释 -->  
    16.     <context:component-scan base-package="com.base.web">  
    17.         <context:include-filter type="annotation"  
    18.             expression="org.springframework.stereotype.Controller" />  
    19.         <context:exclude-filter type="annotation"  
    20.             expression="org.springframework.stereotype.Service" />  
    21.     </context:component-scan>  
    22.     <!-- end开始注释 -->  
    23.       
    24.     <!-- 全局异常配置 start -->  
    25.     <bean id="exceptionResolver"  
    26.         class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
    27.         <property name="exceptionMappings">  
    28.             <props>  
    29.                 <prop key="java.lang.Exception">/errors/error</prop>  
    30.                 <prop key="java.lang.Throwable">/errors/err</prop>  
    31.             </props>  
    32.         </property>  
    33.         <property name="statusCodes">  
    34.             <props>  
    35.                 <prop key="errors/error">500</prop>  
    36.                 <prop key="errors/404">404</prop>  
    37.             </props>  
    38.         </property>  
    39.         <!-- 设置日志输出级别,不定义则默认不输出警告等错误日志信息 -->  
    40.         <property name="warnLogCategory" value="WARN"></property>  
    41.         <!-- 默认错误页面,当找不到上面mappings中指定的异常对应视图时,使用本默认配置 -->  
    42.         <property name="defaultErrorView" value="errors/error"></property>  
    43.         <!-- 默认HTTP状态码 -->  
    44.         <property name="defaultStatusCode" value="500"></property>  
    45.     </bean>  
    46.     <!-- 全局异常配置 end -->  
    47.       
    48.     <!-- start视图配置 -->  
    49.     <bean  
    50.         class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
    51.         p:prefix="/WEB-INF/jsps/" p:suffix=".jsp" />  
    52.     <!-- end视图配置 -->  
    53. </beans>  
    <?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:p="http://www.springframework.org/schema/p"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	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-3.0.xsd
    		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
    	default-autowire="byName">
    	<mvc:annotation-driven />
    	
    	<!-- start开启注释 -->
    	<context:component-scan base-package="com.base.web">
    		<context:include-filter type="annotation"
    			expression="org.springframework.stereotype.Controller" />
    		<context:exclude-filter type="annotation"
    			expression="org.springframework.stereotype.Service" />
    	</context:component-scan>
    	<!-- end开始注释 -->
    	
    	<!-- 全局异常配置 start -->
    	<bean id="exceptionResolver"
    		class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    		<property name="exceptionMappings">
    			<props>
    				<prop key="java.lang.Exception">/errors/error</prop>
    				<prop key="java.lang.Throwable">/errors/err</prop>
    			</props>
    		</property>
    		<property name="statusCodes">
    			<props>
    				<prop key="errors/error">500</prop>
    				<prop key="errors/404">404</prop>
    			</props>
    		</property>
    		<!-- 设置日志输出级别,不定义则默认不输出警告等错误日志信息 -->
    		<property name="warnLogCategory" value="WARN"></property>
    		<!-- 默认错误页面,当找不到上面mappings中指定的异常对应视图时,使用本默认配置 -->
    		<property name="defaultErrorView" value="errors/error"></property>
    		<!-- 默认HTTP状态码 -->
    		<property name="defaultStatusCode" value="500"></property>
    	</bean>
    	<!-- 全局异常配置 end -->
    	
    	<!-- start视图配置 -->
    	<bean
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    		p:prefix="/WEB-INF/jsps/" p:suffix=".jsp" />
    	<!-- end视图配置 -->
    </beans>
    

     

    所有com.base.web下面的controller 基于注释处理。

    /WEB-INF/jsps/   下面的jsp页面为视图页面,加入拦截。

    4.测试Spring MVC  是否配置成功

    建一个测试页面。

    一定在com.base.web 下面建议一个测试的类。

     

    1. <span style="font-size: 16px;">package com.base.web.test.controller;  
    2.   
    3. import org.springframework.stereotype.Controller;  
    4. import org.springframework.web.bind.annotation.RequestMapping;  
    5. import org.springframework.web.bind.annotation.RequestMethod;  
    6. import org.springframework.web.servlet.ModelAndView;  
    7.   
    8. /** 
    9.  *对springMVC 配置测试 
    10.  * xlyu基础代码 
    11.  * 类描述: 
    12.  * @author YuXiaolin 
    13.  * 下午1:41:46 
    14.  */  
    15.   
    16. //加入控制类注释    
    17. @Controller  
    18. public class TestController   
    19. {  
    20.     /** 
    21.      * 测试Spring MVC 
    22.      * @return 
    23.      */  
    24.     @RequestMapping(value = "/spring/test", method = RequestMethod.GET)  
    25.     public  ModelAndView testSpring()   
    26.     {  
    27.         return new ModelAndView ("/test/base_test");  
    28.     }  
    29. }  
    30. </span>  
    package com.base.web.test.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
    
    /**
     *对springMVC 配置测试
     * xlyu基础代码
     * 类描述:
     * @author YuXiaolin
     * 下午1:41:46
     */
    
    //加入控制类注释  
    @Controller
    public class TestController 
    {
    	/**
    	 * 测试Spring MVC
    	 * @return
    	 */
    	@RequestMapping(value = "/spring/test", method = RequestMethod.GET)
    	public  ModelAndView testSpring() 
    	{
    		return new ModelAndView ("/test/base_test");
    	}
    }
    

     

    测试成功,这个测试没有带数据过来,可以建立带数据过来测试。

  • 相关阅读:
    Supervisor安装与使用
    windows常用快捷键和指令
    搜索引擎使用技巧
    golang核心Goroutine和channel
    4、小程序原生底部菜单
    三、小程序值使用vant开发
    axios请求2
    3、小程序消息推送
    居中
    一、底部菜单
  • 原文地址:https://www.cnblogs.com/happy366day/p/3876293.html
Copyright © 2011-2022 走看看