Making an Ajax request in portlets for liferay 6

I want to make an ajax call inside my jsp file that calls the processAction method for the portlet based on the successful completion of the processAction process. I need to make another call to the serveResource method of the portlet. Give some examples.

+7
source share
4 answers

In portlets, the processAction () methods are automatically followed by the render method, and so the ajax response will be embedded with the HTML snippet generated by the render method. Therefore, writing ajax in portlets is a bit difficult.

Take a look at my blog.

http://ajax-and-portlets.blogspot.com/2011/09/ajax-best-practice-in-portlets.html

This gives the idea that it is best to implement ajax in portlets (for the JSR-168 and JSR-286 portlets).

If you want sample portlets, you can contact me through the contact information from the blog. I will be happy to help you.

Thanks Jignesh

+10
source

This question worked for me.

In principle, the controller

@Controller @RequestMapping("VIEW") // VIEW mapping (as opposed to EDIT) public class MyPortlet { @RenderMapping public String handleRenderRequest(RenderRequest request, RenderResponse response) { return "defaultRender"; } @ResourceMapping("myURL") public void handleMyResource(ResourceRequest request, ResourceResponse response) { OutputStream outStream; try { outStream = response.getPortletOutputStream(); ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(outStream, "Hello world!"); } catch (IOException ex) { // TODO : Do something with errors. } } } 

And JSP:

 <portlet:resourceURL id="myURL" var="myURL"/> <script type="text/javascript"> var urlink = "<%= myURL %>"; $.ajax({ url: urlink, cache: false, type: "POST", success: function(jsondata) { console.log(jsondata); } }); </script> 
+3
source

based on success message processAction This is not the right way to do this. When you call the portlet URL in response, you get a normal render response, so you get a page with all portlets. Instead, you should use the Portlet 2.0 Resource Maintenance feature and return your response as a resource.

0
source

You can check your sample portlet to call the serveResource and processAction methods. Ajax jQuery portlet

0
source

All Articles