第二章 SpringMVC返回值类型

x33g5p2x  于2021-03-14 发布在 其他  
字(1.1k)|赞(0)|评价(0)|浏览(109)

ModelAndView

@RequestMapping(value="/test3",method=RequestMethod.GET)
	public ModelAndView test3()
	{
		//跳转到index.jsp
		ModelAndView mav = new ModelAndView();
		mav.addObject("msg", "hello test3");
		mav.setViewName("/index.jsp"); //请求转发
		
		return mav;
	}

String

@RequestMapping("/test5")
	public String test5(HttpServletRequest request)
	{
		request.setAttribute("msg", "hello test5");
		return "/index.jsp";//请求转发
	}
@RequestMapping("/test6")
	public String test6(HttpSession session)
	{
		session.setAttribute("msg", "hello test6");		
		return "redirect:/index.jsp";//请求重定向
	}

void

@RequestMapping("/ajax")
	public void test7(HttpServletResponse response)
	{
		response.setContentType("text/json");
		response.setCharacterEncoding("utf-8");
		
		try {
			PrintWriter out = response.getWriter();
			out.write("{\"username\":\"feiyy\"}");
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

对象类型

SpringMVC的依赖注入:
HttpServletRequest request
HttpSession session
ServletContext application
HttpServletResponse response
Model model

相关文章