spring mvc 返回字符串或者是view的方法

public class JsonView {


	public static ModelAndView Render(Object model, HttpServletResponse response) {
		MappingJacksonHttpMessageConverter jsonConverter = new MappingJacksonHttpMessageConverter();
		MediaType jsonMimeType = MediaType.APPLICATION_JSON;
		try {
			jsonConverter.write(model, jsonMimeType, new ServletServerHttpResponse(response));
		} catch (HttpMessageNotWritableException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}

這是實現方法,下邊是調用方法:

@RequestMapping(value="/users", method=RequestMethod.POST)public Object index(@RequestBody SearchUsersViewModel model, HttpServletResponse response) {

    model.setList(userService.getUsers(model));

    if(true)
    {
        return new ModelAndView("controls/tables/users", "model", model);
    }
    else
    {
        return JsonView.Render(model, response);
    }    }

這裏全部是從Stack Overflow 粘貼的,原文地址:http://stackoverflow.com/questions/4917329/return-json-or-view-from-spring-mvc-controller



這個if條件句就是邏輯實現的,可以根據項目或者是流程的需要返回所需要的html或者是字符串!!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章