Home >Java >javaTutorial >Detailed steps for integrating Freemarker with java Spring
My development environment
Framework: springmvc
Development tool: springsource-tool-suite-2.9.0
Version: 1.6.0_29
tomcat version: apache-tomcat-7.0.26
Foreword: FreeMarker is a template engine written in Java language that generates text output based on templates. FreeMarker is web container agnostic, i.e. it doesn't know about servlets or HTTP when running on the web. It can not only be used as an implementation technology for the presentation layer, but also can be used to generate XML, JSP or Java, etc.
In short, Freemarker displays the information obtained from the server on the page in the form of a template in Java Web development.
step1. Introduce the jar package
Maven code:
<!-- 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. Add in the DispatcherServlet context configuration file spring-servlet.xml The configuration required by Freemarker, the code is as follows:
<!-- 配置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. Write the controller file and ftl file
Create a new package www.asuan.com.controller in the src/main/java directory, and create a new HelloWorldController under the package .java, the code is as follows:
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"; } }
Create a new helloWorld.ftl under the WEB-INF/ftl path configured in step3, the code is as follows:
<!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. Run and debug
Convert the project Deploy to tomcat and run, visit in the browser: http://localhost:8080/the project name you set/helloWorld.htm
Running result:
For more detailed steps for Java Spring to integrate Freemarker, please pay attention to the PHP Chinese website!