FreeMarker做爲模板引擎,是比較經常使用的。html
FreeMarker官方文檔地址爲:https://freemarker.apache.org/java
如今瀏覽器或者翻譯工具這麼多,對於英文方面,我想大多數每天敲代碼的,奮戰在前線的同志們,對於不少編程實用單詞應該不陌生,若是語文老師不是XX老師教的,我想經過Google或者其餘翻譯工具翻譯一些意思,我想你們應該是能夠看的懂,和判斷是否存在問題的。mysql
其實我我的英語,也不怎麼樣,曾經就採起過這樣一種策略,來記單詞,稱之爲"趣味單詞記錄法",這個法,我想只要將Good morning 讀成鬼德莫你的人,應該可以很好的體會這種樂趣,記得正是這種樂趣,使我從學校到如今工做,對於英語也不是特別恐懼,固然了,這樣也會致使不少問題,好比發音不標準等等。web
這裏不做討論,這裏想表達的仍是但願,計算機專業的同胞或者其餘轉行或者感興趣的朋友們,不要由於英語而中止對其研究學習或者放棄。redis
好比那些常見的Java代碼生成器,基本都是經過編寫ftl文件,也就是FreeMarker模板文件,經過調用對應的方法來達到代碼生成的目的。spring
其實FreeMarker同JSP或者Volocity整合SpringMVC,並沒有多大的區別,web.xml基本都是那一套。sql
spring.xml配置的基本是掃描dao,掃描service,整合MyBatis或MyBatis Plus和配置數據源管理和使用xml或者事務註解等,若是要用到shiro或redis,也就至關於配置對應的bean。apache
關於Spring+SpringMVC+MyBatis整合實例或Spring+SpringMVC+MyBatis Plus整合實例,你們能夠參考個人以下幾篇博文:編程
若是你是使用或者想學習SpringBoot,能夠參考以下個人幾篇博文:
或者個人SpringBoot實戰系列文章
固然,這些都是基於SpringBoot官方文檔相關的示例+我我的一些想法和理解。
下面進入整合實例:
1、導入依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.test</groupId> <artifactId>freemarket</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency> <!-- 單元測試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- spring核心包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.13.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.13.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.13.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.13.RELEASE</version> </dependency> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.5.4</version> </dependency> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.5.4</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.11</version> </dependency> <!--mybatis核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.1.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> <type>jar</type> </dependency> <!--打印日誌 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.13.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> </dependency> <!-- javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <!-- spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.13.RELEASE</version> </dependency> <!-- spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.13.RELEASE</version> </dependency> <!-- 阿里巴巴fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.41</version> </dependency> <!--dbcp --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.1.1</version> </dependency> <!-- 輔助 --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.4</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> <!-- end --> <!-- swagger-mvc --> <dependency> <groupId>com.mangofactory</groupId> <artifactId>swagger-springmvc</artifactId> <version>1.0.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.6.6</version> </dependency> <!-- poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.9</version> </dependency> <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp --> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.9.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.squareup.okio/okio --> <dependency> <groupId>com.squareup.okio</groupId> <artifactId>okio</artifactId> <version>1.13.0</version> </dependency> <!-- 騰訊雲 --> <dependency> <groupId>com.qcloud</groupId> <artifactId>cos_api</artifactId> <version>5.2.4</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.11</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mindrot/jbcrypt --> <dependency> <groupId>org.mindrot</groupId> <artifactId>jbcrypt</artifactId> <version>0.4</version> </dependency> <!-- https://mvnrepository.com/artifact/net.coobird/thumbnailator --> <dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> <finalName>${project.artifactId}</finalName> </build> </project>
2、編寫Spring-MVC對應的配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 掃描Controller層 --> <context:component-scan base-package="com.freemark.controller"/> <!-- 靜態資源不攔截放行 --> <mvc:default-servlet-handler/> <!-- 開啓註解 --> <mvc:annotation-driven /> <context:component-scan base-package="com.ffcs"></context:component-scan> <mvc:annotation-driven></mvc:annotation-driven> <bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/ftl/"></property> <property name="defaultEncoding" value="utf-8" /> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">1</prop> <prop key="locale">zh_CN</prop> <prop key="datetime_format">yyyy-MM-dd</prop><!-- 時間格式化 --> <prop key="date_format">yyyy-MM-dd</prop> <prop key="number_format">#.##</prop> </props> </property> </bean> <bean id="freeMarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="cache" value="true" /> <property name="prefix" value="" /><!-- 上面已經配了,這裏就不用配啦 --> <property name="suffix" value=".ftl" /> <property name="contentType" value="text/html;charset=UTF-8" /> <property name="allowSessionOverride" value="true" /> <property name="allowRequestOverride" value="true" /> <property name="exposeSpringMacroHelpers" value="true" /> <property name="exposeRequestAttributes" value="true" /> <property name="exposeSessionAttributes" value="true" /> <property name="requestContextAttribute" value="request" /> </bean> </beans>
3、修改web.xml
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>test</display-name> <filter> <filter-name>CharacterEncoding</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> </filter> <filter-mapping> <filter-name>CharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <description>spring mvc servlet</description> <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-mvc.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>
4、編寫對應的Controller代碼
package com.freemark.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class TestController { @GetMapping(value="test") public String test(Model model) { model.addAttribute("userName", "張三"); return "test"; } }
5、在WEB-INF目錄下新建ftl文件夾並在此文件夾下新建test.ftl文件
test.ftl
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> ${userName} </body> </html>
完成上述步驟,啓動項目,並在瀏覽器輸入對應的url,瀏覽器正確返回視圖,控制檯無報錯,表示整合成功。
說明:這次演示只是針對SpringMVC+FreeMarker
若是你想加入Spring+MySQL或者Redis+Shiro,能夠增長對應的配置文件和相關依賴
簡單的說,若是你想用FreeMarker,不使用JSP或者其餘模板,你只需修改對應的Spring-MVC的配置文件便可,並引入對應的maven依賴。
最後寫個簡單的測試便可,測試是否合法沒問題。
關於整合Shiro實例,能夠參考這個博客:https://blog.csdn.net/qq_27676247/article/details/74280158
關於SpringMVC+MyBatis+Spring整合,能夠參考這篇博客:http://zfei.iteye.com/blog/2367552