1.項目骨架建立
一、使用開發工具IDEA,構建Maven項目,然后調整Maven項目結構,使其成為一個標准的web項目:
-
此處不選擇Maven骨架,直接Next:
-
輸入項目的相關信息,直接Finish
-
項目構建完成后,選擇pom.xml 打包方式為 war
<packaging>war</packaging>
-
選擇1,建立項目的webapp目錄,選擇2,在指定目錄的WEB-INF下建立web.xml文件:
項目最終構建完成結構層次圖:
2.整合SSM + FreeMarker:
一、數據庫資源文件配置:
# 數據庫配置文件 db.properties
db.username=root
db.password=root
db.url=jdbc:mysql:///meeting?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
二、Spring配置:
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--Spring配置文件,配置注解掃描,使用過濾器不掃描Controller注解-->
<context:component-scan base-package="org.taoguoguo.meeting" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--加載外部資源-->
<context:property-placeholder location="classpath:db.properties" />
<!--數據源配置-->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="url" value="${db.url}" />
</bean>
<!--mybatis配置-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="org.taoguoguo.meeting.model" />
<!--mapper資源加載多路徑配置-->
<property name="mapperLocations">
<array>
<value>classpath:mapper/*Mapper.xml</value>
<value>classpath:org/taoguoguo/meeting/mapper/*Mapper.xml</value>
</array>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="mapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean" />
<property name="basePackage" value="org.taoguoguo.meeting.mapper" />
</bean>
<!--事務配置-->
<!-- 配置事務管理器 -->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--定義屬性,聲明事務規則 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--定義切面-->
<aop:config>
<aop:pointcut id="pc1" expression="execution(* org.taoguoguo.meeting.service.*.*(..))"/>
<!-- 將事務增強與切入點組合(織入事務切面) -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc1" />
</aop:config>
<!--注解方式配置事務
<tx:annotation-driven transaction-manager="transactionManager" />
-->
</beans>
三、SpringMVC配置:
<?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: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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--Springmvc配置文件 只掃描Controller注解-->
<context:component-scan base-package="org.taoguoguo.meeting" use-default-filters="true">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--注解驅動 HandlereMapping & HandlerAdapter-->
<mvc:annotation-driven />
<!--靜態資源放行-->
<mvc:resources mapping="/**" location="/" />
<!--加載外部資源文件-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:freemarker-var.properties</value>
</list>
</property>
</bean>
<!--配置模板基本屬性-->
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" id="freeMarkerConfigurer">
<!--模板文件位置-->
<property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
<!--默認編碼格式-->
<property name="defaultEncoding" value="UTF-8" />
<!--全局變量設置-->
<property name="freemarkerVariables">
<map>
<entry key="root" value="${root}" />
</map>
</property>
<!--基本格式配置:時間格式、數字格式-->
<property name="freemarkerSettings">
<props>
<!--模版的緩存時間,單位是s,超過緩存時間則從磁盤加載最新的模版-->
<prop key="template_update_delay">10</prop>
<!--設置默認地區,主要影響數字、日期輸出格式,request中沒有指定地區時模板查找的值-->
<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="time_format">HH:mm:ss</prop>
<!--設置數字的輸出格式-->
<prop key="number_format">#.####</prop>
</props>
</property>
</bean>
<!--freemarker視圖解析器配置-->
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<!--視圖解析器類-->
<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
<!--后綴配置-->
<property name="suffix" value=".ftl"/>
<!--設置各種屬性覆蓋-->
<!--允許重寫Request屬性-->
<property name="allowRequestOverride" value="true"/>
<!--允許重寫Session屬性-->
<property name="allowSessionOverride" value="true"/>
<!--設置request Attribute添加到模型-->
<property name="exposeRequestAttributes" value="true"/>
<!--設置session Attribute添加到模型-->
<property name="exposeSessionAttributes" value="true"/>
<!--頁面內容類型-->
<property name="contentType" value="text/html;charset=utf-8"/>
</bean>
</beans>
四、FreeMarker配置:
##freemarker-var.properties 也可以在mvc配置文件中直接寫死
root=/
-
web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--spring配置--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--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:spring-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--亂碼過濾器配置--> <filter> <filter-name>encodingFilter</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>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
最后在指定目錄下創建html文件,將后綴改為ftl,然后寫一個Controller 請求訪問即可。