简体   繁体   English

如何将对象从Spring 3.0控制器传递到JSP视图并使用JSTL进行测试

[英]How to pass an object from Spring 3.0 controller to JSP view and test with JSTL

First I have a Spring 3.0 controller with a method similar to the following. 首先,我有一个Spring 3.0控制器,其方法类似于以下方法。

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. 我正在向视图传递一个名为“message”的对象,如果已经通过“doStuff”方法设置,则希望通过视图打印该消息。

@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. 到目前为止,只有“test5”正在打印消息,但我只想在“$ {message.message}”不为空时打印消息。

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. 我试过“ http://java.sun.com/jstl/core_rt ”和“ http://java.sun.com/jstl/core ”,但似乎无法得到“<c:if />“语句正常工作。

Would anyone have any ideas as to what I'm doing wrong or a better way to do it? 谁会对我做错了什么或更好的方法有任何想法?

Thanks 谢谢

The taglib URI is wrong. taglib URI是错误的。 It's the old JSTL 1.0 URI. 这是旧的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 ). 扔掉那个有10年历史的教程/书;) 在这里获取JSTL 1.2并在删除任何旧的JSTL库( jstl.jar standard.jar )以及最终提取的内容(一些穷人)之后将其放入/WEB-INF/lib教程即建议您需要提取.tld文件并将其放在类路径中,这是错误的 )。

The right URI is specified in the recent JSTL TLD documentation . 正确的URI在最近的JSTL TLD文档中指定。 If you click any of the individual JSTL libraries, you will see the right URI example in top of the document, eg JSTL core : 如果单击任何单个JSTL库,您将在文档顶部看到正确的URI示例,例如JSTL核心

Standard Syntax: 标准语法:

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

XML Syntax: XML语法:

  <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} . 您应该使用${message.message != null}${message.message != ''}${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 . 您可以在Java EE教程第二部分第5章中了解有关EL的更多信息,以及有关Java EE教程第二部分第7章中的 JSTL的更多信息。 Good luck. 祝好运。

您正在寻找的URI是http://java.sun.com/jsp/jstl/core

Your first two <c:if> tags should look like this: 您的前两个<c:if>标记应如下所示:

<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: 另外,#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". 如果message.message的值实际上是值“true”,则仅评估为true。 Since it is not (it is "Hello World"), the test fails. 由于它不是(它是“Hello World”),测试失败。

Test #4 is also formatted incorrectly (the "not empty" also needs to be inside the braces)... 测试#4的格式也不正确(“非空”也需要在大括号内)...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM