2009-10-07 29 views
8

我有什麼應該是一個簡單的問題來解決,但我沒有運氣。JSP中不顯示模型中的對象Spring

在我的servlet-servlet.xml文件,我有以下豆(除其他):

<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" /> 

我的測試控制器看起來是這樣的:

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視圖長相像這樣:

<!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的值(一些字符串這裏),但只有我得到如下:

<!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對象模型是地方:

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 '[email protected]' 
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做過這種類型的事情,但是我必須在這裏忽略一些明顯的東西。有任何想法嗎?謝謝。

+0

你能提供你的web.xml嗎?和你的整個servlet-servlet.xml? 我測試過你的控制器,在我身邊有相同的視圖,並且正確渲染。 – rochb 2009-10-07 08:30:25

回答

13

您確定在您的JSP中啓用了對EL的評估嗎?我有時會遇到這個問題,它會以某種方式轉向。嘗試評估一個簡單的表達式,如${'test'},看看是否出現「測試」。

如果EL應該被禁用,你也可以嘗試使用頁面指令或其他東西來啓用它。

<%@ page isScriptingEnabled="true" isELIgnored="false" %> //of course it has to be FALSE 

(對不起,我不記得,如果這100%正確的。這可能是「isELEnabled」)

+2

不錯!在我的jsp的頂部添加<%@ page isELIoredored =「false」%>解決了我的問題。我不確定發生什麼事情來禁用EL。 哦,包括isScriptingEnabled屬性似乎打破了jsp,所以我只是刪除它。在JSP 2.0之前,似乎isScriptingEnabled屬性是有效的? 因此,總之,向我的jsp添加<%@ page isELgnored =「false」%>修復了這個問題。 謝謝,Moxn – labratmatt 2009-10-07 16:48:47

+1

不客氣。我很確定有一些方法可以在web.xml中配置它。根據[this] [1],您可以將come配置添加到您的web.xml中,以便爲您的所有JSP啓用EL。 [1] http://www.theserverside.com/discussions/thread.tss?thread_id=44097 – moxn 2009-10-08 08:39:31

1

我還沒有像Spring MVC那樣使用相當多的註解配置,所以我不確定所有使用你的設置自動完成的事情。我唯一的想法是:如果方法參數是一個ModelMap對象?我之前見過的例子都使用了ModelMap作爲參數類型。本頁的13.11.3部分是其中之一:http://static.springsource.org/spring/docs/2.5.6/reference/mvc.html

就像我說過的,我之前沒有使用過這種類型的自動配置 - 我稍微手工做了一些,並且將我的控制器從AbstractController或SimpleFormController類擴展。

+0

感謝您的時間。 我試過使用ModelMap並得到相同的結果。 – labratmatt 2009-10-07 04:50:03

4

我碰到了同樣的問題,比較EL工作2個類似的應用程序(一個後罰款和其他不),注意到我的tomcat 7上的問題取決於應用程序的web.xml中指定的webapp版本。

使用Web App 2.3的相同jsp顯示$ {someMessage}。 (順便說一句,這是你使用maven原型得到的:使用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> 
... 

同一個JSP使用Web應用程序2.4顯示正確的模型對象:

<?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"> 
... 

希望它可以幫助!

相關問題