Dratha
Dratha

Reputation: 1

JSTL tags print in JSP from a data class

I have a buisness layer class to manage some data. There is a method which generate HTML-code in those code include a JSTL tag.

<h1 id=\"infoMsg\"><fmt:message key=\"uebersicht.infomsg\" /></h1>

This code will be included in JSP file like this:

<%=bl.getMessages()%>

But in that way it will be included as HTML request, but is there another way to include the code from a method For example like this:

<c:out value="${bl.getMessages()}"/>

Upvotes: 0

Views: 773

Answers (2)

Dratha
Dratha

Reputation: 1

I didn't find any solution. Therefore I have found a workaround:

<!-- set whole HTML source -->
<c:set var="message" value="${bl.messages}"></c:set>
<!-- set the key from a propertie file on a variable -->
<fmt:message key="uebersicht.infomsg" var="info"/>
<!-- get first part before the fmt tag -->
<c:set var="erst" value="${fn:substringBefore(message, '<fmt:message key')}" scope="application" />
<!-- get second part after the fmt tag -->
<c:set var="zweit" value="${fn:substringAfter(message, 'uebersicht.infomsg\" />')}" />
<!-- connect the three parts (first - key varible - second) -->
<fmt:message key="${fn:replace(erst,'?','')}"/>
${info}
<fmt:message key="${zweit}"/>

It works, but the two parts show question marks, the output is like this:

??? no information!! ???

<h1 id="infoMsg">???
no information!!
???</h1>

How could I replace the question marks?

Upvotes: 0

Amaynut
Amaynut

Reputation: 4271

The easiest way to include it in your JSP file is with the code you provided (supposing that the method is returning String with HTML format):

<%=bl.getMessages()%>

This code :

<c:out value="${bl.getMessages()}"/>

will not work because you declared your object inside a JSP scriplet like this:

<% MessageBL bl = new MessageBL(); %>

The reason is, JSP objects declared inside a scriplet are not accessible from an EL expression.

Upvotes: 1

Related Questions