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";
        }
        }

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学习总结(三)数据绑定的更多相关文章

  1. SpringMVC:学习笔记(5)——数据绑定及表单标签

    SpringMVC——数据绑定及表单标签 理解数据绑定 为什么要使用数据绑定 基于HTTP特性,所有的用户输入的请求参数类型都是String,比如下面表单: 按照我们以往所学,如果要获取请求的所有参数 ...

  2. SpringMVC学习笔记之---数据绑定

    SpringMVC数据绑定 一.基础配置 (1)pom.xml <dependencies> <dependency> <groupId>junit</gro ...

  3. SpringMVC学习笔记(三)

    一.SpringMVC使用注解完成 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 <!--configure the setti ...

  4. SpringMVC 学习笔记(三)数据的校验

    34. 尚硅谷_佟刚_SpringMVC_数据绑定流程分析.avi 例如:在jsp中输入一个String字符串类型,需要转换成Date类型的流程如下 convertservice对传入的数据进行转换 ...

  5. SpringMVC学习总结(三)——Controller接口详解(2)

    4.5.ServletForwardingController 将接收到的请求转发到一个命名的servlet,具体示例如下: package cn.javass.chapter4.web.servle ...

  6. springmvc学习(三)

    第一点---------使用 @RequestMapping 映射请求• Ant 风格资源地址支持 3 种匹配符:?:匹配文件名中的一个字符 *:匹配文件名中的任意字符 **:** 匹配多层路径 @R ...

  7. SpringMVC学习手册(三)------EL和JSTL(上)

    1.含义 EL:       Expression Language , 表达式语言 JSTL:   Java Server Pages Standard Tag Library, JSP标准标签库  ...

  8. SpringMVC学习(三)———— springmvc的数据校验的实现

    一.什么是数据校验? 这个比较好理解,就是用来验证客户输入的数据是否合法,比如客户登录时,用户名不能为空,或者不能超出指定长度等要求,这就叫做数据校验. 数据校验分为客户端校验和服务端校验 客户端校验 ...

  9. SpringMVC学习记录三——8 springmvc和mybatis整合

    8      springmvc和mybatis整合 8.1      需求 使用springmvc和mybatis完成商品列表查询. 8.2      整合思路 springmvc+mybaits的 ...

  10. springmvc学习日志三

    一.文件的上传 1.首先在lib中添加相应的jar包 2.建立jsp页面,表单必须是post提交,编码必须是multipart/form-data,文件上传文本框必须起名 <body> & ...

随机推荐

  1. android wear开发之:创建可穿戴设备应用 - Creating Wearable Apps

    注:本文内容来自:https://developer.android.com/training/wearables/apps/index.html 翻译水平有限,如有疏漏,欢迎批评指教. 译:山人 创 ...

  2. [转载] 常用 Java 静态代码分析工具的分析与比较

    转载自http://www.oschina.net/question/129540_23043 简介: 本文首先介绍了静态代码分析的基本概念及主要技术,随后分别介绍了现有 4 种主流 Java 静态代 ...

  3. SpringBoot之简单日志配置

    我的目的指定一个文件夹输出:(不采用指定文件的原因是一个文件的大小时间长了会很大,不利于处理) logging: level: root: INFO org.sselab: controller: I ...

  4. 【Win 10 应用开发】UI Composition 札记(八):用 XamlLight 制作灯光效果

    前面老周已介绍过灯光的使用,如果你忘了,请用九牛二虎之力猛点击这里去复习一下.本篇老周再介绍另一种添加灯光的方法,这种方法是专为 XAML 元素而设计的,可以很方便地为可视化元素添加灯光效果. 不知道 ...

  5. web-故道白云&Clound的错误

    web-故道白云 题目: 解题思路: 0x01 首先看到题目说html里有秘密,就看了下源代码如图, 重点在红圈那里,表示输入的变量是id,当然上一行的method="get"同时 ...

  6. [最短路][部分转]P1027 Car的旅行路线

    题目描述 又到暑假了,住在城市A的Car想和朋友一起去城市B旅游.她知道每个城市都有四个飞机场,分别位于一个矩形的四个顶点上,同一个城市中两个机场之间有一条笔直的高速铁路,第I个城市中高速铁路了的单位 ...

  7. OC语言的面向对象—类和对象、方法

    1. 面向对象和面向过程思想 OC是面向对象的,C是面向过程的.面向对象和面向过程只是解决问题的两种不同思想 1. 面向对象和面向过程的区别 1) 以用电脑听歌为例子 a) 面向过程 ➢ 打开电脑 ➢ ...

  8. C语言之++--

    #include<stdio.h>int main(){int num,count=0,i=0;scanf("%d",&num);printf("nu ...

  9. docker-compose v3版本命令详解参考

    参考和指南 这些主题描述了Compose文件格式的第3版.这是最新的版本. Compose and Docker 兼容性矩阵 有几个版本的Compose文件格式 - 1,2,2.x和3.x.下表是快速 ...

  10. Java语句语法

    一.流程控制语句 1.分支语句 1)if else 语法:(写法1) if(条件1){     //写一次 满足条件1执行在代码 }else if(条件2){    //写0或n次 不满足条件1,满足 ...