View Full Version : ModelAndView
scott
Aug 13th, 2007, 10:07 PM
In a JSP page, the layout is like this:
<table>
<tr>
<td> left panel </td> <td> <div id="rightPanel"> </div> </td>
</tr>
</table>
A handler method of a controller (in Sprng MVC) named testHandler returns the following:
return new ModelAndView("rightPanel.jsp")
Now I want the rightPanel.jsp to be displayed specifically within the
<div id="rightPanel"> </div>
How can I do it?
Thanks
Scott
Jörg Heinicke
Aug 13th, 2007, 11:10 PM
Iframes or AJAX. But both do not just work by returning "rightPanel.jsp". They do trigger a new request to the server which has to be mapped to your testHandler which then returns the content of your JSP.
Joerg
scott
Aug 14th, 2007, 12:01 AM
Thanks for the response.
1)Assume from the client's javascript:
var http = getHTTPObject();
http.open("GET", "test.htm?cmd=testHandler", true);
http.onreadystatechange = function()
{ if (http.readyState == 4)
{ doSomethingWith(http.responseText); }
}
http.send(null);
}
//test.htm will refer to the testController.java class.
2) Spring MVC testHandler(...)
return new ModelAndView("test.jsp")
3) How should the ajax intercept the test.jsp and extract the content?[/B]
Suppose the test.jsp layout is:
<table>
<tr>
<td>
some text
</td>
</tr>
</table>
4)Once step 3 can be done, I would assume I can use
document.getElementById("rightPanel").appendChild(....) to place the content to the said
<div id="rightPanel"> </div>
Can you give more detailed instruction?
Anyway, thanks.
Scott
The problem is I do not know what to do next, after the
return new ModelAndView("test.jsp");
Can I say that the
http.onreadystatechange = function()
{ if (http.readyState == 4)
{ doSomethingWith(http.responseText); }
will intercept the returned ModelAndVew("test.jsp")?
If so, can I coded the doSomethingWith function like:
function doSomethingWith(content){
document.getElementById("rightPanel").appendChild(content) ;
}
Jörg Heinicke
Aug 14th, 2007, 11:56 AM
I'm a bit confused from your wording. What does intercept mean in this case?
From what I understand you only miss the last bit of the picture: interpreting the response and handle it. You even seem to have set up a handler function correctly but you pass responseText to it. This would make it necessary to parse the text and get XML or DOM out of it. Instead you should just use responseXML (see XMLHttpRequest (http://www.w3.org/TR/XMLHttpRequest/) object) directly which should hold your DOM on which you can then operate and append it to your element.
You might also consider using an AJAX lib instead of writing such code yourself.
Joerg
scott
Aug 14th, 2007, 12:16 PM
Thanks for the response.
What my problem may be explained by this chart:
home.jsp (hosts: <div id="rightPanel"> </div>) -->
http.open("GET", "test.htm?cmd=testHandler", true); --> access Spring MVC testController.java -->
-->Method: testHandler(...) --> return new ModelAndView("test.jsp")
Now can the http.responseText of ajax get the returned test.jsp?
If so, I'll use:
document.getElementById("rightPanel").appendChild(http.responseText ) ;
But if Not, how can I get the returned test.jsp and display it within the
<div id="rightPanel"> </div>
(The <div id="rightPanel"> </div> is hosted by the page home.jsp from where the above ajax call is made.))
Thanks
Scott
Jörg Heinicke
Aug 14th, 2007, 03:13 PM
As I said you can't do just appendChild(http.responseText) since responseText only contains the unparsed text, no XML structure. You have to use http.responseXML to get a DOM. On this DOM you can now operate and append your nodes to the DOM of home.jsp. But I don't think appendChild(http.responseXML) as is will work. I guess you get an error saying that the particular node has already an owning document. You might need to clone the nodes. But I can't be of much help in this regard. Nothing done on this level lately and the rest is only from some dark memories.
Joerg
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.