У меня есть некоторые проблемы, переопределяющие конфигурацию по умолчанию для Mustache для Spring MVC, найденного here в моем проекте.Spring MVC и Mustache - изменить компилятор Mustache по умолчанию null
Полагаю, что mustache.java вызывает MustacheException
по умолчанию, когда шаблон находит переменную, которая разрешена как null. Я бы хотел, чтобы компилятор Mustache разрешил такие значения, как пустая строка.
Я попытался использовать функцию nullValue(String nullValue)
, чтобы установить соответствующее поведение, как показано в приведенном ниже коде, но, похоже, это не имеет никакого эффекта. Я понимаю, что очевидное решение состоит в том, чтобы установить атрибут phoneNumber
на пустое значение, но я бы очень хотел понять, почему этот конкретный код не работает.
Ниже приведен код, который я использую для проверки этого поведения, которое включает объект Employee
, который целенаправленно не создает экземпляр своего значения phoneNumber
в его конструкторе и соответствующие фрагменты кода.
Заранее благодарен!
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>