Reputation: 12220
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:
What should be the controller method return type?
What should be the controller method actual return value (if it matters)?
How should I set the jsp file path in the request.getRequestDispatcher(...)
so it would be recognized?
Upvotes: 3
Views: 7565
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