关于常用的web.xml、applicationContext.xml与springMVC-servlet.xml

一、关于web.xml配置

这个没啥好说的,web启动需要加载的文件都扔这里。

<?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"

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">
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
    </welcome-file-list>

    <!-- Spring MVC配置 -->
    <!-- ====================================== -->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如springMVC-servlet.xml-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/springMVC-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>

    <!-- Spring配置 -->
    <!-- ====================================== -->
    <listener>
        <listenerclass>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <!-- 指定Spring Bean以及数据源datasource的配置文件所在目录。默认配置在WEB-INF目录下 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:config/applicationContext.xml</param-value>
    </context-param>

    <!-- ====================================== -->
    <!--加载其它属性文件-->
    <context-param>
        <param-name>config.file</param-name>
        <param-value>/config.properties</param-value>
    </context-param>

    <!--配置字符编码过滤器-->
    <filter>
        <filter-name>encoding-filter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding-filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>        

二、关于springMVC-servlet.xml配置

  springMVC-servlet这个名字是因为上面web.xml中<servlet-name>标签配的值为springMVC(<servlet-name>springMVC</servlet-name>),再加上“-servlet”后缀而形成的springMVC-servlet.xml文件名,如果改为spring,对应的文件名则为spring-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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="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-4.0.xsd">

    <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器-->
    <context:component-scan base-package="com" />

    <!--mvc:message-converters消息转换器,它主要处理的是response返回的值,比如默认编码,比如支持Fastjson,当然你可以可以加入对其他返回类型的支持,比如gson,protobuf等等-->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <!-- @ResponseBody乱码问题,将StringHttpMessageConverter的默认编码设为UTF-8 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8" />
            </bean>
            <!-- 配置Fastjson支持 -->
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="charset" value="UTF-8" />
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json</value>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
                <property name="features">
                    <list>
                        <value>WriteMapNullValue</value>
                        <value>QuoteFieldNames</value>
                        <value>WriteDateUseDateFormat</value>
                        <value>WriteEnumUsingToString</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

   <!--对静态资源不拦截的处理-->
    <mvc:resources mapping="/static/**" location="classpath:/resources/static/**" />

    <!--JSP视图解析器-->
    <bean id="viewResolverJsp" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
        <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView"/>
        <property name="order" value="1"/>
    </bean>

    <!-- 配置freeMarker视图解析器 -->
    <bean id="viewResolverFtl" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
        <property name="contentType" value="text/html; charset=UTF-8"/>
        <property name="exposeRequestAttributes" value="true" />
        <property name="exposeSessionAttributes" value="true" />
        <property name="exposeSpringMacroHelpers" value="true" />
        <property name="requestContextAttribute" value="request" />

        <property name="cache" value="true" />

        <property name="suffix" value=".ftl" />
        <property name="order" value="0"/>
    </bean>

    <!-- 配置freeMarker的模板路径 -->
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
        <property name="freemarkerVariables">
            <map>
                <entry key="xml_escape" value-ref="fmXmlEscape" />
            </map>
        </property>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="freemarkerSettings">
            <props>
                <prop key="template_update_delay">3600</prop>
                <prop key="locale">zh_CN</prop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                <prop key="date_format">yyyy-MM-dd</prop>
                <prop key="number_format">#.##</prop>
            </props>
        </property>
    </bean>
    <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>

    <!-- 配置文件上传解析器,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 默认编码 -->
        <property name="defaultEncoding" value="utf-8" />
        <!-- 文件大小最大值 -->
        <property name="maxUploadSize" value="10485760000" />
        <!-- 内存中的最大值 -->
        <property name="maxInMemorySize" value="40960" />
    </bean>

    <!--统一处理异常的解析器SimpleMappingExceptionResolver-->
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="warnLogCategory" value="warn" />
        <property name="defaultStatusCode" value="500"/>
        <property name="defaultErrorView" value="/error/500"/>

        <property name="exceptionMappings">
            <props>
                <prop key="java.lang.Exception">/error/500</prop>
            </props>
        </property>

        <property name="statusCodes">
            <props>
                <prop key="/error/404">404</prop>
                <prop key="/error/404">400</prop>
                <prop key="/error/500">500</prop>
            </props>
        </property>
    </bean>

    <!--springmvc拦截器mvc:interceptors-->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/static/**" />
            <bean class="com.xxx.interceptor.LoginRequiredInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>

</beans>

二、关于applicationContext.xml配置

  spring的核心配置文件,也需要在web.xml加载。

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"   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-3.0.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!--加载属性数据库文件-->
    <context:property-placeholder location="classpath:resource/db.properties" />

    <!--C3P0连接池:使用:com.mchange.v2.c3p0.ComboPooledDataSource进行配置-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">

        <!-- 【必须】  数据库驱动-->
        <property name="driverClassName" value="${jdbc.driver}" />

        <!-- 【必须】 数据库连接地址 -->
        <property name="url" value="${jdbc.url}" />

        <!-- 【必须】 数据库用户名 -->
        <property name="username" value="${jdbc.username}" />

        <!-- 【必须】 数据库密码 -->
        <property name="password" value="${jdbc.password}" />

        <!-- 可选 启动时创建的连接数 -->
        <property name="initialSize" value="5"/>

        <!-- 可选 同时可从池中分配的最多连接数,0无限制 -->
        <property name="maxActive" value="10"/>

        <!-- 可选 池中不会被释放的最多空闲连接数 0无限制 -->
        <property name="maxIdle" value=""/>

        <!-- 可选 同时能从语句池中分配的预处理语句最大值,0无限制 -->
        <property name="maxOpenPreparedStatement" value="100"/>

        <!-- 可选 抛异常前池等待连接回收最大时间(当无可用连接),-1无限等待 -->
        <property name="maxWait" value="1000"/>

        <!-- 可选 连接在池中保持空闲而不被回收的最大时间 -->
        <property name="minEvictableIdleTimeMillis" value="2000"/>

        <!-- 可选 不创建新连接情况下池中保持空闲的最小连接数 -->
        <property name="minIdle" value="2"/>

        <!-- 可选 布尔值,是否对预处理语句进行池管理 -->
        <property name="poolPreparedStatements" value="true"/>
    </bean>

    <!--和hibernate的整合-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!-- 与dataSource -->
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
    </bean>

    <!-- 和mybatis的整合 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 加载mybatis的全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
    </bean>

    <!-- 事务管理器 -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 配置通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 传播行为 -->
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!-- 切面 -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice"
                     pointcut="execution(* com.store.service.*.*(..))" />
    </aop:config>

    <!--关于配置文件中的service层的配置。 扫描包下面所有的service层。-->
    <context:component-scan base-package="com.xxx.service"/>

</beans>

原文地址:https://www.cnblogs.com/Luwak90/p/9404592.html

时间: 2024-07-28 20:23:15

关于常用的web.xml、applicationContext.xml与springMVC-servlet.xml的相关文章

web.xml之context-param,listener,filter,servlet加载顺序及其周边

先以加载spring为例子看看加载顺序的作用: Spring加载可以利用ServletContextListener 实现,也可以采用load-on-startup Servlet 实现,但比如filter 需要用到 bean ,但加载顺序是: 先加载filter 后加载spring,则filter中初始化操作中的bean为null:所以,如果过滤器中要使用到 bean,此时就可以根据加载顺序listener>filter>servlet,将spring 的加载 改成 Listener的方式.

web.xml 之contextParam,listener,filter,servlet的加载顺序

先以加载spring为例子看看加载顺序的作用: Spring加载可以利用ServletContextListener 实现,也可以采用load-on-startup Servlet 实现,但比如filter 需要用到 bean ,但加载顺序是: 先加载filter 后加载spring,则filter中初始化操作中的bean为null:所以,如果过滤器中要使用到 bean,此时就可以根据加载顺序listener>filter>servlet,将spring 的加载 改成 Listener的方式.

如何在web.xml文件中引入其他的xml文件

最近在做一个Servlet+javaBean的项目,服务器用的是tomcat.因此,所有的页面都是servlet请求,而且很多,需要在web.xml文件中进行配置.导致web.xml文件特别大,而且这个系统以后会做大,并且会出现系统拆分,为了便于以后拆分,于是想到将web.xml文件中的servlet和servlet-mapping能够从web.xml脱离出来,用其他xml文件保存然后在web.xml文件中引入这些文件,就想类似引入struts.config一样.        在网上找了半天也

【Servlet】常用技术web

Servlet的概述 什么是Servlet?: Servlet是在服务器端的一个小的java程序,接收和相应从客户端发送的请求.Servlet的作用: 处理来自客户端的请求,并且对请求做出相应的响应.使用Servlet :(Servlet的简单案例:即入门) * 编写一个类实现Servlet的接口. public class servletTest01 implements Servlet{ @Override public void service(ServletRequest request

疯狂XML学习笔记(10)---------XML的作用

很长时间都没有整理XML的知识了,是时候好好的整理一下了,一方面,老师快讲完课了,自己该复习一下了,整理一下思路,学一遍不能白学呀,另一方面,希望能够将XML的知识彻底的掌握.下面开始了 总结一下XML主要有哪些用途吧,也是为激励一下自己更好地掌握 XML.其实XML的作用还是蛮多的! 之前总结xml知识的网址http://blog.csdn.net/column/details/studyxml.html XML 应用于 web 开发的许多方面,常用于简化数据的存储和共享. XML 把数据从

ASP.NET常用标准配置web.config

在我们的项目开发过程中,我们经常要配置wei.config文件,而大多数的时候配置差不多,下面的是一个简单的配置,其他的配置可以在这个基础上在添加 <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.web> <!-- 动态调试编译 设置 compilation debug="true" 以启用 ASPX 调试.否则,将此值设置为

常用的web服务器软件整理

(1)ApacheApache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上.Apache源于NCSAhttpd服务器,经过多次修改,成为世界上最流行的Web服务器软件之一.Apache取自"a patchy server"的读音,意思是充满补丁的服务器,因为它是自由软件,所以不断有人来为它开发新的功能.新的特性.修改原来的缺陷.Apache的特点是简单.速度快.性能稳定,并可做代理服务器来使用. (2)IIS是英文Internet Informati

Tomcat配置文件之servlet.xml中选项介绍

Servlet.xml 分为以下元素: server, service, Connector ( 表示客户端和service之间的连接), Engine ( 表示指定service 中的请求处理机,接收和处理来自Connector的请求), Context ( 表示一个web 应用程序,通常为WAR 文件,关于WAR 的具体信息见servlet 规范), host ( 表示一个虚拟主机 ), Logger ( 表示日志,调试和错误信息), Realm ( 表示存放用户名,密码及role 的数据库

spring配置文件[servlet-name]-servlet.xml

注解式控制器简介: 在spring2.5之前都是通过实现controller接口或其实现来定义处理器类. spring2.5开始支持通过注解@controller和@requestmapping来定义处理器类,DefaultAnnotationHandlerMapping.AnnotationMethodHandlerAdapter为@controller和@requestmapping提供支持. spring3.0引入restful架构风格支持,引入了更多的注解. spring3.1使用新的H