Mustache.js View for Spring Web MVC
Supports both jmustache and mustache.java
<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>${jmustache.version}</version>
</dependency>
<!-- mustache.java -->
<dependency>
<groupId>com.github.spullara.mustache.java</groupId>
<artifactId>compiler</artifactId>
<version>${mustache.java.version}</version>
</dependency>
<!-- jmustache -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.mustache.MustacheViewResolver">
<property name="suffix" value=""/>
<property name="cache" value="${TEMPLATE_CACHE_ENABLED}" />
<property name="templateFactory">
<bean class="org.springframework.web.servlet.view.mustache.jmustache.JMustacheTemplateFactory">
<property name="escapeHTML" value="true"/>
<property name="standardsMode" value="false"/>
<property name="templateLoader">
<bean class="org.springframework.web.servlet.view.mustache.jmustache.JMustacheTemplateLoader"/>
</property>
</bean>
</property>
</bean>
<!-- mustache.java -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.mustache.MustacheViewResolver">
<property name="suffix" value=""/>
<property name="cache" value="${TEMPLATE_CACHE_ENABLED}"/>
<property name="templateFactory">
<bean class="org.springframework.web.servlet.view.mustache.java.MustacheJTemplateFactory" />
</property>
</bean>
<bean id="messageSource" .../>
<!-- using mustache.java -->
<bean id="i18nMessageInterceptor"
class="org.springframework.web.servlet.view.mustache.java.LocalizationMessageInterceptor">
<property name="localeResolver" ref="..." />
</bean>
<!-- using jmustache -->
<bean id="i18nMessageInterceptor" class="org.springframework.web.servlet.view.mustache.jmustache.LocalizationMessageInterceptor">
<property name="localeResolver" ref="..." />
</bean>
Once you have a spring boot 3 web application running, register the mustache spring view dependency via the pom.xml:
<dependency>
<groupId>com.github.sps.mustache</groupId>
<artifactId>mustache-spring-view</artifactId>
<version>1.5-SNAPSHOT</version>
</dependency>
Create the localization implementation for mustache i18n lambda
public class LocalizationMessageInterceptor extends MustacheLocalizationMessageInterceptor {
public LocalizationMessageInterceptor() {
}
protected Object createHelper(final HttpServletRequest request) {
return new Mustache.Lambda() {
public void execute(Template.Fragment frag, Writer out) throws IOException {
LocalizationMessageInterceptor.this.localize(request, frag.execute(), out);
}
};
}
}
Register the localization implementation
@Component
public class InterceptorRegistration implements WebMvcConfigurer {
@Autowired
LocalizationMessageInterceptor localizationMessageInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localizationMessageInterceptor);
}
}
Ensure to add your messages.properties
files in your application.yaml:
spring:
messages:
basename: "messages.error.messages,messages.business.messages"
This means: having a directory messages
containing error
and business
directory, having messages bundle within it (messages.properties messages_uk.
properties etc)
Thanks to Eric White for forking this code base and providing the mustache.java implementation.