Detailed steps for Java Spring to integrate the Freemarker

  • 2020-04-01 02:26:46
  • OfStack

I � � �
Framework: for springmvc
Springsource -tool-suite-2.9.0
Version: 1.6.0 _29
Tomcat version: apache-tomcat-7.0.26
Introduction: FreeMarker is a templating engine written in the Java language that generates text output based on templates. The FreeMarker is independent of the Web container, that is, it does not know about servlets or HTTP when the Web is running. It can be used not only as an implementation technique for the presentation layer, but also to generate XML, JSP, or Java, etc.
In the context of a Jave Web app, a Freemarker is a Jave Web app that displays the information in the Jave Web app as a template.

Step1. Introduce jar package
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201311/20131114112219.jpg? 20131014112346 ">
Maven generation �
:


<!-- 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. in src/main/resources/conf See new Freemarker  �  sex file freemarker.properties, This is a copyrighted file Freemarker The commonly used  �   �   �   �   , which reads as follows: 
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. The DispatcherServlet Add the configuration required for Freemarker to the context configuration file spring-servlet.xml, as follows:

<!--  configuration Freemarker Private file channel  -->
<bean id="freemarkerConfiguration"        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location" value="classpath:conf/freemarker.properties" />
</bean>
<!--  configuration freeMarker Add the template and the add address  -->
<bean id="freemarkerConfig"        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <!--  The hang resolver is displayed in the /WEB-INF/ftl/ Walk down and trace the file  -->
    <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" />
<!--  configuration freeMarker  �   �  the parser  -->
<bean id="freemakerViewResolver"        class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="viewClass"        value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
    <!--  All right ftl The file at the end of the stream  -->
    <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" />
    <!--  The - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - jsp The value of the resolver is combined with the 2 -->
    <property name="order" value="1" />
</bean>


Step4. The copyright controller file and FTL file
  Under the SRC/main/Java eye � new package www.asuan.com.controller, under the package new HelloWorldController. Java, generation of � 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!";
       //Add the hang - in to the hang - in container
       model.addAttribute("word0",word0);
       model.addAttribute("word1",word1);
        return "helloWorld.ftl";
    }
}

Create a new helloworld.ftl under the web-inf/FTL channel wok configured in step3 and replace the wok with the following:

<!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. Line � � � �
The hang project is deployed to tomcat and the hang row. In the hang: (link: http://localhost:8080/), the project name you have in the hang is helloworld.htm
Achievements:
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201311/20131114112450.jpg? 20131014112614 ">


Related articles: