This is the third article from the zero-independent development site (named Sonne_game) series.
Let's make a brief review of the past period.
The first article is a concise talk about their own ideas and plans for this site. Http://www.cnblogs.com/rixiang/p/6259097.html
In the second chapter, we start to build the framework and complete the SPRING+MYBATIS+SPRINGMVC framework. Http://www.cnblogs.com/rixiang/p/6260433.html
The task today is to integrate freemarker on the basis of SPRING+MYBATIS+SPRINGMVC.
What is Freemarker?
Freemarker is a template engine. It is designed to generate target static files (such as HTML) based on templates and data. So Freemarker is not just for web development.
For web development, the key is that with this template engine, you can constantly generate HTML pages based on templates and data, which enables dynamic (updating data as the user operates), although static HTML is generated.
For example, a page contains the contents of ${usr.usrname}, display the user name, the user click on the next Page button, the background query database, get the next page user name, as the data, with Freemarker from the new generation of HTML, sent to the browser. The new data will replace the ${...} In the content.
Freemarker relative to JSP
This is mainly because of the advantages of Freemarker vs. JSP: The real embodiment of MVC's design thinking.
Because JSP can be inserted into the Java code, although you can not do so, but always someone will do that, and that does not really reflect the idea of MVC, does not really achieve layering. Only by better realizing the stratification can we realize the division of the front-end development and background development better.
As mentioned above, Freemarker is a template, template + data generation HTML page.
On the operating mechanism, the JSP is converted to Servlert by the engine and then compiled into a. class file. Run the servlet instance to return the response to the browser.
Therefore, from the operational mechanism, the two are still very different.
Spring-mvc+freemarker Integration
The SPRING+MYBATIS+SPRING-MVC framework has been set up before, so the only thing to do today is to continue on a previous basis. Http://www.cnblogs.com/rixiang/p/6260433.html
Add the Freemarker jar package.
In the spring configuration file, add the Freemarker configuration:
<BeanID= "Freemarkerconfig"class= "Org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> < Propertyname= "Templateloaderpath"value= "web-inf/ftl/" /> < Propertyname= "Freemarkersettings"> <Props> <propKey= "Template_update_delay">0</prop> <propKey= "Default_encoding">UTF-8</prop> <propKey= "Number_format">0.##########</prop> <propKey= "Datetime_format">YYYY-MM-DD HH:mm:ss</prop> <propKey= "Classic_compatible">True</prop> <propKey= "Template_exception_handler">Ignore</prop> </Props> </ Property> </Bean>
Don't forget to change Viewresolver to Freemarker's view parser:
<!--Freemarker ' s view resolver - <BeanID= "Viewresolver"class= "Org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> < Propertyname= "ContentType"value= "text/html; charset=utf-8" /> < Propertyname= "suffix"value= ". FTL" /> </Bean>
Viewresolver is the concept of SPRING-MVC, do not understand the need to learn extra.
Depending on the current controller class:
PackageSonn.web.controller;Importjava.util.List;ImportJavax.annotation.Resource;Importjavax.servlet.http.HttpServletRequest;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportSonn.web.entity.User;ImportSonn.web.mapper.UserMapper;/*** @ClassName: Gamecontroller * @Description: The controller of game*@authorsonne* @date 2017-1-7 16:20:16 *@version1.0*/@Controller @requestmapping ("/game") Public classGamecontroller {@Resource (name= "Usermapper") PrivateUsermapper Usermapper; @RequestMapping (Value= "/lst", method =requestmethod.get) PublicString Submit (HttpServletRequest Request,model Model)throwsException {List<User> Usr_lst =Usermapper.findall (); Model.addattribute ("LST", Usr_lst); return"Game_lst"; }}
Write a corresponding Freemarker template:
<!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "> <HTML> <Head> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"> <title>Sonne_game</title> </Head> <Body> <P>Hello, here is Sonne's game world. <#listlst as USR> <P>The USR ' s usrname is ${usr.usrname}</P> <P>The USR ' s passwd is ${usr.passwd}</P> </#list> </Body> </HTML>
Start Project, enter address Http://localhost:8888/Sonne_game/game/lst.form
Please check my github:https://github.com/sonnadolf/sonne_game for related codes.
Sonne_game Website Development Spring-mvc+freemarker Integration