rapt
rapt

Reputation: 12220

forwarding from spring controller to a jsp file

I am trying to forward my request from a Spring-MVC controller method - to a JSP page.

My controller method is supposed to handle an Ajax request. By forwarding the request to the JSP file, I want the response to the Ajax request to be the (dynamic) HTML output of the JSP file.

What I have tried:

public ModelAndView ajaxResponse(HttpServletRequest request, HttpServletResponse response) {

    request.setAttribute("command", "hello world");

    request.getRequestDispatcher("jspfile").forward(request, response);
    return null;    
}

This fails and I get "HTTP Status 404"

"jspfiles" is defined in a tiles config file to be directed to the actual jsp file. When I run the following method:

public String ajaxResponse(HttpServletRequest request, HttpServletResponse response) {

    request.setAttribute("command", "hello world");
    return "jspfile";
}

... I get the content of the file as my Ajax response - but JSP tags in that file are not parsed) - hence I conclude that my tiles definition is correct (???).

My JSP file looks like this:

<%=command%>

So I want to get as my Ajax response the string "hello world".

Could you show me an example code of how to achieve my purpose?

Specifically I need to know:

  1. What should be the controller method return type?

  2. What should be the controller method actual return value (if it matters)?

  3. How should I set the jsp file path in the request.getRequestDispatcher(...) so it would be recognized?

Upvotes: 3

Views: 7565

Answers (1)

user785262
user785262

Reputation:

Have a look at the controller example here: http://maestric.com/doc/java/spring/mvc

It's a bit out of date, but the concept of what you must do remains the same. Spring 3 has annotation-based ways to do a lot of what is in that example.

Upvotes: 3

Related Questions