Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSTL Core fmt:message Tag Dynamic message using bundle/properties file

I understand the usage of standard fmt:message Tag i.e we define something like this in the JSP:

<fmt:setBundle basename="ResourceBundles.ValidationErrorMessages" var="errorMessages" />
<fmt:message key="${error.value}" bundle="${errorMessages}" />

Suppose error.Value = "MQ2009"

My properties file named "ValidationErrorMessages" has following entry

MQ2009 = MQ time out

Now my requirement is to have something like

MQ2009 = Mq timeout happened for message {messagename}.

Can I define the messagename variable dynamically? I.e at runtime, messagename will be available in request scope and it should be substituted in the properties file.

How can I do this? Do I need a custom tag or does Java EE provides this feature which I am not aware off?

like image 357
Metalhead Avatar asked Oct 16 '12 04:10

Metalhead


2 Answers

You can define properties like

MQ2009 = Mq timeout happened for {0}

and then

<fmt:message key="MQ2009" var="val" >
   <fmt:param value="${valueComingFromSomeParameter}"/>
</fmt:message>

and then

<c:out value="${val}"/>
like image 156
jmj Avatar answered Oct 07 '22 01:10

jmj


In you properties file

MQ2009 = Mq timeout happened for message {0}

Then you can nest a <fmt:param value="${messagename}"/> tag withing <fmt:message tag, where messagename is in request scope.

like image 44
Bhesh Gurung Avatar answered Oct 07 '22 00:10

Bhesh Gurung