I want to make an ajax call inside my jsp file which calls processAction method of a portlet, based on the success message from processAction method i need to make another call to serveResource method of portlet,please provide some examples..
Creating a function that calls the AJAX request and using this function in setInterval() and set Interval for 5 sec. Now the function executes every 5 seconds and fetches new data from the server. It repeatedly executes the function even when the previous AJAX request is not successfully executed and return.
Approach 1: In this approach, we will use the XMLHttpRequest object to make Ajax call. The XMLHttpRequest() method which create XMLHttpRequest object which is used to make request with server. Syntax: var xhttp = new XMLHttpRequest();
This question worked for me.
Basically, 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 the 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>
In portlets, processAction() methods are automatically followed by render method and hence ajax response will get embedded with HTML fragment generated by render method. So writing ajax in portlets is a bit tricky.
Have a look at this blog of mine.
http://ajax-and-portlets.blogspot.com/2011/09/ajax-best-practice-in-portlets.html
It gives an insight view of what's the best practice to implement ajax in portlets (for both JSR-168 and JSR-286 portlets).
In case you want sample portlets, you can contact me through the contact details from the blog. I'll be happy to help you.
Thanks Jignesh
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With