springMVC學習總結(三)數據綁定
阿新 • • 發佈:2017-12-25
springmvc core nts 循環 ack sta attribute servle 設置
springMVC學習總結(三)數據綁定
一、springMVC的數據綁定,常用綁定類型有:
1、servlet三大域對象:
- HttpServletRequest
- HttpServletResponse
HttpSession
2、Model的方式
- 類型:
Model
@Controller public class Demo01Controller { @RequestMapping(value = "test.action") public String test(Model md){ md.addAttribute("name","xujie"); return "test"; } }
ModelMap
@Controller public class Demo01Controller { @RequestMapping(value = "test.action") public String test(ModelMap mp){ mp.addAttribute("name","xujie"); return "test"; //字符串是返回頁面的頁面名 } }
ModelAndView
@Controller public class Demo01Controller { @RequestMapping(value = "test.action") public ModelAndView test(ModelAndView mv){ mv.addObject("name","xujie"); mv.setViewName("test"); return mv; } }
前臺頁面jsp編碼
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page isELIgnored="false" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Hello World</title> </head> <body> 1、姓名:${requestScope.name }<br/> </body> </html>
- 總結:
- Model和ModelMap類型的model,都要在參數列表中聲明。
- ModelAndView可以不用在參數列表中聲明,但是最後的跳轉頁面一定要通過
ModelAndView.setViewName()
的方式跳轉,否則頁面可以成功跳轉,但是取不到後臺設置的值。
3、綁定簡單數據類型
用法:
示例一:
//在處理器形參位置聲明簡單數據類型,處理器直接獲取 @Controller public class Demo01Controller { @RequestMapping(value = "test.action") public String test(String name){ System.out.println("獲取到前臺的值是:"+name); return "test"; } }
- 支持的簡單綁定類型:
- 整型(int、Integer)
- 字符串(String)
- 單精度(Float、float)
- 雙精度(Double、double)
- 布爾型(true、false)
- @RequestParam用法:
- @RequestParam 有三個常用屬性值:
- value:綁定參數的變量名
- defaultValue:如果沒有傳這個值,默認取值
required:該變量是否必須要有
示例:@Controller public class Demo01Controller { @RequestMapping(value = "test.action") public String test(@RequestParam(value = "name",defaultValue = "xujie",required = false) String name){ System.out.println("name="+name); return "test"; } }
- @RequestParam 有三個常用屬性值:
4、綁定pojo(簡單的java對象)類型
Student類:(pojo)
public class Student {
private String name;
private int age;
get/set...
}
Controller類:
@Controller
public class Demo01Controller {
@RequestMapping(value = "test.action",method = RequestMethod.POST)
public String test(Student stu){
System.out.println("學生姓名:"+stu.getName());
System.out.println("學生年齡:"+stu.getAge());
return "test";
}
}
+ *這裏我是用的postman做的請求測試,所以此處不列舉前臺是如何發送請求的了,只要是post請求,並且參數名分別為name和age就可以獲取到;*
5、綁定包裝對象(對象裏面有對象)
Courses類(pojo):
package com.springMVC.pojo;
public class Courses {
private String coursesName;
private String teacher;
get/set...
}
Courses類(pojo):
package com.springMVC.pojo;
public class Student {
private String name;
private int age;
private Courses courses;
get/set...
}
Controller類:
@Controller
public class Demo01Controller {
@RequestMapping(value = "test.action",method = RequestMethod.POST)
public String test(Student stu){
System.out.println("學生姓名:"+stu.getName());
System.out.println("學生年齡:"+stu.getAge());
System.out.println("課程名稱"+stu.getCourses().getCoursesName());
System.out.println("課程老師"+stu.getCourses().getTeacher());
return "test";
}
}
6、綁定數組(以字符串數組為例)
直接綁定數組類型參數
Controller類:
@Controller
public class Demo01Controller {
@RequestMapping(value = "test.action",method = RequestMethod.POST)
public String test(String[] strs){
for (String str:strs ) {
System.out.println(str);
}
return "test";
}
}
接口測試:
通過pojo屬性的方式綁定數組
pojo類:
package com.springMVC.pojo;
public class Student {
private String name;
private int age;
private Courses courses;
private String[] friends;
get/set...
}
Controller類:
@Controller
public class Demo01Controller {
@RequestMapping(value = "test.action",method = RequestMethod.POST)
public String test(Student stu){
String[] friends = stu.getFriends();
for (String str:friends ) {
System.out.println(str);
}
return "test";
}
}
接口測試
7、綁定List
接收頁面數據
接收頁面數據的時候,list必須聲明為某一個pojo的屬性才可以接收到
pojo類:
package com.springMVC.pojo;
import java.util.List;
public class Student {
private String name;
private int age;
private Courses courses;
private List<String> friends; //pojo的list
get/set...
}
Controller類:
@Controller
public class Demo01Controller {
@RequestMapping(value = "test.action",method = RequestMethod.POST)
public String test(Student student){
List<String> friends = student.getFriends();
for (String str : friends) {
System.out.println(str);
}
return "test";
}
}
接口測試:
向頁面傳遞數據
Controller類:
此處以ModelMap的方式向頁面傳遞數據
@Controller
public class Demo01Controller {
@RequestMapping(value = "test.action",method = RequestMethod.GET)
public String test(ModelMap modelMap){
//ModelMap modelMap = new ModelMap();
Student student = new Student();
ArrayList<String> list = new ArrayList<String>();
list.add("xujie1");
list.add("xujie2");
list.add("xujie3");
list.add("xujie4");
student.setFriends(list);
student.setName("yuanxiliu");
modelMap.addAttribute("student",student);
return "test";
}
}
jsp頁面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<c:forEach items="${student.friends}" var="friend" varStatus="state" >
${friend}<%--循環輸出List--%>
</c:forEach>
</body>
</html>
頁面結果:
8、綁定Map
跟list類似,同樣必須定義成某個pojo的屬性才可以綁定數據:
pojo類:
package com.springMVC.pojo;
import java.util.HashMap;
import java.util.List;
public class Student {
private String name;
private int age;
private Courses courses;
private HashMap<String,String> parents;
get/set...
}
Controller類:
@Controller
public class Demo01Controller {
@RequestMapping(value = "test.action",method = RequestMethod.POST)
public String test(Student student){
String father = student.getParents().get("father");
String mother = student.getParents().get("mother");
System.out.println("父親是:"+father);
System.out.println("母親是:"+mother);
return "test";
}
}
接口測試:
springMVC學習總結(三)數據綁定