Spring MVC 3中xml的问题 [英] issues with xml in spring mvc 3

查看:100
本文介绍了Spring MVC 3中xml的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅下面的4个简单示例,其中2个适用于xml,其他2个不适用.

See my following 4 simple examples, 2 works for xml, the other 2 does not.

//works for html, json, xml
     @RequestMapping(value = "/test", method = RequestMethod.GET)
            public ModelAndView testContentNegiotation(HttpServletRequest request, HttpServletResponse response) {

                ModelAndView mav = new ModelAndView();

                    TestTO test =  new TestTO("some msg", -888);
                    mav.addObject("test", test);

                    mav.setViewName("test"); //test is a jsp page

                return mav;
            }


//does not work for xml
     @RequestMapping(value = "/test", method = RequestMethod.GET)
            public ModelAndView testContentNegiotation(HttpServletRequest request, HttpServletResponse response) {

                ModelAndView mav = new ModelAndView();

                    ErrorTO error =  new ErrorTO("some error", -111);
                    mav.addObject("error",error );

                    TestTO test =  new TestTO("some msg", -888);
                    mav.addObject("test", test);

                    mav.setViewName("test");

                return mav;
            }


         //works for xml and json   
@RequestMapping(value = "/test3", method = RequestMethod.GET)
    public @ResponseBody ErrorTO test3(HttpServletRequest request, HttpServletResponse response) {

        ErrorTO error = new ErrorTO();
        error.setCode(-12345);
        error.setMessage("this is a test error.");
        return error;
    }


//does not work for xml
            @RequestMapping(value = "/testlist", method = RequestMethod.GET)
            public @ResponseBody List<ErrorTO> testList2(HttpServletRequest request, HttpServletResponse response) {

                    ErrorTO error =  new ErrorTO("an error", 1);
                    ErrorTO error2 =  new ErrorTO("another error", 2);
                    ArrayList<ErrorTO> list = new ArrayList<ErrorTO>();
                    list.add(error);
                    list.add(error2);
                    return list;

            }

在两个无法生成xml的示例中,是否可以配置spring使其工作?

In the two examples that can't produce xml, is it possible for configuring spring to make it work?

推荐答案

两个不生成XML的示例均无法正常工作,因为您的模型中有多个顶级对象. XML无法表示这一点-您需要一个模型对象,然后可以将其转换为XML.同样,Spring MVC无法将裸列表转换为XML.

The two examples that don't generate XML aren't working because you have multiple top-level objects in your model. XML has no way to represent that - you need a single model object that can then be converted into XML. Similarly, a bare list cannot be converted into XML by Spring MVC.

在两种情况下,您都需要将各种模型对象包装到单个根对象中,然后将其添加到模型中.

In both cases, you need to wrap the various model objects into a single root object, and add that to the model.

JSON在单个文档中表示多个顶级对象没有问题.

JSON, on the other hand, has no issue with representing multiple top-level objects in a single document.

这篇关于Spring MVC 3中xml的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆