SpringMVC_5_响应数据和结果视图
1. 以返回值分类
1)字符串
UserController.java:
@Controller @RequestMapping("/user") public class UserController { @RequestMapping("/testString") public String testString(Model model){ User user = new User(); user.setUname("温岭拿皇"); user.setAge(26); model.addAttribute("user",user); System.out.println("testString"); return "success"; } }
success.jsp:
姓名:${user.uname}<br/>
年龄:${user.age}<br/>
2) void
当处理器的返回值类型为void,在执行完处理器后,程序会默认去找与注解同名的.jsp文件。
例如,/user/testVoid对应的.jsp文件为/WEB-INF/pages/user/testVoid.jsp。
另外,可以选择进行请求转发、重定向或者直接响应:
/** * 返回值类型为void * @param * @return */ @RequestMapping("/testVoid") public void testVoid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //请求转发的程序 /* request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);*/ //重定向 /*System.out.println(request.getContextPath()); response.sendRedirect(request.getContextPath()+"/index.jsp");*/ //直接进行响应 response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("直接返回内容"); System.out.println("testVoid"); }
3)ModelAndView
ModelAndView是SpringMVC提供的一个对象,该对象可以用作处理器的返回值。
/** * 返回值为ModelAndView * @param request * @param response * @throws ServletException * @throws IOException */ @RequestMapping("/testModelAndView") public ModelAndView testModelAndView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ModelAndView modelAndView = new ModelAndView(); User user = new User(); user.setUname("feixiang"); user.setAge(12); //把bean存储到modelAndView对象中,也会把bean存储到request域中 modelAndView.addObject("user",user); //跳转页面 modelAndView.setViewName("success"); return modelAndView; }
2.转发和重定向
1)forward转发
将处理器类型设置为String,返回以关键字forward打头,紧跟转发目标资源路径的字符串:
/** *使用关键字的方式进行转发 * @return */ @RequestMapping("/testForwardOrRedirect") public String testForwardOrRedirect(){ System.out.println("testForwardOrRedirect"); return "forward:/WEB-INF/pages/success.jsp"; }
2)重定向
/** *使用关键字的方式进行重定向 * @return */ @RequestMapping("/testForwardOrRedirect") public String testForwardOrRedirect(){ System.out.println("testForwardOrRedirect"); return "redirect:/index.jsp"; }
3.ResponseBody响应Ajax请求
作用:用于将处理器返回的对象转换为指定格式(json,xml等),然后充当响应体,响应给客户端。可以出现在方法上或者返回值类型上,一般只修饰方法。
格式的指定有三种方式:
- 配置MessageConverter;
- *在@RequestMapping、@GetMapping等注解中配置produces属性;----最常用
- 在接收请求头配置。
最常见的produces的值及其对应的指定格式有三种:
//表示方法的返回值将被转为JSON对象然后存入响应体 produces = {MediaType.APPLICATION_JSON_UTF8_VALUE} //表示方法的返回值将被转为 XML对象然后存入响应体 produces = {MediaType.APPLICATION_XML_VALUE} //表示方法的返回值将直接存入响应体 produces = {"text/plain;charset=UTF-8"}
示例代码如下:
1 //Controller中的方法 2 @RequestMapping(value = "/loginVerify", method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}) 3 @ResponseBody 4 public Map loginVerify(HttpServletRequest request, HttpServletResponse response) { 5 Map<String, Object> map = new HashMap<String, Object>(); 6 7 //判断request,然后往map存东西的代码,省略了...... 8 return map; 9 }
1 //下面的data变量就是一个json对象,是从响应体中得到的map 2 $.ajax({ 3 async: false,//同步,待请求完毕后再执行后面的代码 4 type: "POST", 5 url: '/loginVerify', 6 contentType: "application/x-www-form-urlencoded; charset=utf-8", 7 data: $("#loginForm").serialize(), 8 dataType: "text", 9 success: function (data) { 10 if(data.code==0) { 11 alert(data.msg); 12 } else { 13 window.location.href="/admin"; 14 15 } 16 }, 17 error: function () { 18 alert("数据获取失败") 19 } 20 })

浙公网安备 33010602011771号