您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

java Spring整合Freemarker的详细步骤

2024/2/21 7:42:17发布16次查看
我的開發環境
框架:springmvc
開發工具:springsource-tool-suite-2.9.0
版本:1.6.0_29
tomcat版本:apache-tomcat-7.0.26
前言:freemarker是一个用java语言编写的模板引擎,它基于模板来生成文本输出。freemarker与web容器无关,即在web运行时,它并不知道servlet或http。它不仅可以用作表现层的实现技术,而且还可以用于生成xml,jsp或java 等。
簡而言之,freemarker就是在jave web開發中以模板的方式在頁面展示從服務端獲取的信息。
step1.引入jar包
maven代碼:
<!-- freemarker --> <dependency> <groupid>org.freemarker</groupid> <artifactid>freemarker</artifactid> <version>2.3.20</version> </dependency> <!-- ui.freemarker --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context-support</artifactid> <version>3.2.4.release</version> </dependency> step2.在src/main/resources/conf目錄下新建freemarker屬性文件freemarker.properties,此屬性文件定義了freemarker常用的編碼轉換,代碼如下: tag_syntax=auto_detect template_update_delay=2 default_encoding=utf-8 output_encoding=utf-8 locale=zh_cn date_format=yyyy-mm-dd time_format=hh:mm:ss datetime_format=yyyy-mm-dd hh:mm:ss
step3.在dispatcherservlet上下文配置文件spring-servlet.xml中添加freemarker所需的配置,代碼如下:
<!-- 配置freemarker屬性文件路徑 --> <bean id="freemarkerconfiguration" class="org.springframework.beans.factory.config.propertiesfactorybean"> <property name="location" value="classpath:conf/freemarker.properties" /> </bean> <!-- 配置freemarker模板加載地址 --> <bean id="freemarkerconfig" class="org.springframework.web.servlet.view.freemarker.freemarkerconfigurer"> <!-- 視圖解析器會在/web-inf/ftl/路徑下掃描視圖文件 --> <property name="templateloaderpath" value="/web-inf/ftl/" /> <property name="freemarkervariables"> <map> <entry key="xml_escape" value-ref="fmxmlescape" /> </map> </property> </bean> <bean id="fmxmlescape" class="freemarker.template.utility.xmlescape" /> <!-- 配置freemarker視圖解析器 --> <bean id="freemakerviewresolver" class="org.springframework.web.servlet.view.freemarker.freemarkerviewresolver"> <property name="viewclass" value="org.springframework.web.servlet.view.freemarker.freemarkerview" /> <!-- 掃描路徑內所有以ftl結尾的文件 --> <property name="viewnames"> <array> <value>*.ftl</value> </array> </property> <property name="contenttype" value="text/html; charset=utf-8" /> <property name="exposerequestattributes" value="true" /> <property name="exposesessionattributes" value="true" /> <property name="exposespringmacrohelpers" value="true" /> <property name="requestcontextattribute" value="request" /> <!-- 給視圖解析器配置優先級,你可以給之前jsp視圖解析器的值配為2 --> <property name="order" value="1" /> </bean>
step4.編寫controller文件和ftl文件
在src/main/java目錄下新建包www.asuan.com.controller,在包下新建helloworldcontroller.java,代碼如下:
package www.asuan.com.controller; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.requestmapping; @controller public class helloworldcontroller { @requestmapping("/helloworld") public string helloworld(model model) { string word0 = "hello "; string word1 = "world!"; //將數據添加到視圖數據容器中 model.addattribute("word0",word0); model.addattribute("word1",word1); return "helloworld.ftl"; } }
在step3中配置的web-inf/ftl路徑下新建helloworld.ftl,代碼如下:
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>insert title here</title> </head> <body> <h2>${word0}${word1}</h2> </body> </html>
step5.運行與調試
將工程部署到tomcat并運行,在瀏覽器中訪問:http://localhost:8080/你設置的工程名/helloworld.htm
運行結果:
更多java spring整合freemarker的详细步骤。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product