在我的项目中,我有一些问题覆盖了Spring MVC的Mustache for here的默认配置。
我的理解是,当模板找到一个被解析为null的变量时,默认情况下mustache.java会引发MustacheException
。我希望Mustache编译器将这些值解析为空字符串。
我尝试使用nullValue(String nullValue)
函数设置适当的行为,如下面的代码所示,但它似乎没有任何效果。我理解显而易见的解决方案是将phoneNumber
属性设置为空值,但我真的很想理解为什么这个特定代码不起作用。
您将在下面找到我用来测试此行为的代码,其中包含一个Employee
实体,该实体故意不在其构造函数中实例化其phoneNumber
成员值以及相关代码片段。 / p>
提前致谢!
的pom.xml
<dependency>
<groupId>com.github.sps.mustache</groupId>
<artifactId>mustache-spring-view</artifactId>
<version>1.3</version>
</dependency>
<!-- jmustache -->
<dependency>
<groupId>com.samskivert</groupId>
<artifactId>jmustache</artifactId>
<version>1.10</version>
</dependency>
<!-- mustache.java -->
<dependency>
<groupId>com.github.spullara.mustache.java</groupId>
<artifactId>compiler</artifactId>
<version>0.8.17</version>
</dependency>
Employee.java
public class Employee {
protected String firstName;
protected String lastName;
protected String phoneNumber;
public Employee() { }
public Employee(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
// Getter and Setters for all attributes
}
getViewResolver
@Bean
public ViewResolver getViewResolver(ResourceLoader resourceLoader) {
MustacheViewResolver mustacheViewResolver = new MustacheViewResolver();
mustacheViewResolver.setPrefix("/src/main/resources/templates/");
mustacheViewResolver.setSuffix(".html");
mustacheViewResolver.setCache(false);
mustacheViewResolver.setContentType("text/html;charset=utf-8");
JMustacheTemplateLoader mustacheTemplateLoader = new JMustacheTemplateLoader();
mustacheTemplateLoader.setResourceLoader(resourceLoader);
JMustacheTemplateFactory mustacheTemplateFactory = new JMustacheTemplateFactory();
mustacheTemplateFactory.setTemplateLoader(mustacheTemplateLoader);
mustacheTemplateFactory.setCompiler(Mustache.compiler().nullValue(" "));
mustacheViewResolver.setTemplateFactory(mustacheTemplateFactory);
return mustacheViewResolver;
}
EmployeeController
@Autowired
private EmployeeRepository repository;
...
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String viewEmployee(@PathVariable Long id, Model model) {
model.addAttribute("employee", repository.findOne(id));
return "employee_view";
}
employee_view.html
<!DOCTYPE html>
<html>
<body>
<ul>
{{#employee}}
<li> First Name: {{firstName}}
<li> Last Name: {{lastName}}
<li> Phone Number: {{phoneNumber}}
{{/employee}}
</ul>
</body>
</html>
答案 0 :(得分:1)
我现在正在使用spring-boot.1.2.5.RELEASE。
如果您查看MyCustomValidationRule
的源代码(第73行~78行),请致电告诉您org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration
无法配置!!!
可能你应该新建一个你自己的defaultValue
来修复它。
或者您可以使用spring-boot-starter
或freemarker
代替它。
或者,当ApplicationContext启动时,您可以通过velocity
来破解它。
BeanPostProcessor
祝你好运!
答案 1 :(得分:0)
你试过这个吗?
<!DOCTYPE html>
<html>
<body>
<ul>
{{#employee}}
<li> First Name: {{firstName}}
<li> Last Name: {{lastName}}
<li> Phone Number:{{#phoneNumber}} {{phoneNumber}} {{/phoneNumber}} {{^phoneNumber}} NA {{/phoneNumber}}
{{/employee}}
</ul>
</body>
答案 2 :(得分:0)
我设法通过创建自定义Mustache自动配置来实现。首先排除默认的MustacheAutoConfiguration。
@SpringBootApplication
@EnableAutoConfiguration(exclude = MustacheAutoConfiguration.class)
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
然后为Mustache创建新的自动配置。
@Configuration
@EnableConfigurationProperties({MustacheProperties.class})
public class LayoutAutoConfiguration extends MustacheAutoConfiguration {
public LayoutAutoConfiguration(MustacheProperties mustache, Environment environment, ApplicationContext applicationContext) {
super(mustache, environment, applicationContext);
}
@Override
public Mustache.Compiler mustacheCompiler(Mustache.TemplateLoader mustacheTemplateLoader) {
return super.mustacheCompiler(mustacheTemplateLoader).defaultValue("");
}
}
Spring Boot 1.5.1.RELEASE with jmustache 1.13。