In the previous article, we explained the process of adding a message converter, the message converter can only take effect under @responsebody annotations, which is what we call the rest interface, then how to configure the rendering page.
Under normal circumstances, SPRINGMVC must not have the views layer, the mainstream views frame has freemarker, Velocity, Tiles, Groovy Markup and JSP, and I prefer Freemarker, Because its syntax is very similar to rails (I'm transitioning from rails), of course freemarker is one of the most popular views frames.
This article describes how to introduce the Freemarker template engine in the 0 XML configuration Springmvc project. introducing a dependency package
SPRINGMVC support for the three-party template engine is in the Spring-context-support package, in addition to the introduction of Freemarker own package.
Pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId> spring-context-support</artifactid>
<version>4.3.13.RELEASE</version>
</ dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId> freemarker</artifactid>
<version>2.3.23</version>
</dependency>
Configure Freemarkerconfigurer
To configure the Freemarkerconfigurer bean in RootConfig:
RootConfig:
@Bean public
freemarkerconfigurer freemarkerconfigurer () {
Freemarkerconfigurer configurer = new Freemarkerconfigurer ();
Configurer.setdefaultencoding ("Utf-8");
Configurer.settemplateloaderpath ("/web-inf/views/");
Properties Properties = new properties ();
Properties.setproperty ("defaultencoding", "UTF-8");
Configurer.setfreemarkersettings (properties);
return configurer;
}
Configure Freemarkerviewresolver
The default Freemarkerconfigurer feature is very small and not enough for us to use, so SPRINGMVC specifically provides the freemarkerviewresolver for us to attach our own personalized configuration.
RootConfig:
@Bean public
freemarkerviewresolver freemarkerviewresolver () {
Freemarkerviewresolver resolver = new Freemarkerviewresolver ();
Resolver.setsuffix (". FTL");
Resolver.setcontenttype ("Text/html;charset=utf-8");
Resolver.setcache (false);
Resolver.setexposerequestattributes (true);
Resolver.setrequestcontextattribute ("request");
Resolver.setorder (0);
return resolver;
}
Add a page
Add a controller
@Controller
@RequestMapping ("/page") public
class Pagetestcontroller {
@GetMapping ("/hello")
public string Hello (string name, model model) {
System.out.println (name);
Model.addattribute ("name", name);
return "app";
}
}
New Page
Add the Webapp/web-inf/views directory under the main directory and add the following code to the directory:
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Results
In the browser, enter:
Http://localhost:8080/page/hello?name= Chinese
Page:
Precautions
Must pay attention to the Chinese coding problem, add the following line, the use of UTF-8 can solve the Chinese coding problem, but also pay attention to the file encoding.
@Bean public
freemarkerconfigurer freemarkerconfigurer () {
Freemarkerconfigurer configurer = new Freemarkerconfigurer ();
Solve the Chinese coding problem
configurer.setdefaultencoding ("Utf-8");
Configurer.settemplateloaderpath ("/web-inf/views/");
Properties Properties = new properties ();
Configurer.setfreemarkersettings (properties);
return configurer;
}
In addition ServletConfig must certainly not overload Webmvcconfigureradapter the following method:
@Override public
void Configureviewresolvers (Viewresolverregistry registry) {
}
Overloading this method will cause our own implementation of the freemarkerviewresolver to not take effect and will take effect on the default freemarkerviewresolver.
Spring's official guidance document is overloaded with this method, causing me to check this problem for nearly two hours, and finally debug tracking source to find the reason.