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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2255352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:18:34  来源:igfitidea点击:

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

javaspringjspspring-mvcjstl

提问by wsams

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

谢谢

采纳答案by BCunningham

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 insidethe 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". Since it is not (it is "Hello World"), the test fails.

如果 message.message 的值实际上是值“true”,则只会评估为 true。由于它不是(它是“Hello World”),因此测试失败。

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

测试 #4 的格式也不正确(“非空”也需要在大括号内)...

回答by skaffman

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

您要查找的 URI 是 http://java.sun.com/jsp/jstl/core

回答by BalusC

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 hereand drop it in /WEB-INF/libafter removing any old JSTL libraries (jstl.jarandstandard.jar) and the eventual extracted contents (some poor tutorials namely suggests that you need to extract the .tldfiles and put it in classpath, this is wrong).

taglib URI 是错误的。这是旧的 JSTL 1.0 URI。你从哪里得到的?扔掉那本 10 年前的教程/书;)在这里获取JSTL 1.2/WEB-INF/lib在删除任何旧的 JSTL 库(jstl.jarstandard.jar)和最终提取的内容后将其放入(一些糟糕的教程即建议您需要提取.tld文件并将其放入它在类路径中,这是错误的)。

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:

最近的JSTL TLD 文档中指定了正确的 URI 。如果单击任何单个 JSTL 库,您将在文档顶部看到正确的 URI 示例,例如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" />

标准语法:

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

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}. You can learn more about EL in Java EE tutorial part II chapter 5and about JSTL in Java EE tutorial part II chapter 7. Good luck.

也就是说,您的测试实际上是错误的。您应该使用${message.message != null},${message.message != ''}${not empty message.message}。您可以在Java EE 教程第二部分第 5 章中了解有关 EL 的更多信息,在Java EE 教程第二部分第 7 章中了解有关 JSTL 的更多信息。祝你好运。