作者:rukal2502900501_324 | 来源:互联网 | 2023-09-24 18:14
概念数据绑定就是将请求中的参数接收到处理器即控制器中。不同类型的数据绑定简单对象自定义对象复合对象集合对象简单的对象绑定基本类型包装类型简单对象绑定代码示例@Controller(
概念
数据绑定就是将请求中的参数接收到处理器即控制器中。
不同类型的数据绑定
简单的对象绑定
简单对象绑定代码示例
@Controller("simpleController")
@RequestMapping("/simple")
public class SimpleController {
//基本类型绑定
@RequestMapping("/bindBase.do")
public String bindBaseType(HttpServletRequest request){
String parameter = request.getParameter("kgc");
System.out.println("获取到的参数是"+parameter);
return "success";
}
@RequestMapping("/bindBase1.do")
public String bindBaseType1(int age){
System.out.println("获取到的年龄是"+age);
return "success";
}
@RequestMapping("/bindWrap.do")
public String bindWrap(Integer age){
System.out.println("获取到的包装类型参数"+age);
return "success";
}
}
自定义对象绑定
通过参数注入方式实现数据绑定,按属性名绑定 1.提供自定义对象
/** * 学生的数据模型 * */
public class StudentModel {
//学生ID
private int studentId;
//学生姓名
private String studentName;
@Override
public String toString() {
return "StudentModel [studentId=" + studentId + ", studentName="
+ studentName + "]";
}
//getter、setter方法省略。。。
}
2.提供处理方法
/** * 自定义对象绑定示例控制器 * */
@Controller
@RequestMapping("/custorm")
public class CustormController {
//提供绑定方法
//访问路径:http://localhost:8080/DataBind//custorm/bindCustorm.do?studentId=123&studentName=%27xiaoming%27
@RequestMapping("/bindCustorm.do")
public String bindCustorm(StudentModel student){
System.out.println(student);
return "success";
}
}
复合对象绑定
通过参数注入方式进行数据绑定
- 请求参数与自定义对象写法略有区别
- 提供一个复合对象,即有属性是类的对象类型的
复合对象绑定代码示例
public vlass ClassModel{
private Long classId;
private String className; //学生对象
private StudentModel studet;
}
参数就变成了:student.studentId=123&student.studentName=’xxx’
/** * 班级的数据模型 * */
public class ClassModel {
//班级ID
private Integer classId;
//班级名称
private String className;
//学生对象
private StudentModel student;
@Override
public String toString() {
return "ClassModel [classId=" + classId + ", className=" + className
+ ", student=" + student + "]";
}
//getter、setter方法省略。。。
/** * 复合对象的绑定示例控制器 * @author luojin * */
@Controller
@RequestMapping("/complex")
public class ComplexController {
//绑定方法
//请求路径:http://localhost:8080/DataBind//complex/bindComplex.do?classId=123&className=%27kgc%27&student.studentId=456&student.studentName=%27zhangsan%27
@RequestMapping("/bindComplex.do")
public String bindComplex(ClassModel clazz){
System.out.println("获取到的参数:"+clazz);
return "success";
}
}
集合对象绑定
集合参数绑定:提供一个数据对象来进行参数收集,否则不生效
集合对象绑定代码示例
/** * 学生的list集合模型 * */
public class StudentList {
//学生集合
private List<StudentModel> students;
@Override
public String toString() {
return "StudentList [students=" + students + "]";
}
public List<StudentModel> getStudents() {
return students;
}
public void setStudents(List<StudentModel> students) {
this.students = students;
}
}
/** * 学生set模型 * */
public class StudentSet {
private Set<StudentModel> studentSet;
public StudentSet() {
studentSet=new HashSet<StudentModel>();
studentSet.add(new StudentModel());
studentSet.add(new StudentModel());
}
@Override
public String toString() {
return "StudentSet [studentSet=" + studentSet + "]";
}
public Set<StudentModel> getStudentSet() {
return studentSet;
}
public void setStudentSet(Set<StudentModel> studentSet) {
this.studentSet = studentSet;
}
}
/** * 班级的map模型 * */
public class ClassMap {
private Map<String,ClassModel> classMap;
@Override
public String toString() {
return "ClassMap [classMap=" + classMap + "]";
}
public Map<String, ClassModel> getClassMap() {
return classMap;
}
public void setClassMap(Map<String, ClassModel> classMap) {
this.classMap = classMap;
}
}
/** * 学生的数组模型 * */
public class StudentArray {
private String[] studentIds=new String[3];
@Override
public String toString() {
return "StudentArray [studentIds=" + Arrays.toString(studentIds) + "]";
}
//getter、setter方法省略
}
/** * 集合参数绑定的示例 * */
@Controller
@RequestMapping("/collection")
public class CollectionController {
//错误的写法!
@RequestMapping("/bindError.do")
public String bindError(List<String> testList){
System.out.println(testList);
return "success";
}
//list 参数绑定
//http://localhost:8080/DataBind//collection/bindList.do?students[0].studentId=110&students[3].studentName=%27kgc%27
@RequestMapping("/bindList.do")
public String bindList(StudentList studentList){
System.out.println(studentList);
return"success";
}
//set参数绑定
//请求路径:http://localhost:8080/DataBind//collection/bindSet.do?studentSet[0].studentId=110&studentSet[1].studentName=%27kgc%27
@RequestMapping("/bindSet.do")
public String bindSet(StudentSet studentSet){
System.out.println(studentSet);
return"success";
}
//map参数绑定 classMap.get(key)
//%27相当于get请求当中的引号"" ''
//请求路径:http://localhost:8080/DataBind//collection/bindMap.do?classMap[x].classId=101&classMap[x].className=%27springMVC%27&classMap[x].student.studentId=123&classMap[x].student.studentName=%27xiaoming%27
@RequestMapping("/bindMap.do")
public String bindMap(ClassMap classMap){
System.out.println(classMap);
return"success";
}
//数组的参数绑定
//请求路径:http://localhost:8080/DataBind//collection//bindArr.do?studentIds[0]=1&studentIds[1]=2&studentIds[2]=3
//真实项目当中,一般使用:?studentIds=1,2,3,4,5,6 String split();
@RequestMapping("/bindArr.do")
public String bindArr(StudentArray students){
System.out.println(students);
return"success";
}
}
数据绑定的注解
- @RequestParam:绑定单个请求数据(最常用)
- @PathVariable:绑定 URL 模板变量值
- @ModelAttribute:取 model 中存的值
@RequestParam
@PathVariable
@ModelAttribute
完整形式:
@ModelAttribute(value=”参数名”)
作用:可以加在方法上,表示将方法返回值封装在Model对象当中; 可以加载方法的参数前,表示将获 取到的对象,绑定到具体的参数上; 可以配合表单标签使用,表示在请求中获取参数对象
package cn.kgc.controllers; import org.springframework.stereotype.Controller;
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.RequestParam; import cn.kgc.model.ClassModel;
import cn.kgc.model.StudentModel; /**
* 数据绑定注解示例控制器
* @author luojin
*
*/
@Controller
@RequestMapping(&#8220;/anotation&#8221;)
public class AnotationController {
//验证RequestParam注解
@RequestMapping(&#8220;/checkRp.do&#8221;)
public String checkRp(
@RequestParam(value = &#8220;kgcName&#8221;) String kgcName) {
System.out.println(kgcName);
return &#8220;success&#8221;;
}
//验证RequestParam注解
@RequestMapping(&#8220;/checkPv/{id}&#8221;)
public String checkPv(
@PathVariable(value=&#8221;id&#8221;) String kgcId) {
System.out.println(kgcId);
return &#8220;success&#8221;;
}
@ModelAttribute(&#8220;dataKey&#8221;)
public StudentModel initData(){
StudentModel student=new StudentModel();
student.setStudentId(123);
student.setStudentName(&#8220;课工场&#8221;);
return student;
}
@RequestMapping(&#8220;/getData.do&#8221;)
public String getModelData(@ModelAttribute(&#8220;dataKey&#8221;) StudentModel stu){
System.out.println(&#8220;model中的参数是:&#8221;+stu);
return &#8220;success&#8221;;
}
//请求路径:http://localhost:8080/DataBind//anotation/getFormData.do?classId=123&className=kgc
@RequestMapping(&#8220;/getFormData.do&#8221;)
public String getFormData(@ModelAttribute(&#8220;class&#8221;) ClassModel clazz){
System.out.println(&#8220;获取的表单对象:&#8221;+clazz);
return &#8220;success&#8221;;
} }