This article introduces the introduction of the Spring MVC framework into the Handlebars.js plug-in basic step 1, download the Handlebars.js plugin, and add it to the project
2. Download the handlebars-dependent jar package and add it to the project
The red box is the handlebars core package, and the other is the Handlebars dependent toolkit
3. Add handlebars View parser configuration in spring MVC configuration file Springmvc-servlet.xml
1 <!--VIEW RESOLVER -2 <BeanID= "Handlebarsviewresolver"class= "Com.github.jknack.handlebars.springmvc.HandlebarsViewResolver">3 <!--<constructor-arg value= "Com.ruijie.crazy.handlebars.CustomHandlebarsView"/> -4 < Propertyname= "Order"value= "1" />5 < Propertyname= "prefix"value= "/page/" />6 < Propertyname= "suffix"value= ". html" />7 < Propertyname= "ContentType"value= "Text/html;charset=utf-8" />8 < Propertyname= "Failonmissingfile"value= "false" />9 < Propertyname= "Cache"value= "false" />Ten </Bean>
4. Testing
The Controller implements the following:
1 PackageCom.ruijie.crazy.controller;2 3 ImportJava.util.HashMap;4 ImportJava.util.Map;5 6 ImportOrg.springframework.stereotype.Controller;7 Importorg.springframework.web.bind.annotation.RequestMapping;8 ImportOrg.springframework.web.bind.annotation.RequestMethod;9 ImportOrg.springframework.web.bind.annotation.ResponseBody;Ten ImportOrg.springframework.web.servlet.ModelAndView; One A @Controller -@RequestMapping ("/myweb") - Public classMyfirstcontroller { the -@RequestMapping (value = "/test", method =requestmethod.get) - PublicModelandview Getuserinfobycode () { -map<string, object> map =NewHashmap<string, object>(); +Map.put ("UserName", "YPF"); - return NewModelandview ("Hello", map); + } A at}
The front-end hello.html is implemented as follows:
1 <!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd ">2 <HTML>3 <Head>4 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">5 <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge">6 <Metaname= "Viewport"content= "Width=device-width, initial-scale=1, User-scalable=no"> 7 <title>Hello YPF</title>8 <Scripttype= "Text/javascript"src=".. /js/libs/jquery/jquery.min.js "></Script> 9 <Scripttype= "Text/javascript"src=".. /js/libs/handlebars/handlebars-1.0.0.beta.6.js "></Script>Ten </Head> One <Body> A <Div> - {{UserName}} - </Div> the </Body> - </HTML>
To run the project, enter http://127.0.0.1/crazypf/myweb/test.html in the browser and display the results as follows:
At this point handlebars successfully introduced the project
Introducing the handlebars plugin into the Spring MVC framework