简体   繁体   English

春季MVC @ResponseBody

[英]Spring MVC @ResponseBody

I have a controller that extends the MultiActionController and that does not use annotation. 我有一个扩展MultiActionController且不使用注释的控制器。 Everything is configured by xml. 一切都由xml配置。

Is there a way to reproduce this method in a MultiActionController? 有没有办法在MultiActionController中重现此方法?

    @RequestMapping(value = "/products", method = RequestMethod.GET)
        public @ResponseBody List<Product> products() {
            return product.getList();
        }

The problem I'm facing is how to return the list in the @ResponeBody. 我面临的问题是如何在@ResponeBody中返回列表。

My methods looks like these: 我的方法如下所示:

public ModelAndView login(HttpServletRequest request,HttpServletResponse response) throws ServletRequestBindingException {
    return new ModelAndView("login", model);
}

Sort of an open ended question, what I mean is, what response type do you want, json, xml, plain text, etc., 有点悬而未决的问题,我的意思是,您想要哪种响应类型,json,xml,纯文本等,

If you are not confined to using this specific controller you could setup a new controller specifically for RESTful responses such as using @ResponseMapping as you are trying to achieve at the beginning of your question. 如果您不限于使用此特定控制器,则可以针对问题的开头设置一个专门针对RESTful响应的新控制器,例如使用@ResponseMapping

You could also go the straight servlet route if you are tied to this specific controller, and don't have the ability to use annotations. 如果您绑定到此特定控制器,并且没有使用注释的能力,那么也可以采用servlet直路。 For example: 例如:

public void generateProductList(HttpServletResponse response) {
    OutputStream os = response.getOutputStream();
    // This is where you would massage the data into the response type you want
    String responseBody = generateResponseBody(productList);
    os.write(responseBody.getBytes());
    os.flush();
}

I'm assuming if you're writing directly to the response body, this will be used in some sort of AJAX call. 我假设如果您直接写到响应主体,则将在某种AJAX调用中使用它。 If this is the case, I would recommend converting the list into JSON using the Jackson (or similar) library, and using either of the methods I just mentioned to write the response. 如果是这种情况,我建议您使用Jackson(或类似的库)库,并使用我刚才提到的任何一种方法将列表转换为JSON来编写响应。 If JSON isn't an option, you could easily convert the list into a CSV response, then use javascript to parse the CSV to do something with. 如果不是JSON选项,则可以轻松地将列表转换为CSV响应,然后使用javascript解析CSV以执行某些操作。

If you want to get the list back to where it is called from you can do something like this: 如果要将列表返回到调用它的位置,可以执行以下操作:

public ModelAndView login(HttpServletRequest request,HttpServletResponse response) throws ServletRequestBindingException {

   ModelMap model=new ModelMap();

   List<Product> productList=product.getList();

   model.add("productList",productList);
   return new ModelAndView("login", model);
}

For example you have called this from a jsp then you can retrieve the data inside jsp by writing this : 例如,您从一个jsp调用了它,然后您可以通过编写this来检索jsp中的数据:

   {productList}

Now by using jstl for loop you can iterate the list. 现在,通过使用jstl for循环,您可以迭代列表。

public ModelAndView products(HttpServletRequest request,HttpServletResponse response) {
    final List<Product> products = product.getList();
    return new ModelAndView(new AbstractView() {

        @Override
        protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
            ByteArrayOutputStream baos = this.createTemporaryOutputStream();
            byte[] b = generateBytes(products)
            baos.write(b);
            this.writeToResponse(response, baos);
        };

        private byte[] generateBytes(List<Product> products) {
            // TODO Your codes.
        }

    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM