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>
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>
阅读全文
0 0
- SSM-4 web.xml 及相关spring配置文件
- SSM配置文件-web.xml
- SSM(spring-springmvc-mybatis)整合开发xml配置文件内容springmvc-config.xml,application.xml,web.xml
- 一个spring web的配置文件web.xml
- [spring]Spring MVC web.xml 配置文件配置
- ssm配置文件(一) ——web.xml配置
- 关于SSM的配置文件和Web.xml的配置
- Maven Web Project整合SSM之 pom.xml配置文件详解
- ssm spring+springmvc+mybatis中的xml配置文件详解
- SSM:spring+springmvc+mybatis框架中的XML配置文件功能
- Spring配置文件和SpringMVC配置文件 web.xml配置文件 保存自用
- xml相关概念及java读取配置文件
- jsf、Spring、Hibernate 集成 web.xml配置文件
- spring + ibatis 配置文件浅析 之 web.xml
- spring多个配置文件web.xml写法
- spring struts hibernate web.xml配置文件模板
- Spring MVC 配置文件 web.xml文件详解
- web.xml加载多个spring配置文件
- Spark中Job、Stage和Task的关系
- 查看SQLServer占用内存
- Java Calendar 类的时间操作
- Mongodb 命令
- JS document 用法
- SSM-4 web.xml 及相关spring配置文件
- 用Git向远程仓库提交代码步骤
- 如何轻松爬取网页数据?
- StringUtils中 isNotEmpty 和isNotBlank的区别
- Ubuntu学习日记--Lesson3:vim编辑器使用和常用命令
- UEditor setContent()多一行 setContent()报错
- XADC
- String 和StringBuffer的区别
- HttpClient 4.5.3 模拟登陆CSDN