如何使用AJAX调用具有指定URL的Controller的@RequestMapping方法

发布于 2024-12-02 04:11:27 字数 890 浏览 2 评论 0原文

我对 Spring 和 Portlet 非常陌生。我想使用 jqgrid 来显示一些列表。我试图调用控制器中的一个方法,该方法用 @RequestMapping 注释,但该方法没有被调用

我的控制器有以下方法

@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;
    }
}

我使用 AJAX 的 JSP 代码

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){
       ...
    }
});

当上面的 AJAX 代码执行时,我的方法没有被调用。

I'm very new to Spring and Portlet. I want to use jqgrid to show some list. I am trying to call a method in controller which is annoted with the @RequestMapping but the method is not being called

My Controller has 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 above AJAX code is executing my method is not called.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海拔太高太耀眼 2024-12-09 04:11:27

您在问题中提到了 Portlet。使用 Spring 和 portlet 与 servlet 有点不同。

因此,假设您有一个像这样的 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
    }
}

正如您所看到的,@ResourceMapping 由资源 ID 标识。资源映射的 URL 可以使用标准 portlet API 方法和类 createResourceURL()javax.portlet.ResourceURL 创建。

如果您更喜欢使用 portlet 标记库,您还可以使用 标记生成资源 URL。

您的视图可能类似于

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>
...

You mention Portlets in your question. Working with Spring and portlets is a bit different from servlets.

So, assuming you have a portlet like this

@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, the @ResourceMapping is identified by a resource ID. The url for the resource mapping can be created using the standard portlet API methods and classes createResourceURL() and javax.portlet.ResourceURL.

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

Your view might 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>
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文