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=https://www.cnblogs.com/doondo/p/"${db.username}" />
classpath:mapper/*Mapper.xml
classpath:org/taoguoguo/meeting/mapper/*Mapper.xml
三、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=https://www.cnblogs.com/doondo/p/"/WEB-INF/ftl/"/>
10
zh_CN
yyyy-MM-dd HH:mm:ss
yyyy-MM-dd
HH:mm:ss
#.####
四、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 請求訪問即可,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/35171.html
標籤:Java
