开发者

Annotated Controllers in Spring, passing parameter to jsp

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/he开发者_开发技巧llo.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!


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜