Detailed steps for Java Spring consolidation Freemarker _java

Source: Internet
Author: User

My environment.
Frame: Springmvc
Development tool: springsource-tool-suite-2.9.0
Version: 1.6.0_29
Tomcat version: apache-tomcat-7.0.26
Freemarker is a template engine written in the Java language that generates text output based on a template. Freemarker is not related to the Web container, that is, when the web is running, it does not know the servlet or HTTP. Not only can it be used as a performance layer implementation technology, but also can be used to generate xml,jsp or Java.
In short, Freemarker is a template in the Jave Web launch to display information from the server in a format.

Step1. Introducing Jar Packs

Maven Codes
:

Copy Code code as follows:

<!--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 document under the new Freemarker freemarker.properties, this document defines the freemarker commonly used code conversion , the code is 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. In DispatcherservletThe configuration required to add Freemarker to the context configuration file Spring-servlet.xml is as follows:
Copy Code code as follows:

<!--configuration of freemarker file path-->
<bean id= "freemarkerconfiguration" class= "Org.springframework.beans.factory.config.PropertiesFactoryBean" >
<property name= "Location" value= "Classpath:conf/freemarker.properties"/>
</bean>
<!--Configure Freemarker template add address-->
<bean id= "Freemarkerconfig" class= "Org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" >
<!--The video parser scans the file in the/web-inf/ftl/path-->
<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 Video parser-->
<bean id= "Freemakerviewresolver" class= " Org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver ">
<property name= "Viewclass" value= "Org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
<!--scans all FTL end files-->
<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"/>
<!--to the image parser to configure the priority, you can give the previous JSP image parser value of 2-->
<property name= "Order" value= "1"/>
</bean>


step4. Edit controller files and FTL files
In the Src/main/java, new package Www.asuan.com.controller under the package, the new Helloworldcontroller.java, the code is as follows:
Copy Code code 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 data to the visual data container
Model.addattribute ("word0", word0);
Model.addattribute ("Word1", word1);
return "HELLOWORLD.FTL";
}
}

In the STEP3 configuration of the WEB-INF/FTL path under the new HELLOWORLD.FTL, the code is as follows:
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
</body>


Step5. Running and tuning
Deploy the project to Tomcat and run, and interview in the browser: http://localhost:8080/the project name you set/helloworld.htm
Running results:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.