Spring MVC XML 的配置

JavaEE体系结构包括四层,从上到下分别是应用层、Web层、业务层、持久层。Struts和这篇文章中讲解的SpringMVC是Web层的框架,Spring是业务层的框架,之前文章中讲解的Hibernate和MyBatis是持久层的框架。

SpringMVC是一种基于Java,实现了Web MVC(模型 - 视图 - 控制器)设计模式,请求驱动类型的轻量级的开源的Web框架,即用了MVC架构模式的思想,将Web层进行职责解耦。基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发,SpringMVC也是要简化我们日常Web开发。

模型(Model)封装了应用程序数据,通常它们将由POJO类组成。
视图(View)负责渲染模型数据,一般来说它生成客户端浏览器可以解释HTML输出。
控制器(Controller)负责处理用户请求并构建适当的模型,并将其传递给视图进行渲染。
Spring Web MVC 框架是围绕 DispatcherServlet 设计的,它将请求分派给处理程序,具有可配置的处理程序映射,视图解析,区域设置,本地化和主题解析,并且支持上传文件。默认的处理是基于注解 @Controller 和 @RequestMapping,提供一系列灵活的处理方法。随着 Spring 3.0的推出,通过 @PathVariable 或者其他注解,@Controller 机制开始允许你去创建 Rest 风格的 web 站点和应用。

其中 Spring Web MVC 和 Spring 有一条关键的准则是“对扩展开放,对修改关闭”。在Spring Web MVC中一些核心类的方法被标注为final,由于开发者不能用自已的方法去覆盖这些方法,这并不是任意的,而是特别考虑到这个原则

原文参考链接:https://blog.csdn.net/u014010512/article/details/82767352

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"  
   xmlns:context="http://www.springframework.org/schema/context"  
   xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd  
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">  
   <!-- 配置扫描包 -->  
   <context:component-scan base-package="com.springmvc.web"></context:component-scan>  
   <!--Spring视图解析器 -->  
   <bean id="internalResourceViewResolver"  
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
      <property name="prefix" value="/WEB-INF/view/" />  
      <property name="suffix" value=".jsp" />  
   </bean>  
   <!-- 配置注解驱动 -->  
   <mvc:annotation-driven conversion-service="conversionService"  />  
   <!-- 配置文件上传解析器 -->  
 <bean id="multipartResolver"  
       class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
      <property name="defaultEncoding" value="UTF-8"></property>  
      <property name="maxInMemorySize" value="102400000"></property>  
   </bean>  
   <!-- 配置转换器工厂 -->  
   <bean id="conversionService"  
      class="org.springframework.context.support.ConversionServiceFactoryBean">  
      <property name="converters">  
         <set>  
            <bean class="com.springmvc.converte.DateConvert">  
                <constructor-arg>  
                   <list>  
                      <value>yyyy/MM/dd</value>  
                   </list>  
                </constructor-arg>  
            </bean>  
         </set>  
      </property>  
   </bean>  
   <!-- 配置默认处理的Servlet -->  
   <mvc:default-servlet-handler />  
   <!-- 配置国际化资源文件 -->  
   <bean id="messageSource"  
      class="org.springframework.context.support.ResourceBundleMessageSource">  
      <property name="basename" value="i18n"></property>  
      <property name="defaultEncoding" value="UTF-8"></property>  
   </bean>  
   <!-- 配置 CookieLocaleResolver -->  
   <bean id="localeResolver"  
         class="org.springframework.web.servlet.i18n.CookieLocaleResolver">  
      <property name="cookieName" value="locale"></property>  
   </bean>  
   <!-- 拦截器配置 -->  
   <mvc:interceptors>  
      <!-- 配置 LocaleChanceInterceptor -->  
      <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>  
   </mvc:interceptors>  
   <!-- 异常处理配置 -->  
   <bean id="simpleMappingExceptionResolver"    
   
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
      <property name="exceptionMappings">  
         <props>  
           <prop  
              key="org.springframework.web.servlet.NoHandlerFoundException">error  
           </prop>  
         </props>  
      </property>  
   </bean>  
</beans>  

原文参考链接:https://liguanfeng.iteye.com/blog/2172744 

视图解析器我和原文章的不一样,我用的是默认视图解析器

 <!-- 默认的视图解析器- -->
    <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

 也多了json数据的处理

<!-- json数据处理配置 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="false">
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

 总结:在网上看了大量的配置Spring MVC XML的配置,其中扫描包和视图解析器是必配的,其他的配置可以根据需要来进行配置

猜你喜欢

转载自www.cnblogs.com/lindaiyu/p/10925299.html