How to call the @RequestMapping method of a controller with a specified URL using AJAX

I am very new to Spring and the portlet. I want to use jqgrid to display some list. I am trying to call a method in a controller that is being written using @RequestMapping, but the method is not being called

My controller has the following method

@Controller(value = "myController") public class MyController { @RequestMapping(value="/myURL",method=RequestMethod.GET) public @ResponseBody MyDTO initItemSearchGrid(RenderResponse response, RenderRequest request){ MyDTO myDto=new MyDTO(); return myDto; } } 

My JSP code using AJAX

 var urlink="/myURL"; /* myURL is the exact String written in value Attribute of resourceMapping in Controller*/ $.ajax({ url :urlink, cache: false, data:$('#myForm').formSerialize(), dataType: "json", type: "GET", contentType: "application/json; charset=utf-8", success: function(jsondata){ ... } }); 

When the above AJAX code is executed, my method is not called.

+1
source share
1 answer

You mention portlets in your question. Working with Spring and portlets is slightly different than servlets.

So, suppose you have such a portlet

 @Controller @RequestMapping("VIEW") // VIEW mapping (as opposed to EDIT) public class MyPortlet { @RenderMapping public ModelAndView handleRenderView(RenderRequest request, RenderResponse response) { ResourceURL resourceUrl = response.createResourceURL(); resourceUrl.setResourceID("myResource"); // this is the id used to reference a @ResourceMapping ModelAndView ret = new ModelAndView("myPortlet"); ret.addObject("resourceUrl", resourceUrl.toString()); return ret; } @ResourceMapping("myResource") public void handleMyResource(ResourceRequest request, ResourceResponse response) { OutputStream out = response.getPortletOutputStream(); // write whatever to output } } 

As you can see, @ResourceMapping is identified by a resource identifier. A resource mapping URL can be created using standard API methods and the createResourceURL() and javax.portlet.ResourceURL .

If you prefer to use the taglibrary of the portlet, you can also create a resource URL using the <portlet:resourceRequest> .

Your opinion may look something like this.

myPortlet.jsp

 ... <script> $.ajax({ url :${resourceUrl}, cache: false, data:$('#myForm').formSerialize(), dataType: "json", type: "GET", contentType: "application/json; charset=utf-8", success: function(jsondata){ ......... ......... ......... } }); </script> ... 
+5
source

All Articles