Spring MVC 和 Mustache - 更改 Mustache 的编译器默认空值

标签 spring spring-mvc mustache

我在覆盖 Mustache for Spring MVC 的默认配置时遇到了一些问题 here在我的项目中。

我的理解是,当模板发现解析为 null 的变量时,mustache.java 默认会引发 MustacheException。我希望 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>

员工.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;
}

雇员 Controller

@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>

最佳答案

我现在正在使用 spring-boot.1.2.5.RELEASE。

如果你查看 org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration 的源代码(第 73 ~ 78 行),你会告诉你 defaultValue 是不可配置的! !!

也许你应该自己制作一个新的 spring-boot-starter 来修复它。 或者您可以使用 freemarkervelocity 代替它。

或者您可以在 ApplicationContext 启动时通过 BeanPostProcessor 破解它。

像这样:

@Bean
public BeanPostProcessor mutacheHackerBeanPostProcessor() {
    return new BeanPostProcessor() {
        private static final String BEAN_NAME = "mustacheCompiler";

        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            return bean;
        }

        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (ClassUtils.isAssignable(bean.getClass(), Mustache.Compiler.class) || BEAN_NAME.equals(beanName)) {
                System.out.println("----------");
                System.out.println("hack ok!!!");
                System.out.println("----------");
                Mustache.Compiler compiler = (Mustache.Compiler) bean;
                return compiler.defaultValue("").nullValue("");
            }

            return bean;
        }
    };
}

祝你好运!

关于Spring MVC 和 Mustache - 更改 Mustache 的编译器默认空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29766577/

相关文章:

java - EurekaServer com.netflix.discovery.shared.transport.TransportException : Cannot execute request on any known server on Docker

java - 找不到可接受的表示 - 406 Not Acceptable 错误

java - Spring 验证注释 - 如何验证字符串中的 2 个字符条目是否是实际的美国州?

node.js - 模板/渲染 - 如何为我的 View 之一排除或指定备用layout.mustache?

java - gradle依赖文件构建gradle

mysql - java.sql.SQLException : Access denied for user 'root' @'localhost' (using password: YES) Spring boot 异常

javascript - AngularJS 按钮提交不起作用

java - 在 Spring 中从 Ajax 调用接收 Controller 中的参数

php - 如何使用 MUSTACHE 从 JSON 数据中获取 HTML?

python - 使用 pystache 渲染一个大文件