views:

43

answers:

2

Hi guys,

I'm working on converting an old Struts 1.x application to Spring MVC, and in some of the jsp pages the "bean:define" tag is being used to get a string from a resource bundle, and is then used later in the page:

<bean:define id="example_title"><fmt:message bundle="${example_Labels}" key="example_field"/></bean:define>

then later:

title="<%=example_title%>".

I'm not sure what the equivalent JSTL (or if it even should be JSTL) tag should be in order to do away with the Struts tag, can anyone offer a suggestion? I've tried playing with JSTL "set", and "jsp:useBean", but either they're the wrong way to go or I'm implementing them improperly.

Thanks for any suggestions!

+1  A: 

You can access your defined bean using the ${} notation

<%
  title = ${example_title}
%>

If you want to print it, you could use the <c:out> tag

<c:out value=${example_title}/>

Here's a quick link on JSTL

Tom
EL doesn't work inside *scriptlet* and you forgot the quotes in c:out.
BalusC
+1  A: 

Use the var attribute of the fmt:message.

<fmt:message bundle="${example_Labels}" key="example_field" var="example_title" />

This basically exports the value associated with the key into a page scoped variable named example_title. You can print it later in the page the usual EL way:

title="${example_title}"

Or if you're still on pre-JSP-2.0 where EL in template text isn't supported (consider upgrading..), then use <c:out> to display it:

title="<c:out value="${example_title}" />"
BalusC
I tried this way: I don't get any JSP errors when testing but the value of the title is blank when the page renders. It's almost like the var="example_title" doesn't get recognized. The project is pre-JSP-2.0, as it takes the "${example_title}" as a literal string value and uses it as the title.
Ryan P.
Then it's probably not in the same scope. Are you displaying it in an include page (`jsp:include`)? If so, add a `scope="request"` to the `fmt:message`. The include page uses the same request.
BalusC
Good thought, but it's within the same page. Just in case I tried using scope = page/request/session/application with the same result. Would it make a difference that the title= portion is within a <display:column> tag? So I tried: <display:column title="<c:out value='${example_title}' />">
Ryan P.
You should use `<display:column title="${example_title}">` instead.
BalusC
I'm assigning this as the correct answer. It turns out there's some issue the <display:column> tag has with rendering the output either with ${example_title} or <c:out value='${example_title}'/>"> Otherwise this does work as it should. If I can find a way around the <display:column> tag issue I'll post it here for others. Thanks for the input!
Ryan P.