Assign java object from response to a custom JSP tag communicated using AJAX

让人想犯罪 __ 提交于 2019-12-25 02:17:27

问题


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:


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.



来源:https://stackoverflow.com/questions/6147831/assign-java-object-from-response-to-a-custom-jsp-tag-communicated-using-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!