ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-11-02 11:33:57  阅读:180  来源: 互联网

标签:requestModelAttribute return RequestMapping springMVC param ModelAttribute stude


一、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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有