Assign java object from response to a custom JSP t

2019-09-05 16:14发布

I have a button in JSP, when clicked, goes to a servlet stores a java object using request.setAttribute("attr", object) and forwards to another page. In that page, I am using a custom JSP tag which gets this attribute and displays some values. Now I want this all to happen using AJAX. I want to have only one page which submits a form and receives an object to be used by custom JSP tag in the same page. How do I do this ? Is there a reliable library for that ?

From what I see, in ajax I can send response by printing it which means I must send an XML back. If I do so, how do I convert it back to java object so that JSP tag can use it ?

1条回答
对你真心纯属浪费
2楼-- · 2019-09-05 16:58

Assuming your custom tag just displays some data, you could submit your form via ajax and return HTML. Then just push that HTML into a div. The HTML that is returned would be what your JSP with the custom tag renders, jQuery can help you out...

pseudo code:

$.post(url, params, function(htmlData) {
   $('#results').html(htmlData);
}); 

On the server side, nothing would really change from the way you are handling it now. If you don't need to post a form but just submit some data via ajax, you could also use the load() function.

Your ajax request will only return XML if you return XML. The response type is entirely up to you.

查看更多
登录 后发表回答