java JSP 不显示来自 Spring 模型的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1529184/
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
JSPs not displaying objects from model in Spring
提问by labratmatt
I have what should be an easy issue to solve, but I'm having no luck.
我有一个应该很容易解决的问题,但我没有运气。
In my servlet-servlet.xml file, I have the following beans (in addition to others):
在我的 servlet-servlet.xml 文件中,我有以下 bean(除了其他 bean):
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="com.servlet.web" />
My test controller looks like this:
我的测试控制器如下所示:
package com.servlet.web;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController
{
protected final Log log = LogFactory.getLog(getClass());
@RequestMapping("/test")
public String methodName(Map<String, Object> map) {
map.put("someMessage", "some string here");
return "test";
}
}
My jsp view looks like this:
我的 jsp 视图如下所示:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>servlet.com</title>
</head>
<body>
${someMessage}
</body>
</html>
So, when I view the jsp, I'd expect the value of someMessage (some string here), but I only get the following:
所以,当我查看 jsp 时,我希望得到 someMessage 的值(这里是一些字符串),但我只得到以下信息:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>servlet.com</title>
</head>
<body>
${someMessage}
</body>
</html>
When I crank up the logging, I see that my someMessage object is being place in the model:
当我启动日志记录时,我看到我的 someMessage 对象被放置在模型中:
22:21:17,425 DEBUG DispatcherServlet:852 - DispatcherServlet with name 'servlet' determining Last-Modified value for [/servlet/access/test]
22:21:17,426 DEBUG DefaultAnnotationHandlerMapping:183 - Mapping [/test] to handler 'com.servlet.web.TestController@762fef'
22:21:17,426 DEBUG DispatcherServlet:868 - Last-Modified value for [/servlet/access/test] is: -1
22:21:17,426 DEBUG DispatcherServlet:700 - DispatcherServlet with name 'servlet' processing GET request for [/servlet/access/test]
22:21:17,427 DEBUG HandlerMethodInvoker:158 - Invoking request handler method: public java.lang.String com.servlet.web.TestController.methodName(java.util.Map)
22:21:17,427 DEBUG DispatcherServlet:1070 - Rendering view [org.springframework.web.servlet.view.JstlView: name 'test'; URL [/WEB-INF/jsp/test.jsp]] in DispatcherServlet with name 'servlet'
22:21:17,427 DEBUG JstlView:328 - Added model object 'someMessage' of type [java.lang.String] to request in view with name 'test'
22:21:17,428 DEBUG JstlView:237 - Forwarding to resource [/WEB-INF/jsp/test.jsp] in InternalResourceView 'test'
22:21:17,429 DEBUG DispatcherServlet:666 - Successfully completed request
Obviously, my view is mapped correctly, but I can't seem to access model objects added to the request in the view. I've done this type of thing with Spring MVC many times in the past, but I must be missing something obvious here. Any ideas? Thanks.
显然,我的视图映射正确,但我似乎无法访问添加到视图中请求的模型对象。过去我已经多次使用 Spring MVC 做过这种类型的事情,但我一定在这里遗漏了一些明显的东西。有任何想法吗?谢谢。
回答by moxn
Are you sure that evaluation of EL is enabled in your JSP? I sometimes had the problem, that it got turned of somehow. Try evaluating a simple expression like ${'test'}and see if 'test' appears.
您确定在您的 JSP 中启用了对 EL 的评估吗?我有时会遇到问题,它以某种方式被关闭。尝试评估一个简单的表达式${'test'},看看是否出现“test”。
You can also try enabling it with page directives or something else if EL should be disabled.
如果应该禁用 EL,您还可以尝试使用页面指令或其他内容启用它。
<%@ page isScriptingEnabled="true" isELIgnored="false" %> //of course it has to be FALSE
(Sorry, I can't remember if this 100% correct. It might be 'isELEnabled')
(抱歉,我不记得这是否 100% 正确。它可能是“isELEnabled”)
回答by fcorsino
I ran into the same problem, and after comparing 2 similar apps (one with EL working fine and the other not), noticed that the problem on my tomcat 7 depended on the webapp version specified in the web.xml of the application.
我遇到了同样的问题,在比较了 2 个类似的应用程序(一个 EL 工作正常,另一个没有)后,注意到我的 tomcat 7 上的问题取决于应用程序的 web.xml 中指定的 webapp 版本。
The same jsp using Web App 2.3 displays ${someMessage}. (BTW, this is what you get using maven archetype:generate with archetypeArtifactId=maven-archetype-webapp).
使用 Web App 2.3 的相同 jsp 显示 ${someMessage}。(顺便说一句,这就是你使用 maven archetype:generate with archetypeArtifactId=maven-archetype-webapp 得到的结果)。
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
...
The same jsp using Web App 2.4 displays the model object properly:
使用 Web App 2.4 的相同 jsp 正确显示模型对象:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="webapp-id" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
...
Hope that it helps!
希望它有帮助!
回答by Kaleb Brasee
I haven't used quite as much annotation configuration as you have with Spring MVC, so I'm not sure of all the things that are being automatically done with your setup. My only thought is this: should the method parameter be a ModelMap object? The examples I've seen before have all used ModelMap as the parameter type. Section 13.11.3 of this page is one of them: http://static.springsource.org/spring/docs/2.5.6/reference/mvc.html.
我没有像在 Spring MVC 中那样使用那么多的注释配置,所以我不确定在你的设置中自动完成的所有事情。我唯一的想法是:方法参数应该是一个 ModelMap 对象吗?我之前看到的例子都使用 ModelMap 作为参数类型。此页面的第 13.11.3 节是其中之一:http://static.springsource.org/spring/docs/2.5.6/reference/mvc.html 。
Like I said, I haven't used this type of auto-configuration before -- I do it slightly more manually, and extend my controllers from something like an AbstractController or a SimpleFormController.
就像我说的那样,我以前没有使用过这种类型的自动配置——我稍微手动一点,并从 AbstractController 或 SimpleFormController 之类的东西扩展我的控制器。

