开发者

Spring-JSON Used in Spring MVC

开发者 https://www.devze.com 2022-12-28 23:26 出处:网络
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?

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开发者_如何学编程?


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.

UPDATE

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

}

I hope it can be useful to you

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

0

精彩评论

暂无评论...
验证码 换一张
取 消