SpringMVC第二篇----处理模型层数据以及ModelAttribute等注解使用

在Controller层用于接收数据的方法
  • 在控制层中,可以使用map、model、modelmap和modelandview接收数据,其中包括view前端页面数据和后台返回的数据,并展示在下一个页面中通过requestScope对象接收,具体代码示例如下:
  • index.jsp页面代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<a href="handle/test01">test01</a>
	<br>
	<a href="handle/test02">test02</a>
	<br>
	<a href="handle/test03">test03</a>
	<br>
	<a href="handle/test04">test04</a>
	<br>


</body>
</html>

  • Controller层代码:
package indi.dsl.controller;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import indi.dsl.entry.Student;

@Controller
@RequestMapping(value = "handle")
public class SpringMVCController {
	//测试ModelAndView
	@RequestMapping(value="test01")
	public ModelAndView test01() {
		ModelAndView mv = new ModelAndView("success");
		Student stu = new Student();
		stu.setName("zs");
		stu.setAge(22);
		//将数据放入request域中,即requestScope中
		mv.addObject("student01", stu);
		return mv;
	}
	//测试Map
	@RequestMapping(value="test02")
	public String test02(Map<String, Object> map) {
		Student stu = new Student();
		stu.setName("zs");
		stu.setAge(22);
		//将数据放入request域中,即requestScope中
		map.put("student02", stu);
		return "success";
	}
	//测试Model
	@RequestMapping(value="test03")
	public String test03(Model mm) {
		Student stu = new Student();
		stu.setName("zs");
		stu.setAge(22);
		//将数据放入request域中,即requestScope中
		mm.addAttribute("student03", stu);
		return "success";
	}
	//测试ModelMap
	@RequestMapping(value="test04")
	public String test04(ModelMap mm) {
		Student stu = new Student();
		stu.setName("zs");
		stu.setAge(22);
		//将数据放入request域中,即requestScope中
		mm.addAttribute("student04", stu);
		return "success";
	}
}

  • success.jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    welcome <br>
    ${requestScope.student01.name}--01--${requestScope.student01.age}</br>
    ${requestScope.student02.name}--02--${requestScope.student02.age}</br>
    ${requestScope.student03.name}--03--${requestScope.student03.age}</br>
    ${requestScope.student04.name}--04--${requestScope.student04.age}</br>
</body>
</html>
  • 如果需要将数据存放在session域中,则需要在Controller类上,添加注释@SessionAttributes(value=“xxxx”),如果存在多个需要使用逗号隔开例如@SessionAttributes(value=“xxxx,YYYY”),其中“”中存放的是map、model、modelmap和modelandview中的KEY值,例如map(“stu”,student),则写法@SessionAttributes(value=“stu”)。
ModelAttribute的使用场景
  • 在正常开发中,往往需要先从数据库中查询到某一个人,然后再进行修改这个人的信息,这时可以使用ModelAttribute注解在查询的方法上,这时程序在执行相应的@RequestMapping注释的方法时,先会执行@ModelAttribute注释的方法,然后在执行@RequestMapping注释的方法,起到先查询在修改的作用,具体代码如下:
package indi.dsl.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import indi.dsl.entry.Student;

@Controller
@RequestMapping(value = "handle")
public class SpringMVCController {
	//查询数据
	@ModelAttribute
	public void queryStudent(Map<String, Object> map) {
		Student student = new Student();
		//Map<String, Object> map = new HashMap<String, Object>();
		student.setName("zs");
		student.setAge(22);
		student.setPhoneNo(123456);
		map.put("stu", student);
	}
	
	//修改查询到的信息
	@RequestMapping(value = "test")
	public String test(@ModelAttribute(value = "stu") Student student) {
		student.setAge(55);
		System.out.println(student.getName() + "--" + student.getAge() + "--" + student.getPhoneNo());
		return "success";
	}
}

  • 特别注意:

1.带有@ModelAttribute注解的方法,会在所有方法执行之前执行;
2.约定@ModelAttribute注解的方法中map中的KEY为@RequestMapping注解中的方法的参数类型的首字符小写,例如:map.put(“student”, student);
3.如果不一致,可以使用(@ModelAttribute(value = “xxx”)注解,在@RequestMapping注解的方法中的参数中特别声明,用来接收不满足默认约定的参数赋值;
4.对于@ModelAttribute注解的方法的方法中,需要在参数中声明声明Map,用于参数传递到@RequestMapping注释的方法中;或者可以将map中的参数作为返回值返回也可以,如果不使用这两种方式,则会产生查询出来的对象与修改方法中的对象不是同一个,会产生数据问题;
5.如果不使用@ModelAttribute注解注释方法,则查询出来的对象与修改方法中的对象不是同一个,会产生数据问题;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值