Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning outcome of another JSTL tag as value of one JSTL tag

Tags:

java

jsp

jstl

el

I've got this, which is working:

<c:choose>
    <c:when test="${sometest}">
        Hello, world!
    </c:when>
    <c:otherwise>
        <fmt:message key="${page.title}" />
    </c:otherwise>
</c:choose>

And I want to change it to this:

<c:choose>
    <c:when test="${sometest}">
        <c:set var="somevar" scope="page" value="Hello, world!"/>
    </c:when>
    <c:otherwise>
        <c:set var="somevar" scope="page" value="<fmt:message key="${page.title}">"
    </c:otherwise>
</c:choose

But of course the following line ain't correct:

<c:set var="somevar" scope="page" value="<fmt:message key="${page.title}">"

How can I assign to the somevar variable the string resulting from a call to fmt:message?

like image 989
NoozNooz42 Avatar asked May 31 '10 18:05

NoozNooz42


People also ask

Which of the following JSTL tags can be used to assign a new property value to an object?

<c:set> Tag Attributes This attribute is the property name of the object to set the value specified by the target attribute.

Which tag is used in JSTL to show the output?

The <c:out> tag is similar to JSP expression tag, but it can only be used with expression. It will display the result of an expression, similar to the way < %=... % > work.

Which tag is used to declare variable and initialize with some value expression in JSTL library?

The <c:set> tag is used for declaring scoped variables in JSP. We can also declare the name of the variable and its value in the var and value attributes respectively.

Which JSTL provides support for string manipulation in JSTL?

The JSTL core tag provide variable support, URL management, flow control, etc. The URL for the core tag is http://java.sun.com/jsp/jstl/core. The prefix of core tag is c. The functions tags provide support for string manipulation and string length.


2 Answers

The fmt:message has a var attribute as well which does effectively what you want.

 <fmt:message key="${page.title}" var="somevar" />

That's all. Bookmark the JSTL tlddoc, it may come in handy.

like image 59
BalusC Avatar answered Sep 22 '22 06:09

BalusC


It is also possible to specify the value to set using the contents of the body, rather than through the value attribute:

<c:set var="somevar" scope="page">
  <fmt:message key="${page.title}"/>
</c:set>
like image 32
Timo Westkämper Avatar answered Sep 22 '22 06:09

Timo Westkämper