views:

1194

answers:

2

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):

<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:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
<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:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
<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:

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.

A: 

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.

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.

Kaleb Brasee
Thanks for the time.I've tried using a ModelMap and had the same result.
labratmatt
+1  A: 

Hi.

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.

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

(Sorry, I can't remember if this 100% correct. It might be 'isELEnabled')

moxn
Nice! Adding <%@ page isELIgnored="false" %> to the top of my jsp fixed my problem. I'm not sure what's happening to disable EL.Oh, including the isScriptingEnabled property seems to break the jsp, so I just removed it. It seems that the isScriptingEnabled property was valid pre JSP 2.0?So, in summary, adding <%@ page isELIgnored="false" %> to my jsp fixed the issue.Thanks, Moxn
labratmatt
You're welcome. I'm pretty sure that there is some way to configure this in the web.xml. Acoording to [this][1] you can add come configuration to your web.xml to enable EL for all your JSPs.[1]http://www.theserverside.com/discussions/thread.tss?thread_id=44097
moxn