欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringMVC-Controller处理器方法的返回值

程序员文章站 2024-03-24 08:11:34
...
  • Servlet

    • 接收请求数据
      • 同步请求
      • 异步请求
    • 处理请求
    • 响应用户
      • 同步响应:
        • 数据存储到request域,转发到页面,页面使用模板引擎(EL,JSTL,Freemark,thymeleaf)解析数据
      • 异步响应:
        • JSON—页面—dom操作:拼标签,添加到页面指定的位置
  • Controller处理器方法的参数

    • HttpServletRequest HttpServletResponse HttpSession Model 用户请求参数(基本类型,String对象)

1. Controller理器方法的返回值-ModelAndView

一般用在同步请求

Model就是request域中的数据

View就是要跳转的视图或者其他处理器方法

@RequestMapping("/my.do")
	public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp){
		ModelAndView mv = new ModelAndView();
		// 从前端获取数据
		String sname = req.getParameter("sname");
		int sage = Integer.parseInt(req.getParameter("sage"));
		char ssex = req.getParameter("ssex").charAt(0);
		int cid = Integer.parseInt(req.getParameter("cid"));
		Student student = new Student(sname, sage, ssex, cid);
		// 执行添加学生方法
		studentService.addStudent(student);
		mv.addObject("msg", "插入成功");
		mv.setViewName("/jsp/index.jsp");
		return mv;
	}

只需要跳转,不需要向request域中存储数据

return new ModelAndView(“/视图页面”);

@RequestMapping("/my.do")
	public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp){
		
		return new ModelAndView("/jsp/mvctest.jsp");
	}

2.** Controller理器方法的返回值-String

一般用在同步请求

String就是视图名

//Controller处理器方法的返回值—String
	@RequestMapping("/test01.do")
	public String test01(HttpServletRequest req, HttpServletResponse resp,Model model){
		model.addAttribute("msg", "测试");
		//String就是视图名
		return "/jsp/mvctest.jsp";
	}

3. Controller理器方法的返回值-void

同步请求:

​ 使用原生的ServletAPI

//Controller处理器方法的返回值—void
	@RequestMapping("/test02.do")
	public void test02(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		//同步请求,使用原生的ServletAPI
		req.setAttribute("msg", "测试");
		req.getRequestDispatcher("/jsp/mvctest.jsp").forward(req, resp);
	}

异步请求:

​ 序列化JSON:

jsonlib:老

gson:google

fastjson:阿里

Jackson:

jackson jar包:

​ jackson-annotations-2.7.0.jar

​ jackson-core-2.7.0.jar

​ jackson-databind-2.7.0.jar

//Controller处理器方法的返回值—void
	@RequestMapping("/test03.do")
	public void test03(HttpServletRequest req, HttpServletResponse resp) throws JsonGenerationException, JsonMappingException, IOException{
		//异步请求,序列化json
		Student s1 = new Student(1,"张三",18,'女',2);
		Student s2 = new Student(2,"李四",20,'女',2);
		List<Student> students = new ArrayList<Student>();
		students.add(s1);
		students.add(s2);
		//序列化json的核心类:ObjectMapper
		ObjectMapper objectMapper = new ObjectMapper();
		//List集合
		objectMapper.writeValue(resp.getWriter(), students);
		//对象
		//objectMapper.writeValue(resp.getWriter(), s1);
	}
<script type="text/javascript">
	$(function(){
		$.post("<c:url value='/test03.do'/>",
				null,
				function(data){
					/* 该方法把data转换成json */
					/* data=JSON.parse(data) */
					/* alert(data.sname) */
					/* alert(typeof(data)) */
					$(data).each(function(){						
						alert(this.sname);
					})
		},"json")
	})
</script>

4.Controller理器方法的返回值-Object

异步请求

​ 使用@ResponseBody注解+Jacksonjar包+注解驱动配置

​ SpringMVC底层默认使用这个Jacksonjar包[email protected],将JSON串注入到响应体中

<!-- 配置MVC注解驱动,自动生成底层需要的核心类:
			[email protected]:底层需要HttpMessageConvert
		 -->
		<mvc:annotation-driven/>
//Controller处理器方法的返回值—Object
	@RequestMapping("/test04.do")
	@ResponseBody
	public Object test04(HttpServletRequest req, HttpServletResponse resp){
		//异步请求,序列化json
		//使用@ResponseBody注解+Jacksonjar包+注解驱动配置(<mvc:annotation-driven/>)
		//SpringMVC底层默认使用这个Jacksonjar包[email protected],将JSON串注入到响应体中
		Student s1 = new Student(1,"张三",18,'女',2);
		Student s2 = new Student(2,"李四",20,'女',2);
		List<Student> students = new ArrayList<Student>();
		students.add(s1);
		students.add(s2);
		Map<String, Student> m = new HashMap<String, Student>();
		m.put("s1", s1);
		m.put("s2", s2);
		//返回List对象
//		return students;
		//返回对象
//		return s1;
		//返回map
		return m;
	}