Search code examples
jspjstl

How to format nested JSP/JSTL tags


I am trying to set a JSTL variable to the value of another JSTL tag. How do I correctly format this? (Note: this is just a fragment, but where I am I having a problem)

<c:set var="jobs_url" value='<fmt:message key="jobs-widget.url" bundle="${vars}"/>'/>
<c:set var="jobs_id" value='<fmt:message key="jobs-widget.site_id" bundle="${vars}"/>'/>

<input type="hidden" name="site_id" value="${jobs_id}" />
<input type="button" onclick="my_func('${jobs_url}', '${jobs_id}');" value="">

The output I'm looking for would be something like this:

<c:set var="jobs_url" value='MY_URL'/>
<c:set var="jobs_id" value='MY_ID'/>
<input type="hidden" name="site_id" value="MY_ID" />
<input type="button" onclick="my_func('MY_URL', 'MY_ID');" value="">

Solution

  • You may not have a tag as the attribute of another tag. But you can store the output of the message tag in a variable using its var attribute:

    <fmt:message var="url" key="jobs-widget.url" bundle="${vars}"/>
    <fmt:message var="id" key="jobs-widget.site_id" bundle="${vars}"/>
    <input type="hidden" name="${id}" value="${url}" />
    

    That said, I wonder if you're not abusing resource bundles. Are you sure the URL and the site ID are really dependant on the locale?