其他分享
首页 > 其他分享> > springMVC(三)-处理数据模型及@requestModelAttribute

springMVC(三)-处理数据模型及@requestModelAttribute

作者:互联网

一、ModelAndView

1、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>
	<form action="index/requestModelAndView" method="post">
		<input type="text" name="name" />
		<input type="text" name="age" />
		<input type="text" name="stucard.cardId" />
		<input type="text" name="stucard.cardName" />
		<input type="submit" value="带返回值 ModelAndView"/>
	</form>
</body>
</html>

2、控制器类

通过 ModelAndView 的构造函数指定转发页面(可以由视图解析器添加前后缀),并通过 ModelAndView 的 addObject(String attributeName, Object attributeValue)方法设置返回值

/**
 * Servlet implementation class Test
 */
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
	/**
     * 带返回值--ModelAndView
     * @param student
     * @return
     */
    @RequestMapping(value={"/requestModelAndView"})
    public ModelAndView requestModelAndView(Student student) {
    	//将 welcome.jsp放在构造函数中
    	ModelAndView modelAndView=new ModelAndView("welcome"); 
    	//将返回值放入request域中
    	modelAndView.addObject("studentMav",student);
    	return modelAndView;
    }
}

3、跳转页面 : welcome.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>
		返回值:</br>
		ModelAndView :</br>
		name:${requestScope.studentMav.name} </br>
		cardId:${requestScope.studentMav.stucard.cardId}</br>
		cardName:${requestScope.studentMav.stucard.cardName}</br>
</body>
</html>

二、ModelMap

1、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>
	<form action="index/requestModelMap" method="post">
		<input type="text" name="name" />
		<input type="text" name="age" />
		<input type="text" name="stucard.cardId" />
		<input type="text" name="stucard.cardName" />
		<input type="submit" value="带返回值 ModelMap"/>
	</form>
</body>
</html>

2、控制器类

/**
 * Servlet implementation class Test
 */
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
	/**
     * 带返回值--ModelMap
     * @param student
     * @param modelMap
     * @return
     */
    @RequestMapping(value={"/requestModelMap"})
    public String requestModelMap(Student student,ModelMap modelMap) {
    	modelMap.put("studentMm", student);
    	return "welcome";
    }
}

3、跳转页面 : welcome.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>
		ModelMap :</br>
		name:${requestScope.studentMm.name} </br>
		cardId:${requestScope.studentMm.stucard.cardId}</br>
		cardName:${requestScope.studentMm.stucard.cardName}</br>
</body>
</html>

三、Model

1、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>
	<form action="index/requestModel" method="post">
		<input type="text" name="name" />
		<input type="text" name="age" />
		<input type="text" name="stucard.cardId" />
		<input type="text" name="stucard.cardName" />
		<input type="submit" value="带返回值 Model"/>
	</form>
</body>
</html>

2、控制器类

/**
 * Servlet implementation class Test
 */
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
	/**
     * 带返回值--Model
     * @param student
     * @param model
     * @return
     */
    @RequestMapping(value={"/requestModel"})
    public String requestModel(Student student,Model model) {
    	model.addAttribute("studentModel", student);
    	return "welcome";
    }
}

3、跳转页面 : welcome.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>
		Model :request</br>
		name:${requestScope.studentModel.name} </br>
		cardId:${requestScope.studentModel.stucard.cardId}</br>
		cardName:${requestScope.studentModel.stucard.cardName}</br></br></br>
</body>
</html>

四、Map

1、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>
	<form action="index/requestMap" method="post">
		<input type="text" name="name" />
		<input type="text" name="age" />
		<input type="text" name="stucard.cardId" />
		<input type="text" name="stucard.cardName" />
		<input type="submit" value="带返回值 Map"/>
	</form>
</body>
</html>

2、控制器类

/**
 * Servlet implementation class Test
 */
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
	/**
     * 带返回值--Map
     * @param student
     * @param map
     * @return
     */
    @RequestMapping(value={"/requestMap"})
    public String requestMap(Student student,Map<String,Object> map) {
    	map.put("studentMap", student);
    	return "welcome";
    }
}

3、跳转页面 : welcome.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>
		Map :request</br>
		name:${requestScope.studentMap.name} </br>
		cardId:${requestScope.studentMap.stucard.cardId}</br>
		cardName:${requestScope.studentMap.stucard.cardName}</br></br>
</body>
</html>

五、将返回信息放入session

@SessionAttributes(value ={“studentMap”,“studentModel”}):根据已在request与中设置的 key 设置 session

/**
 * Servlet implementation class Test
 */
@SessionAttributes(value ={"studentMap","studentModel"})
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
	/**
     * 带返回值--Model
     * @param student
     * @param model
     * @return
     */
    @RequestMapping(value={"/requestModel"})
    public String requestModel(Student student,Model model) {
    	model.addAttribute("studentModel", student);
    	return "welcome";
    }
    
	/**
     * 带返回值--Map
     * @param student
     * @param map
     * @return
     */
    @RequestMapping(value={"/requestMap"})
    public String requestMap(Student student,Map<String,Object> map) {
    	map.put("studentMap", student);
    	return "welcome";
    }
}

六、@ModelAttribute

1、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>
	<form action="index/requestModelAttribute" method="post">
		<input type="submit" value="@ModelAttribute"/>
	</form>
</body>
</html>

2、控制器类

1、@ModelAttribute:在任何一次请求前,都会先执行 @ModelAttribute 注解的方法

2、@ModelAttribute(“studentAttr”):通过@ModelAttribute("studentAttr")注解将 @ModelAttribute 注解 的方法的 map 返回值注入 @ModelAttribute("studentAttr") 所注解的参数

/**
 * Servlet implementation class Test
 */
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
	@ModelAttribute//在任何一次请求前,都会先执行 @ModelAttribute 注解 的方法
    public void queryStudent(Map<String,Object> map) {
    	Student student =new Student();
    	student.setId("11");
    	student.setName("myname");
    	map.put("studentAttr", student);
    	System.out.println("queryStudent--------------------");
    }
    /**
     * @ModelAttribute
     * @param student
     * @param map
     * @return
     */
    @RequestMapping(value={"/requestModelAttribute"})
    //若省略@ModelAttribute("studentAttr"),则@ModelAttribute所注释的方法中Map的key值默认必须为该方法参数类型的首字母小写(而非变量名),即 student
    public String requestRequestModelAttribute(@ModelAttribute("studentAttr") Student student,Map<String,Object> map) {
    	System.out.println("requestrequestModelAttribute--------------------");
    	map.put("studentModelAttr", student);
    	return "welcome";
    }
    
}

标签:requestModelAttribute,return,RequestMapping,springMVC,param,ModelAttribute,stude
来源: https://blog.csdn.net/weixin_43520586/article/details/121096199