版权声明:本文为博主原创文章,未经博主允许不得转载。转载请附上博主博文网址,并标注作者。违者必究 https://blog.csdn.net/HuHui_/article/details/79301222
ssm_spring.xml+springmvc.xml+web.xml详解和注意事项
ReadMe
搭建SSM的配置文件主要有三个,分别是web.xml,application.xml,application-mvc.xml。在这里分别理解一下他们的作用和关联。后面希望总结理清几个框架的各种方法。
如有错误,感谢指导。
转载请注明出处和原文链接
2018-2-9
注意点
- web.xml
a.<url-pattern>/</url-pattern>和 <url-pattern>/*</url-pattern>的区别和选择。
< url-pattern > / </ url-pattern > 不会匹配到*.jsp,
即:*.jsp不会进入spring的 DispatcherServlet类 。
< url-pattern > /* </ url-pattern > 会匹配*.jsp,
会出现返回jsp视图时再次进入spring的DispatcherServlet 类,导致找不到对应的controller所以报404错。
b. 如何不拦截静态资源
//在spring-mvc.xml进行通过
<mvc:resources location="/WEB-INF/resources/images/" mapping="/resources/images/**" />
//设置必须在Dispatcher(前置控制器)的前面
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
<url-pattern>*.css</url-pattern>
<url-pattern>/assets/*"</url-pattern>
<url-pattern>/images/*</url-pattern>
</servlet-mapping>
// /-》*.action
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
- spring和spring-mvc 的配置
a. 在使用一些模板的时候,应该从properties读取变量
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Hui</display-name>
<!--启动后弹出来的页面-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--加载spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/application.xml</param-value>
</context-param>
<!--监听器,启动Web容器时,自动装配ApplicationContext的配置信息-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--静态资源不拦截-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
<url-pattern>*.css</url-pattern>
<url-pattern>/assets/*"</url-pattern>
<url-pattern>/images/*</url-pattern>
</servlet-mapping>
<!--配置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>classpath:config/application-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- <url-pattern>*.do</url-pattern>-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置字符过滤器,设置为UTF-8-->
<filter>
<filter-name>CharacterEncodingFilter</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>
<!--配置过滤的url为全部-->
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Spring application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 自动扫描service,将标注Spring注解的类自动转化Bean -->
<context:component-scan base-package="com.hui">
<!-- <context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" /> -->
</context:component-scan>
<!-- 引入配置文件 -->
<context:property-placeholder location="classpath:properties/jdbc.properties"/>
<!--创建数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--maxActive: 最大连接数量 -->
<property name="maxActive" value="${jdbc.maxActive}"/>
<!--minIdle: 最小空闲连接 -->
<property name="minIdle" value="${jdbc.minIdle}"/>
<!--maxIdle: 最大空闲连接 -->
<property name="maxIdle" value="${jdbc.maxIdle}"/>
<!--initialSize: 初始化连接 -->
<property name="initialSize" value="${jdbc.initialSize}"/>
<!-- 连接被泄露时是否打印 -->
<property name="logAbandoned" value="true"/>
<!--removeAbandoned: 是否自动回收超时连接 -->
<property name="removeAbandoned" value="true"/>
<!--removeAbandonedTimeout: 超时时间(以秒数为单位) -->
<property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>
<!--maxWait: 超时等待时间以毫秒为单位 -->
<property name="maxWait" value="${jdbc.maxWait}"/>
<!-- 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. -->
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
<!-- 在每次空闲连接回收器线程(如果有)运行时检查的连接数量 -->
<property name="numTestsPerEvictionRun" value="${jdbc.numTestsPerEvictionRun}"/>
<!-- 1000 * 60 * 30 连接在池中保持空闲而不被空闲连接回收器线程 -->
<property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
<property name="validationQuery" value="SELECT 1"/>
</bean>
<!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--声明式事务
@Transactional
应用到方法上,该方法使用事务;
应用到类上,类的方法使用事务,
定义到父类上,执行父类的方法时使用事务;-->
<!-- 使用JDBC事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 使用annotation注解方式配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--映射接口类文件(.java)和映射XML文件(.xml)需要放在相同的包下,没有必要在Spring的XML配置文件中注册所有的映射器,可以使用一个MapperScannerConfigurer ,它将会查找类路径下的映射器并自动将它们创建成MapperFactoryBean。如果你使用了一个
以上的DataSource,那么自动装配可能会失效,可以指定sqlSessionFactoryName,多个包逗号分隔 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hui.*.*.dao"/>
</bean>
</beans>
SpringMVC application-mvc.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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<!-- 只扫描Controller注解的类 -->
<context:component-scan base-package="com.hui" use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 处理器适配器和处理器映射器 自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter两个bean -->
<mvc:annotation-driven>
<mvc:message-converters>
<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>
<!-- 通用json日期转换, @JsonFormat 优先级更高 -->
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/>
</bean>
</property>
<!-- Json 不为空 -->
<property name="serializationInclusion">
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/page/"/>
<property name="suffix" value=".jsp"/>
<property name="contentType" value="text/html; charset=utf-8"/>
<property name="cache" value="false"/>
</bean>
<!--DispatcherServlet拦截"/",也可以访问静态资源,不会出现404 -->
<!-- <mvc:resources location="/WEB-INF/resources/images/" mapping="/resources/images/**" />
<mvc:resources location="/WEB-INF/resources/css/" mapping="/resources/css/**" />
<mvc:resources location="/WEB-INF/resources/js/" mapping="/resources/js/**" />-->
<!-- 异常处理 -->
<bean id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView">
<value>/error/500</value>
</property>
<property name="defaultStatusCode">
<value>500</value>
</property>
<!-- spring就会使用apache的org.apache.commons.logging.Log日志工具,
记录这个异常,级别是warn -->
<property name="warnLogCategory">
<value>org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
</value>
</property>
</bean>
<!-- 附件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="10485760000"></property>
<property name="maxInMemorySize" value="40960"></property>
</bean>
<!-- <mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*/*"/>
<bean class=" com.hui.usercenter.user.action.AuthHandlerInterceptor">
<property name="allowUrilist">
<list>
<value>/login</value>
<value>/tologin</value>
</list>
</property>
</bean>
</mvc:interceptor>
</mvc:interceptors>-->
</beans>