JSP不在Spring中显示模型中的对象 [英] JSPs not displaying objects from model in Spring
问题描述
我有一个容易解决的问题,但我没有运气。
I have what should be an easy issue to solve, but I'm having no luck.
在我的servlet-servlet.xml文件中,我有以下内容bean(除了其他人):
In my servlet-servlet.xml file, I have the following beans (in addition to others):
<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";
}
}
我的jsp视图如下所示:
My jsp view looks like this:
<!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>
所以,当我查看jsp时,我期望someMessage的值(这里有一些字符串) ,但我只得到以下内容:
So, when I view the jsp, I'd expect the value of someMessage (some string here), but I only get the following:
<!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>
当我启动日志记录时,我看到我的someMessage对象放在模型中:
When I crank up the logging, I see that my someMessage object is being place in the model:
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
显然,我的视图已正确映射,但我可以似乎访问视图中添加到请求的模型对象。我过去曾经多次使用Spring MVC做过这种事情,但我必须在这里遗漏一些明显的东西。有任何想法吗?谢谢。
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.
推荐答案
您确定在JSP中启用了对EL的评估吗?我有时遇到了问题,它以某种方式转变了。尝试评估一个简单的表达式,如 $ {'test'}
,看看是否出现'test'。
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.
你可以如果EL应该被禁用,也尝试使用页面指令或其他东西启用它。
You can also try enabling it with page directives or something else if EL should be disabled.
<%@ page isScriptingEnabled="true" isELIgnored="false" %> //of course it has to be FALSE
(对不起,我不记得这100%这可能是'isELEnabled')
(Sorry, I can't remember if this 100% correct. It might be 'isELEnabled')
这篇关于JSP不在Spring中显示模型中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!