Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an arbitrary object to String with EL + JSTL? (calling toString())

Tags:

java

jsp

jstl

el

Is there any way to call toString() on an object with the EL and JSTL? (I need the String representation of an enum as index in a map in a JSP EL expression.) I hoped something like ${''+object} would work like in java, but EL isn't that nice, and there does not seem to be any function that does it.

Clarification: I have a variable somemap that maps Strings to Strings, and I have a variable someenum that is an enumeration. I'd like to do something like ${somemap[someenum.toString()]}. (Of course .toString() does not work, but what does?)

like image 459
Hans-Peter Störr Avatar asked May 21 '10 16:05

Hans-Peter Störr


4 Answers

You just do it like this:

${object}

And it'll toString it for you.


edit: Your nested expression can be resolved like this:

<c:set var="myValue">${someenum}</c:set>
${somemap[myValue]}

The first line stringifies (using toString()) the ${someenum} expression and stores it in the myValue variable. The second line uses myValue to index the map.

like image 175
skaffman Avatar answered Nov 15 '22 06:11

skaffman


Couple things you can do.

One, you can use c:set -

<c:set var="nowAString">${yourVar}</c:set>

Another thing you can do is create your own EL function, call it toString, and then call that in JSTL. EL functions are basically static methods hooked up with a taglib file. Straightforward to do.

Edit:

Really? Did you actually, you know, try it?

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<%@taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <%
        pageContext.setAttribute("testDate", new java.util.Date());
        %>

        <c:set var="myVar">${testDate}</c:set>
        testDate = ${testDate}<br/>
        myVar = ${myVar}<br/>
        testDate Class = ${testDate.class}<br/>
        myVar Class = ${myVar.class}<br/>
    </body>
</html>

And JSP 2.0 tagfile and JSTL functions are trivial.

like image 38
Will Hartung Avatar answered Nov 15 '22 08:11

Will Hartung


I think in new versions of JSP api you can call methods, even with parameters!

I just tried ${statusColorMap[jobExecution.exitStatus.toString()]} and it works fine!

like image 3
Sebastien Lorber Avatar answered Nov 15 '22 07:11

Sebastien Lorber


The answer of Will Hartung should work. Here's a copy'n'paste'n'runnable SSCCE:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!doctype html>

<%!
    enum MyEnum {
        FOO, BAR
    }
%>
<%
    request.setAttribute("myEnum", MyEnum.FOO);
    java.util.Map<String, String> map = new java.util.HashMap<String, String>();
    map.put("FOO", "value of key FOO");
    map.put("BAR", "value of key BAR");
    request.setAttribute("map", map);
%>

<html lang="en">
    <head>
        <title>Test</title>
    </head>
    <body>
        <p>Map: ${map}
        <p>Enum: ${myEnum}
        <c:set var="myEnumAsString">${myEnum}</c:set>
        <p>Map value: ${map[myEnumAsString]}        
    </body>
</html>

This yields:

Map: {BAR=value of key BAR, FOO=value of key FOO}

Enum: FOO

Map value: value of key FOO

(scriptlets are just for quick prototyping, don't use them in real!)

like image 1
BalusC Avatar answered Nov 15 '22 06:11

BalusC