本人微信公众号"aeolian"~

Spring MVC Web.xml配置

Web.xml

spring&spring mvc

在web.xml中定义contextConfigLocation参数,Spring会使用这个参数去加载所有逗号分隔的xml文件,如果没有这个参数,Spring默认加载web-inf/applicationContext.xml文件。

    
    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>/WEB-INF/springConfig.xmlparam-value>
    context-param>

    
    
    <servlet>
        <servlet-name>springmvcservletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        
        <init-param>
             <param-name>contextConfigLocationparam-name>
             <param-value>/WEB-INF/springmvc.xmlparam-value>
        init-param>
        
        <init-param>
             <param-name>appNameparam-name>
             <param-value>authplatformparam-value>
        init-param>
        
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservletservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>

    
    <filter>
        <filter-name>charsetfilterfilter-name>
        <filter-class>
            org.springframework.web.filter.CharacterEncodingFilter
        filter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
        <init-param>
            <param-name>forceEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>charsetfilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

    
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        listener-class>
    listener>

    
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>
    listener>

proxool连接池

    
    <servlet>
        <servlet-name>proxoolServletConfiguratorservlet-name>
        <servlet-class>
            org.logicalcobwebs.proxool.configuration.ServletConfigurator
        servlet-class>
        <init-param>
            <param-name>xmlFileparam-name>
            <param-value>WEB-INF/classes/jdbcproxool.xmlparam-value>
        init-param>
        <load-on-startup>2load-on-startup>
    servlet>

webAppRootKey

<context-param>
    <param-name>webAppRootKeyparam-name>
    <param-value>webapp.rootparam-value>
context-param>

“webapp.root”这个字符串可以随便写任何字符串。如果不配置默认值是”webapp.root”。
可以用System.getProperty(“webapp.root”)来动态获项目的运行路径。一般返回结果例如:/usr/local/tomcat6/webapps/项目名。

log4j

    
    <context-param>
        <param-name>log4jConfigLocationparam-name>
        <param-value>classpath:log4j.propertiesparam-value>
    context-param>
<listener> <listener-class> org.springframework.web.util.Log4jConfigListener listener-class> listener>

资源文件

spring里面有替代标签resource

    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.jpgurl-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.htmurl-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.htmlurl-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.jpegurl-pattern>
    servlet-mapping>

    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.pngurl-pattern>
    servlet-mapping>
    
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.icourl-pattern>
    servlet-mapping>

    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.gifurl-pattern>
    servlet-mapping>

    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.jsurl-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.jsonurl-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.cssurl-pattern>
    servlet-mapping>

    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.bmpurl-pattern>
    servlet-mapping>

    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.swfurl-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.xmlurl-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.docxurl-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.docurl-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.xlsurl-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.xlsxurl-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.rarurl-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.zipurl-pattern>
    servlet-mapping>

SpringMVC.xml

springmvc的配置文件

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:util="http://www.springframework.org/schema/util"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/util
                            http://www.springframework.org/schema/util/spring-util-3.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-3.1.xsd
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
                            
    
    <context:component-scan base-package="com.gmtx">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    context:component-scan>
        
    
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
         
        <property name="defaultErrorView" value="errorpages/500"/>
        <property name="defaultStatusCode" value="500"/>
        <property name="exceptionAttribute" value="ex"/>
        <property name="warnLogCategory" value="WARN"/>
        
        <property name="exceptionMappings">
           <props>
              
              <prop key="java.lang.RunTimeException">errorpages/500prop> 
              <prop key="java.sql.SQLException">errorpages/500prop>
              <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">errorpages/upLoadFileErrorprop>  
           props>
        property>
        
        <property name="statusCodes">
           <props>
                 
                 <prop key="errorpages/404">404prop>
                 <prop key="errorpages/404">400prop>
           props>
        property>
    bean>
   
    
      
     <mvc:resources mapping="/images/**" location="/WEB-INF/asset/images/" />
     <mvc:resources mapping="/css/**" location="/WEB-INF/asset/css/" />
     <mvc:resources mapping="/js/**" location="/WEB-INF/asset/js/" />
     <mvc:default-servlet-handler />
    
    
    <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    bean>
    
    
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"/>
        <property name="maxUploadSize" value="52428800"/>
        <property name="maxInMemorySize" value="50000"/>
    bean>
    
   
   <mvc:annotation-driven validator="validation" conversion-service="myConversionService"/>
   
    
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="defaultEncoding" value="utf-8"/>
        <property name="basename" value="classpath:msg"/>
        <property name="useCodeAsDefaultMessage" value="false"/>
        <property name="cacheSeconds" value="200"/>
    bean>
    
    
    <bean id="validation" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
        <property name="validationMessageSource" ref="messageSource"/>
    bean>
    
    
    <bean id="myConversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">   
         <property name="converters">
             <list>
                 <bean class="com.gmtx.system.convertre.DateConverter"/>
                 <bean class="com.gmtx.system.convertre.NumberConverter"/>
                 <bean class="com.gmtx.system.convertre.BooleanConverter"/>
                 <bean class="com.gmtx.system.convertre.IntegerConverter"/>
                 <bean class="com.gmtx.system.convertre.FloatConverter"/>
                 <bean class="com.gmtx.system.convertre.DoubleConverter"/>
                 <bean class="com.gmtx.system.convertre.LongConverter"/>
                 <bean class="com.gmtx.system.convertre.ShortConverter"/>
                 <bean class="com.gmtx.system.convertre.CharConverter"/>
                 <bean class="com.gmtx.system.convertre.StringConvert"/>
             list>
         property>
     bean>
    
    
    <mvc:view-controller path="/" view-name="forward:/login">mvc:view-controller>
    
     
     <mvc:interceptors>
         <mvc:interceptor>
             <mvc:mapping path="/*/*"/>
             <bean class="实现org.springframework.web.servlet.HandlerInterceptor的类(或其他拦截类)"/>
         mvc:interceptor>
     mvc:interceptors>
beans>

异常友好展示

参数处理

SpingConfig.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:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
                        http://www.springframework.org/schema/task
                        http://www.springframework.org/schema/task/spring-task-3.1.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
    
    <context:component-scan base-package="com.gmtx">
    
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>
    
    <context:annotation-config/>
    
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.logicalcobwebs.proxool.ProxoolDriver"/>
        <property name="url" value="proxool.hjzzAuthPlatform"/>
    bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    bean>
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource">property>
    bean>
    
    
    <aop:aspectj-autoproxy expose-proxy="true"/>
  
    
    <tx:annotation-driven transaction-manager="txManager"/>
    
    
    <task:annotation-driven/>
beans>

 

点赞

Leave a Reply

Your email address will not be published. Required fields are marked *