Spring MVC中使用的Spring-JSON [英] Spring-JSON Used in Spring MVC

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

问题描述

我遇到了图书馆 Spring-JSON ,同时希望在其中添加Ajax支持我的春季mvc webapp 2.5.

I came across the library Spring-JSON while looking to add Ajax Support in my spring mvc webapp 2.5.

我的问题是,这里有没有人使用过这个库,您的经历是什么?

My question is, has anybody here have used this library and what are your experiences?

有没有比这更好的选择了?

Is there any better alternative than this?

推荐答案

也许这篇文章- http ://www.jroller.com/kaiulrich/entry/a_mappingjacksonjsonview_springframework_and_spring 可以让您深入了解应该选择哪种Json View.

Maybe this post - http://www.jroller.com/kaiulrich/entry/a_mappingjacksonjsonview_springframework_and_spring can give you a good insight about which Json View you should choose.

更新

假设这里是我的MultiActionController

Suppose here goes my MultiActionController

import static org.springframework.validation.ValidationUtils.*;

@Component
public class PersonController extends MultiActionController {

    /**
      * Notice my own JsonView implementation
      */ 
    private JsonView jsonView = new JsonView();

    public PersonController() {
        setValidators(new Validator[] {new Validator() {
            public boolean supports(Class clazz) {
                return clazz.isAssignableFrom(Person.class);
            }

            public void validate(Object command, Errors errors) {
                rejectIfEmpty(errors, "age", "", "Age is required");
                rejectIfEmptyOrWhitespace(errors, "name", "", "Name is required");
            }

        }});
    }

    public ModelAndView add(HttpServletRequest request, HttpServletResponse response, Person person) throws Exception {
        // do something (save our Person object, for instance)

        return new ModelAndView(jsonView);
    }

    /**
      * If something goes wrong (validation, data-binding)
      * This method takes care of handling "what goes wrong"
      */
    public ModelAndView hanldeBindException(HttpServletRequest request, HttpServletResponse response, ServletRequestBindingException bindingException) {
        BindException bindException = (BindException) bindingException.getRootCause();

        /**
          * Notice i am using jsonView to handle the response
          */
        return new ModelAndView(jsonView).addAllObjects(bindException.getModel());
    }

}    

JsonView的实现如下所示

JsonView implementation is shown as follows

public class JsonView implements View {

    @Autowired
    private MessageSource messageSource;

    public String getContentType() {
        return "application/json";
    }

    public void render(Map model, HttpServletRequest request, HttpServletResponse response) {
        PrintWriter writer = response.getWriter();

        response.setContentType("text/plain; charset=UTF-8");

        JSONObject jsonObject = new JSONObject();
        for (Object object : bindingResult.getAllErrors()) {
            if(object instanceof FieldError) {
                FieldError fieldError = (FieldError) object;

                jsonObject.put(fieldError.getField(), messageSource.getMessage(fieldError, null));
            }
        }

        writer.print(jsonObject.toString());
        writer.close();
    }

}

我希望它对您有用

这里通过使用DOJO(我不使用DOJO),您可以看到有关Json的不错的教程

Here you can see a nice Tutorial about Json by using DOJO (i do not use DOJO)

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

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