Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use messages with freemarker in spring mvc?

Tags:

In a .jsp I would use:

<fmt:message key="welcome.title"/>

to display a message from my messages.properties file.

How would I do this with freemarker ?

like image 400
Blankman Avatar asked Jul 01 '10 02:07

Blankman


People also ask

How do you comment out a FreeMarker?

Comments: <#-- and --> Comments are similar to HTML comments, but they are delimited by <#-- and -->. Comments will be ignored by FreeMarker, and will not be written to the output.

Which is the spring boot starter that has to be added to include FreeMarker template engine?

The spring-boot-starter-freemarker is starter for building Spring MVC applications with FreeMarker. The spring-boot-starter-jdbc is a starter for using JDBC in Spring Boot. This is City bean class. It contains item id, name, and population.

What is spring FreeMarker?

FreeMarker is a Java based template engine from the Apache Software Foundation. Like other template engines, FreeMarker is designed to support HTML web pages in applications following the MVC pattern. This tutorial illustrates how to configure FreeMarker for use in Spring MVC as an alternative to JSP.

What is FreeMarker spring boot?

FreeMarker is a Template Engine. it is provided by Apache as an open source Java library. The FreeMarker reads sample files and combines them with Java objects to generate an output text (html, email, ..).


2 Answers

Import Spring Macro

<#import "/spring.ftl" as spring/>

Then

<@spring.message "yourMessageKeyGoesHere"/>

But you need to register ResourceBundleMessageSource

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages"/>
</bean>

Keep in mind MessageSource must be called messageSource

like image 190
Arthur Ronald Avatar answered Sep 19 '22 12:09

Arthur Ronald


@Blankman

No, you don't have to import this manually in each template. You can set an auto_import property in your freemarker settings as showed below.

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
   ...

   <property name="freemarkerSettings">
        <props>
            <prop key="auto_import">spring.ftl as spring</prop>
        </props>
   </property>
</bean>
like image 20
matthaeus Avatar answered Sep 19 '22 12:09

matthaeus