SSM-4 web.xml 及相关spring配置文件

来源:互联网 发布:java loader 编辑:程序博客网 时间:2024/06/07 04:56

建立maven工程,配置好pom,接下来就是web.xml配置。


这个地方走过许多弯路,springmvc相关配置其实不多,理清相互的关系即可。初次配置,建议功能一个一个加,或者将文件拆分,一部分一部分添加,看看运行tomcat后是否有错。   一次性配置好,经常查错查半天,大多是路径错误,没有被获取到,有些功能前后顺序有问题,copy的代码,忘记修改一些参数名等。


web.xml:

1 欢迎页面

2 加载spring容器  相关配置文件  applicationcontext.xml等

3 加载监听器

4 拦截器,比如乱码解决  登陆状态拦截等

5 spring前端控制器,配置文件路径及url拦截模式


<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"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"><display-name>webappname</display-name><!--欢迎页面 主页 --><welcome-file-list><welcome-file>login.html</welcome-file></welcome-file-list><!-- 加载spring容器 注意classpath和classpath*区别--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:config/applicationContext*.xml</param-value></context-param><!--加载监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 解决post乱码 --><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><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 视项目情况可以配置多个filter 拦截器,实现 登陆拦截等功能--><!-- springmvc的前端控制器 拦截一切地址--><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:config/springmvc.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></web-app>


前端控制器配置文件

springmvc.xml:

1 扫描包

2 注解功能

3 视图解析器


<?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.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 --><context:component-scan base-package="com.XXX.controller" /><!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <mvc:annotation-driven /><!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->  <beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean></beans>



spring配置文件

applicationContext.xml

开启注解,扫描包,数据连接池 加载mybatis 

applicationcontext.xml也可以分成多个配置文件,方便管理修改。或者功能增减。一般可以分server  dao trans   3个大功能。独立配置,容易查看。

这时候只要在webxml里配置将几个文件都包含即可。


<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"  xsi:schemaLocation="http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/aop       http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-3.1.xsd  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><!-- 开启注解 --><context:annotation-config /><!-- 自动扫描 service dao等jar包 --><context:component-scan base-package="com.XXX.XXX"><!--过滤controller注解,此为springmvc配置--><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /><context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /></context:component-scan><context:property-placeholder location="classpath:resources/jdbc.properties" />      <!-- 数据库连接池 ali-->      <bean id="dataSource"class="com.alibaba.druid.pool.DruidDataSource"           destroy-method="close">           <property name="url" value="${jdbc.url}" />           <property name="username" value="${jdbc.username}" />           <property name="password" value="${jdbc.password}" />            <property name="driverClassName" value="${jdbc.driver}"/>           <property name="maxActive" value="10" />           <property name="minIdle" value="5" />      </bean><!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->      <beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">           <!-- 数据库连接池 -->           <property name="dataSource" ref="dataSource" />           <!-- 加载mybatis的全局配置文件 -->           <property name="configLocation" value="classpath:configs/SqlMapConfig.xml"/>      </bean>      <beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">           <property name="basePackage" value="com.XXX.mapper"/>      </bean><!-- 事务管理器-->      <beanid="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.XXX.service.*.*(..))" />      </aop:config></beans>




其中数据连接池内相关数据源单独配置,方便修改
jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8

jdbc.username=root

jdbc.password=123456

sqlsession 配置

sqlmapconfig

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration></configuration>