You can follow below steps to make AJAX call using resource URL
- Create Spring MVC portlet
- In view.jsp write below code to generate resource URL
- <portlet:resourceURL var='myInfo' id='myInfo' />
- Write AJAX call scriptin view.jsp
<script type="text/javascript">
loadMyInfo();
function loadMyInfo(){
var myInfoUrl = "<%=myInfo%>";
$.ajax({
url: myInfoUrl ,
type: 'GET',
datatype:'json',
success: function(data){
var obj = $.parseJSON(data);
alert(obj);
}
});
}
</script>
- Go to controller and write method as given below
@ResourceMapping(value="myInfo")
public void getMyInformation(ResourceRequest request, ResourceResponse response) throws IOException {
JSONObject json = JSONFactoryUtil.createJSONObject();
try{
ThemeDisplay themeDisplay =(ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
User user = themeDisplay .getUser();
response.setCharacterEncoding("UTF-8");
json.put("firstName",user != null ? user.getFirstName() :"");
json.put("lastName",user != null ? user.getLastName() :"");
response.getWriter().write(json.toString());
}catch (Exception e) {
}
}
- Now deploy your portlet and see the result ,On load it will make ajax call and render the data