views:

67

answers:

3

Hello,

I'm trying to complete that tutorial with annotated controllers. I got stuck on the Step 2. Here is what they have for Simple Controllers:

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String now = (new Date()).toString();
    logger.info("Returning hello view with " + now);

    return new ModelAndView("WEB-INF/jsp/hello.jsp", "now", now);
}

I tried to replace it with

    @RequestMapping(method = RequestMethod.GET)
public String showUserForm(ModelMap model)
{
    String now = (new Date()).toString();
    logger.info("Returning hello view with " + now);
    model.addAttribute("now", now);
    return "WEB-INF/jsp/hello.jsp";
}

But that parameter "now" is not read in hello.jsp (it could be accessed by the first link, I can't paste html here).

How can I transfer that parameter to hello.jsp?

Thanks!

A: 

Your showUserForm() method does not return a ModelAndView object. This is how the view gets data from your controller.

Bernard
It's wrong, see http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-arguments
axtavt
@Bernard: Incorrect. The `ModelMap` is passed to the view before rendering.
skaffman
Thank you. Learned something new. Still doesn't explain why it isn't working.
Bernard
A: 

Generally, your views are resolved via a view resolver, and its default configuration includes the WEB-INF/jsp as prefix, and .jsp as suffix.

So the view name (the string the you should return) is just "hello".

If this is not the case, please share your dispatcher-servlet.xml and the jsp itself.

Bozho
A: 

The problem might be you're trying to use a scriptlet to output the data in your JSP. Like this:

<p><%= now %></p>

This will not work as the variable is not in the page scope but in the request scope. You should use EL to output the value, like this:

<p>${now}</p>

Or, if you want to use scriptlets, do something like this:

<p><%= pageContext.findAttribute("now") %></p>

If you could post your JSP code this would be quite helpful.

Philipp Jardas
I guess that if it has worked before, he is using the proper construct.
Bozho
Did it actually work with the first example? I'm not understanding this from the OP.
Philipp Jardas
Scriptlet scope should not be confused with page scope.
BalusC
After I tried using <%= pageContext.findAttribute("now") %> it started to work. But it's strange that <c:out value="${now}"/> works in Windows but not in Linux. Do you know what could be the problem?
for unknown reasons in linux I should add isELIgnored="false" to my page directove for jstl to work