spring
基本不用介绍了,是目前最好的 IOC 容器了,它的 mvc 架构也十分出色,特别是基于标注的开发方式,免除了大多数框架庞大配置的缺点。而 mustache
则是新兴的模板语言,难能可贵的是在各个主流语言下都有对应的解析器,恰好有机会尝试将 spring mvc 的 view 层替换为 mustache ,感觉很不错~~
1.写个 controller
public class Product{
private String name;
// setter . getter
}
@Controller
public Test(){
@RequestMapping("/{id}")
public String personal(@PathVariable int id, Model model) throws Exception {
Product d = manager.getProduct(id);
model.addAttribute("product", d);
return "personal";
}
}
controller 没有改变,通过 resetful 的 url 暴漏查询服务.
2.下载 mustache 解析器以及 viewresolver
java 解析器
就列在 mustache 主页上,不多说..
而 viewresolver 也已经有人封装好了,但是对于中文用户就欠考虑了,我修改了下放在 这里
3.配置 mustache viewresolver
在 xx-servlet.xml 中配置:
<bean id="viewResolver" class="org.springframework.web.servlet.view.mustache.MustacheViewResolver">
<property name="cache" value="false"/>
<property name="nullValue" value=""/>
<property name="prefix" value="/WEB-INF/mustache/"/>
<property name="suffix" value=".htm"/>
<property name="templateLoader">
<bean class="org.springframework.web.servlet.view.mustache.MustacheTemplateLoader">
<property name="encoding" value="utf-8"/>
</bean>
</property>
<property name="contentType">
<value>text/html; charset=utf-8</value>
</property>
</bean>
其中特别注意的是
当取值为 null时,默认解析器会抛出异常,而经常想要的效果是直接渲染为空字符串,那么需要配置:
<property name="nullValue" value=""/>
中文需要告诉解析器模板文件的编码(否则就系统默认了),一般是 utf-8
<property name="encoding" value="utf-8"/>
也要告诉 viewresolver 最后要修改 content-type 头,否则默认为 iso8859-1 ,浏览器会显示乱码
<value>text/html; charset=utf-8</value>
view name 与模板文件对应和其他类型的 viewresolver 一样。模板文件放在 WEB-INF/mustache 下,根据控制器返回的 view name 来找对应的模板文件并结合 model 渲染出来.
4.模板文件编写
第一步已经在 controller 中将 Product 的实例放在 model 的 product 属性上. 那么就可以直接在模板中读到 Product 的实例值
<h1>{{product.name}}</h1>
如果 Product 实例有多个字段的话,可以采用直接把当前实例作为其属性的 context:
<ul>
{{#product}}
<li> {{name}} </li>
<li> {{price}} </li>
{{/product}}
</ul>
优点:
最大的优点即是可以前后端共用模板,初始屏可以由后端渲染,接下来的无跳转局部刷新可以由 js 取得服务器端模板和数据在客户端直接渲染.
分享到:
相关推荐
Spring5Boot2Mustache Spring5 Boot2小胡子
弹簧同构视图示例 Spring MVC 同构视图示例项目。 在后端和前端之间共享视图模板。 提供 3 个基于 Mustache(Hogan.js, Trimou), ... 也可以通过handlebar 分支与Spring MVC 中的Handlerbars 和Mustache 进行比较。
使用Mustache.js实现的asp.net mvc客户端模版的例子
spring-boot-starter-mustache spring-boot-starter-redis spring-boot-starter-security spring-boot-starter-social-facebook spring-boot-starter-social-linkedin spring-boot-...
刮弹簧胡子 一个简单的 Spring Boot web 应用程序,它使用作为模板语言。 用法 在运行此 webapp 之前,您必须确保其中一个scratch-*-rest webapps 正在... java -jar target/scratch-spring-mustache-1.0-SNAPSHOT.war
Spring MVC示例 Java 1.8.0 Gradle6.0.1 Spring Boot 2.2.2 SpringMVC 自由标记 胡子 模组 mvc-rest-api: @RestController示例 mvc-web-freemarker: @...mvc-web-mustache: @Controller + Mustache示例
resource4j.zip,resource4j与spring框架的集成
PHP实现的Mustache模板语言类库
Mustache:一个Mustache模板语言的PHP实现
mustache - Mustache模板语言的一个Go实现
mustache.php, 在PHP中,mustache 实现 Mustache.phpPHP中的一个 mustache 。 用法一个简单的例子:<?php$m = new Mustache_Engine;echo $m->render('He
赠送jar包:lang-mustache-client-5.5.1.jar; 赠送原API文档:lang-mustache-client-5.5.1-javadoc.jar; 赠送源代码:lang-mustache-client-5.5.1-sources.jar; 赠送Maven依赖信息文件:lang-mustache-client-...
Phly Mustache一个PHP实现的Mustache模板语言类库,感兴趣的朋友们可以下载下来,用到自己的项目中。
使用JMustache实现的Spring Boot。 这些样本非常简单(使用部分渲染非常简单的模板)。 如果需要,请不要犹豫提交样品。 ## 执照MIT许可证(MIT) ##贡献如果发现错误或东西遗失,请随时贡献并提交问题或请求请求...
赠送jar包:lang-mustache-client-6.3.0.jar; 赠送原API文档:lang-mustache-client-6.3.0-javadoc.jar; 赠送源代码:lang-mustache-client-6.3.0-sources.jar; 赠送Maven依赖信息文件:lang-mustache-client-...
spec, mustache 规范 https://github.com/mustache/spec 是 mustache的正式标准。 它定义了解析 mustache 模板语言( 或是它们的超集)的库的正常用法和边缘案例行为。规范是作为一系列YAML文件开发的,在 spe
前端模板引擎Mustache教程.zip 现在前端模块引擎如雨手春笋般出现,当然也多如牛毛 今天来介绍一款我用得还不错的前端引擎Mustache大胡子
Mustache 是一个 logic-less (轻逻辑)模板解析引擎,它的优势在于可以应用在javascript、PHP、Python、Perl 等多种编程语言中
PHP实现的Mustache模板语言类库,感兴趣的朋友们可以下载下来,用到自己的项目中。
Mustache是基于JavaScript实现的模版引擎,类似于JQuery Template,但是这个模版更加的轻量级,语法更加的简单易用,很容易上手。