Skip to content

java spring framework mvc view for Mustache.js templates

Notifications You must be signed in to change notification settings

DNWEIJ/mustache-spring-view

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Supports both jmustache and mustache.java

Build Status Coverage Status

Maven Dependency

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

Spring Configuration

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

Localization Support

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

Spring Boot 3 - Configuration

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

Thanks to Eric White for forking this code base and providing the mustache.java implementation.

About

java spring framework mvc view for Mustache.js templates

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%