The integration process is as follows:
1. Create a new MAVEN Web project that uses the associated jar of Maven dependency Spring,springmvc,freemarker, and the project directory is as follows:
The configuration in the Pom.xml file is as follows:
<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>com.ffcs.oss</groupId> <artifactId> Springmvc_freemarker</artifactid> <version>0.0.1-SNAPSHOT</version> <packaging>war</ Packaging> <properties> <org.springframework.version>4.1.6.release</org.springframework.vers ion> <freemarker.version>2.3.23</freemarker.version> </properties> <DEPENDENCIES&G
T <dependency> <groupId>org.freemarker</groupId> <artifactid>freemarker</ artifactid> <version>${freemarker.version}</version> </dependency> <de Pendency> <GROUPID>ORG.SPRingframework</groupid> <artifactId>spring-core</artifactId> <version>${or g.springframework.version}</version> </dependency> <dependency> <groupid& Gt;org.springframework</groupid> <artifactId>spring-beans</artifactId> <versi on>${org.springframework.version}</version> </dependency> <dependency> &L T;groupid>org.springframework</groupid> <artifactId>spring-aop</artifactId> &
Lt;version>${org.springframework.version}</version> </dependency> <dependency>
<groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> <dependenc Y> <GROupid>org.springframework</groupid> <artifactId>spring-context-support</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency>
;
<groupId>org.springframework</groupId> <artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version> </dependency> <dependency>
<groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework.version}</version> </dependency> </dependencie S> </project>
2. Configure Web.xml files
<?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_3_0.xsd "id=" webapp_id "version=" 3.0 "> <servlet> <servlet- Name>springdispatcherservlet</servlet-name> <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-n Ame>contextconfiglocation</param-name> <param-value>classpath:springmvc_freemarker.xml</para
m-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern& Gt;/*</url-pattern> </servlet-mapping> </web-app>
3. Configure SPRINGMVC and Freemarker consolidated configuration files Springmvc_freemarker.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns:
Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "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.1.xsd http://www.springframework.org/ Schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd "> <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.freemark" Er. Freemarkerconfigurer "> <property name=" Templateloaderpath "value="/web-inf/ftl/"></property> <property name=" defaultencoding "value=" Utf-8 "/> <property name=" free
Markersettings "> <props> <prop key=" Template_update_delay ">1</prop> <prop key= "locale" >zh_CN</prop> <prop key= "Datetime_format" >YYYY-MM-DD</PR op><!--time format--> <prop key= "Date_format" >yyyy-MM-dd</prop> <prop key= "Number_format" >#.##</prop> </props> </property> </bean> <b Ean id= "Freemarkerviewresolver" class= "Org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver" &
Gt <property name= "Cache" value= "true"/> <property name= "prefix" value= ""/><!--it's already on.
; <property name= "suffix" value= ". Ftl"/> <property name= "ContentType" value= "Text/html;charset=utf-8"/>
; <property name= "Allowsessionoverride" value= "true"/> <property name= "allowrequestoverride" value= "t Rue "/> <property name=" Exposespringmacrohelpers "value=" true "/> <property name=" exposereques Tattributes "value=" true "/> <property name=" exposesessionattributes "value=" true "/>
Y name= "Requestcontextattribute" value= "request"/> </bean> </beans>
4. Create a new Freemarker template file May.ftl under the web-inf/ftl/directory, which reads as follows:
user:<br/>
${user.name}--->${user.age}<br/>
list:<br/>
< #list List as item >
<font color= "Red" >${item}</br></font>
</#list >
param:</br>
$ {requestparameters.a} a=${param!}
5. Write the user and Testfreemarker two classes, the code looks like this:
User class:
Package com.ffcs.oss.domain;
Import java.io.Serializable;
public class User implements Serializable {
/**
* * */
private static final long Serialversionuid = 1l;
private String name;
Private Integer age;
Public User () {
super ();
}
Public User (String name, Integer age) {
super ();
this.name = name;
This.age = age;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
Public Integer Getage () {return age
;
}
public void Setage (Integer age) {
this.age = age;
}
}
Testfreemarker class:
Package Com.ffcs.oss.controller;
Import java.util.ArrayList;
Import java.util.List;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestParam;
Import Org.springframework.web.bind.annotation.ResponseBody;
Import Org.springframework.web.servlet.ModelAndView;
Import Com.ffcs.oss.domain.User; @Controller @RequestMapping (value = "/freemarker") public class Testfreemarker {@RequestMapping (value = "/testfreemar Ker ") @ResponseBody public Modelandview testfreemarker (@RequestParam (value =" a ", Required = False) String a) thro
WS Exception {modelandview mv = new Modelandview ();
User user = New User ("may", 21);
list<string> list = new arraylist<string> ();
List.add ("Jack1");
List.add ("Jack2");
List.add ("Jack3");
Mv.addobject ("list", list);
Mv.addobject ("user", user); Mv.addobject ("PaRam ", New String (A.getbytes (" iso8859-1 ")," Utf-8 "));
Mv.setviewname ("may");
return MV;
}
}
6. To run the project to Tomcat, project startup if there is no error, then open the browser, enter url:http://localhost:8080/springmvc_freemarker/freemarker/ Testfreemarker?a=123, after the return, the effect figure is as follows:
So far, SPRINGMVC and Freemarker have been successfully integrated. The following is attached demo source download address:
Link: http://pan.baidu.com/s/1qYxxEUg
Password: 0azi