本篇介绍引入spring mvc框架中引入handlebars.js插件最基本步骤
1、下载handlebars.js插件,并添加到项目中
2、下载handlebars依赖的jar包,添加到工程
红框中的是handlebars核心包,其他是handlebars依赖的工具包
3、在spring mvc配置文件springMvc-servlet.xml中添加handlebars视图解析器配置
1 23 4 5 6 7 8 9 10
4、测试
controller中实现如下:
1 package com.ruijie.crazy.controller; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestMethod; 9 import org.springframework.web.bind.annotation.ResponseBody;10 import org.springframework.web.servlet.ModelAndView;11 12 @Controller13 @RequestMapping("/myweb")14 public class MyFirstController {15 16 @RequestMapping(value = "/test", method = RequestMethod.GET)17 public ModelAndView getUserInfoByCode() { 18 Mapmap = new HashMap (); 19 map.put("userName", "ypf"); 20 return new ModelAndView("hello",map);21 }22 23 }
前端hello.html实现如下:
1 2 3 4 5 6 7hello ypf 8 9 10 11 1213 { {userName}}1415 16
运行工程,在浏览器输入http://127.0.0.1/crazypf/myweb/test.html,显示结果如下:
至此handlebars成功引入工程