views:

1044

answers:

3

First I have a Spring 3.0 controller with a method similar to the following.

I'm passing the view an object named "message" and hoping to print that message via the view if it has been set by the "doStuff" method.

@RequestMapping("/index")
public ModelAndView doStuff() {
    ModelAndView mav = new ModelAndView();
    Map<String, String> message = new HashMap<String, String>();
    message.put("message", "Hello World");
    mav.setViewName("pages/myView");
    mav.addObject("message", message);
    return mav;
}

The view is similar to the following,

<%@ page session="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<html>
<head>
<title>Test</title>
</head>
<body>
<c:if test="${message.message} != null">
    <div class="msg">test1: ${message.message}</div>
</c:if>
<c:if test="${message.message} != ''">
    <div class="msg">test2: ${message.message}</div>
</c:if>
<c:if test="${message.message}">
    <div class="msg">test3: ${message.message}</div>
</c:if>
<c:if test="not empty ${message.message}">
    <div class="msg">test4: ${message.message}</div>
</c:if>
<div class="msg">test5: ${message.message}</div>
</body>
</html>

So far, only "test5" is printing the message, but I only want to print the message if "${message.message}" is not null.

I've tried both "http://java.sun.com/jstl/core_rt" and "http://java.sun.com/jstl/core", but can't seem to get the "<c:if />" statements to work correctly.

Would anyone have any ideas as to what I'm doing wrong or a better way to do it?

Thanks

A: 

The URI you're looking for is http://java.sun.com/jsp/jstl/core

skaffman
+1  A: 

The taglib URI is wrong. It's the old JSTL 1.0 URI. Where did you get it from? Throw that 10-year old tutorial/book away ;) Go get JSTL 1.2 here and drop it in /WEB-INF/lib after removing any old JSTL libraries (jstl.jar and standard.jar) and the eventual extracted contents (some poor tutorials namely suggests that you need to extract the .tld files and put it in classpath, this is wrong).

The right URI is specified in the recent JSTL TLD documentation. If you click any of the individual JSTL libraries, you will see the right URI example in top of the document, e.g. JSTL core:

Standard Syntax:

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

XML Syntax:

    <anyxmlelement xmlns:c="http://java.sun.com/jsp/jstl/core" />

That said, your tests are in fact wrong. You should have used ${message.message != null}, ${message.message != ''} and ${not empty message.message}. You can learn more about EL in Java EE tutorial part II chapter 5 and about JSTL in Java EE tutorial part II chapter 7. Good luck.

BalusC
+1  A: 

Your first two <c:if> tags should look like this:

<c:if test="${message.message != null}">
    <div class="msg">test1: ${message.message}</div>
</c:if>
<c:if test="${message.message != ''}">
    <div class="msg">test2: ${message.message}</div>
</c:if>

Note the placement of the != and the } in the test attribute -- the condition needs to be inside the braces.

also, the test in #3:

<c:if test="${message.message}">
    <div class="msg">test3: ${message.message}</div>
</c:if>

Will only evaluate to true if the value of the message.message is in fact the value "true". Since it is not (it is "Hello World"), the test fails.

Test #4 is also formatted incorrectly (the "not empty" also needs to be inside the braces)...

BCunningham
Oh thanks a bunch - that's what it was.
wsams
@user272159: You only **really** need to get rid of the deprecated URI's and any old JSTL JAR's. Also see my answer.
BalusC