SSM-4 web.xml 及相關spring配置檔案
阿新 • • 發佈:2019-01-07
建立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 /> <!-- 對模型檢視名稱的解析,即在模型檢視名稱新增前後綴 --> <bean class="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"?>
<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: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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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/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 configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>