1. 程式人生 > 實用技巧 >SSM + Freemarker 開發框架快速搭建

SSM + Freemarker 開發框架快速搭建

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=/
  1. 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 請求訪問即可。