Spring MVC 中使用 Spring-JSON

2024-01-09

我偶然发现了图书馆Spring-JSON http://spring-json.sourceforge.net/faq.html同时希望在我的 spring mvc webapp 2.5 中添加 Ajax 支持。

我的问题是,这里有人使用过这个库吗?你的体验如何?

还有比这更好的选择吗?


也许这篇文章——http://www.jroller.com/kaiulrich/entry/a_mappingjacksonjsonview_springframework_and_spring http://www.jroller.com/kaiulrich/entry/a_mappingjacksonjsonview_springframework_and_spring可以让您很好地了解应该选择哪个 Json 视图。

UPDATE

假设这是我的 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实现如下所示

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();
    }

}

我希望它对你有用

Here http://today.java.net/article/2006/04/24/using-dojo-and-json-build-ajax-applications你可以看到一个关于 Json 使用 DOJO 的很好的教程(我不使用 DOJO)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring MVC 中使用 Spring-JSON 的相关文章

随机推荐