JSP不在Spring中显示模型中的对象

lab*_*att 9 java spring jsp spring-mvc

我有什么应该是一个容易解决的问题,但我没有运气.

在我的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" />
Run Code Online (Sandbox Code Playgroud)

我的测试控制器看起来像这样:

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";
     }

}
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

所以,当我查看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>
Run Code Online (Sandbox Code Playgroud)

当我启动日志记录时,我看到我的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
Run Code Online (Sandbox Code Playgroud)

显然,我的视图已正确映射,但我似乎无法访问视图中添加到请求中的模型对象.我过去曾多次使用Spring MVC做过这种事情,但我必须在这里遗漏一些明显的东西.有任何想法吗?谢谢.

mox*_*oxn 14

您确定在JSP中启用了对EL的评估吗?我有时遇到了问题,它以某种方式转变了.尝试评估一个简单的表达式${'test'},看看是否出现'test'.

如果应禁用EL,您还可以尝试使用页面指令或其他内容启用它.

<%@ page isScriptingEnabled="true" isELIgnored="false" %> //of course it has to be FALSE
Run Code Online (Sandbox Code Playgroud)

(对不起,我不记得这100%是否正确.可能是'isELEnabled')

  • 太好了!将<%@ page isELIgnored ="false"%>添加到我的jsp顶部修复了我的问题.我不确定禁用EL会发生什么.哦,包括isScriptingEnabled属性似乎打破了jsp,所以我就把它删除了.似乎isScriptingEnabled属性在JSP 2.0之前是有效的吗?因此,总之,将<%@ page isELIgnored ="false"%>添加到我的jsp中解决了这个问题.谢谢,Moxn (2认同)

小智 5

我遇到了同样的问题,在比较了 2 个类似的应用程序(一个 EL 工作正常,另一个没有)后,注意到我的 tomcat 7 上的问题取决于应用程序的 web.xml 中指定的 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>
...
Run Code Online (Sandbox Code Playgroud)

使用 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">
...
Run Code Online (Sandbox Code Playgroud)

希望它有帮助!