1

我对 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 代码正在执行时,我的方法没有被调用。

4

1 回答 1

5

您在问题中提到了 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 标记库,也可以使用该<portlet:resourceRequest>标记生成资源 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>
...
于 2011-08-29T11:55:44.337 回答