springMVC学习总结(三)数据绑定

一、springMVC的数据绑定,常用绑定类型有:

1、servlet三大域对象:

  • HttpServletRequest
  • HttpServletResponse
  • HttpSession

2、Model的方式

  • 类型:

    • Model

      1. @Controller
      2. public class Demo01Controller {
      3. @RequestMapping(value = "test.action")
      4. public String test(Model md){
      5. md.addAttribute("name","xujie");
      6. return "test";
      7. }
      8. }
    • ModelMap

      1. @Controller
      2. public class Demo01Controller {
      3. @RequestMapping(value = "test.action")
      4. public String test(ModelMap mp){
      5. mp.addAttribute("name","xujie");
      6. return "test"; //字符串是返回页面的页面名
      7. }
      8. }
    • ModelAndView

      1. @Controller
      2. public class Demo01Controller {
      3. @RequestMapping(value = "test.action")
      4. public ModelAndView test(ModelAndView mv){
      5. mv.addObject("name","xujie");
      6. mv.setViewName("test");
      7. return mv;
      8. }
      9. }
  • 前台页面jsp编码

    1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    2. <%@page isELIgnored="false" %>
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    4. "http://www.w3.org/TR/html4/loose.dtd">
    5. <html>
    6. <head>
    7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    8. <title>Hello World</title>
    9. </head>
    10. <body>
    11. 1、姓名:${requestScope.name }<br/>
    12. </body>
    13. </html>
  • 总结:

    • Model和ModelMap类型的model,都要在参数列表中声明。
    • ModelAndView可以不用在参数列表中声明,但是最后的跳转页面一定要通过ModelAndView.setViewName()的方式跳转,否则页面可以成功跳转,但是取不到后台设置的值。

3、绑定简单数据类型

  • 用法:

    • 示例一:

      1. //在处理器形参位置声明简单数据类型,处理器直接获取
      2. @Controller
      3. public class Demo01Controller {
      4. @RequestMapping(value = "test.action")
      5. public String test(String name){
      6. System.out.println("获取到前台的值是:"+name);
      7. return "test";
      8. }
      9. }
    • 支持的简单绑定类型:

      • 整型(int、Integer)
      • 字符串(String)
      • 单精度(Float、float)
      • 双精度(Double、double)
      • 布尔型(true、false)
  • @RequestParam用法:

    • @RequestParam 有三个常用属性值:

      • value:绑定参数的变量名

      • defaultValue:如果没有传这个值,默认取值

      • required:该变量是否必须要有

        示例:

        1. @Controller
        2. public class Demo01Controller {
        3. @RequestMapping(value = "test.action")
        4. public String test(@RequestParam(value = "name",defaultValue = "xujie",required = false) String name){
        5. System.out.println("name="+name);
        6. return "test";
        7. }
        8. }

4、绑定pojo(简单的java对象)类型

Student类:(pojo)

  1. public class Student {
  2. private String name;
  3. private int age;
  4. get/set...
  5. }

Controller类:

  1. @Controller
  2. public class Demo01Controller {
  3. @RequestMapping(value = "test.action",method = RequestMethod.POST)
  4. public String test(Student stu){
  5. System.out.println("学生姓名:"+stu.getName());
  6. System.out.println("学生年龄:"+stu.getAge());
  7. return "test";
  8. }
  9. }
  10. + *这里我是用的postman做的请求测试,所以此处不列举前台是如何发送请求的了,只要是post请求,并且参数名分别为nameage就可以获取到;*

5、绑定包装对象(对象里面有对象)

Courses类(pojo):

  1. package com.springMVC.pojo;
  2. public class Courses {
  3. private String coursesName;
  4. private String teacher;
  5. get/set...
  6. }

Courses类(pojo):

  1. package com.springMVC.pojo;
  2. public class Student {
  3. private String name;
  4. private int age;
  5. private Courses courses;
  6. get/set...
  7. }

Controller类:

  1. @Controller
  2. public class Demo01Controller {
  3. @RequestMapping(value = "test.action",method = RequestMethod.POST)
  4. public String test(Student stu){
  5. System.out.println("学生姓名:"+stu.getName());
  6. System.out.println("学生年龄:"+stu.getAge());
  7. System.out.println("课程名称"+stu.getCourses().getCoursesName());
  8. System.out.println("课程老师"+stu.getCourses().getTeacher());
  9. return "test";
  10. }
  11. }

6、绑定数组(以字符串数组为例)

直接绑定数组类型参数

Controller类:

  1. @Controller
  2. public class Demo01Controller {
  3. @RequestMapping(value = "test.action",method = RequestMethod.POST)
  4. public String test(String[] strs){
  5. for (String str:strs ) {
  6. System.out.println(str);
  7. }
  8. return "test";
  9. }
  10. }

接口测试:

通过pojo属性的方式绑定数组

pojo类:

  1. package com.springMVC.pojo;
  2. public class Student {
  3. private String name;
  4. private int age;
  5. private Courses courses;
  6. private String[] friends;
  7. get/set...
  8. }

Controller类:

  1. @Controller
  2. public class Demo01Controller {
  3. @RequestMapping(value = "test.action",method = RequestMethod.POST)
  4. public String test(Student stu){
  5. String[] friends = stu.getFriends();
  6. for (String str:friends ) {
  7. System.out.println(str);
  8. }
  9. return "test";
  10. }
  11. }

接口测试

7、绑定List

接收页面数据

接收页面数据的时候,list必须声明为某一个pojo的属性才可以接收到

pojo类:

  1. package com.springMVC.pojo;
  2. import java.util.List;
  3. public class Student {
  4. private String name;
  5. private int age;
  6. private Courses courses;
  7. private List<String> friends; //pojo的list
  8. get/set...
  9. }

Controller类:

  1. @Controller
  2. public class Demo01Controller {
  3. @RequestMapping(value = "test.action",method = RequestMethod.POST)
  4. public String test(Student student){
  5. List<String> friends = student.getFriends();
  6. for (String str : friends) {
  7. System.out.println(str);
  8. }
  9. return "test";
  10. }
  11. }

接口测试:

向页面传递数据

Controller类:

此处以ModelMap的方式向页面传递数据

  1. @Controller
  2. public class Demo01Controller {
  3. @RequestMapping(value = "test.action",method = RequestMethod.GET)
  4. public String test(ModelMap modelMap){
  5. //ModelMap modelMap = new ModelMap();
  6. Student student = new Student();
  7. ArrayList<String> list = new ArrayList<String>();
  8. list.add("xujie1");
  9. list.add("xujie2");
  10. list.add("xujie3");
  11. list.add("xujie4");
  12. student.setFriends(list);
  13. student.setName("yuanxiliu");
  14. modelMap.addAttribute("student",student);
  15. return "test";
  16. }
  17. }

jsp页面:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <%@page isELIgnored="false" %>
  3. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  5. "http://www.w3.org/TR/html4/loose.dtd">
  6. <html>
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  9. <title>Hello World</title>
  10. </head>
  11. <body>
  12. <c:forEach items="${student.friends}" var="friend" varStatus="state" >
  13. ${friend}<%--循环输出List--%>
  14. </c:forEach>
  15. </body>
  16. </html>

页面结果:

8、绑定Map

跟list类似,同样必须定义成某个pojo的属性才可以绑定数据:

pojo类:

  1. package com.springMVC.pojo;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. public class Student {
  5. private String name;
  6. private int age;
  7. private Courses courses;
  8. private HashMap<String,String> parents;
  9. get/set...
  10. }

Controller类:

  1. @Controller
  2. public class Demo01Controller {
  3. @RequestMapping(value = "test.action",method = RequestMethod.POST)
  4. public String test(Student student){
  5. String father = student.getParents().get("father");
  6. String mother = student.getParents().get("mother");
  7. System.out.println("父亲是:"+father);
  8. System.out.println("母亲是:"+mother);
  9. return "test";
  10. }
  11. }

接口测试:

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. 使用c#操作txt

    如何读取文本文件内容: 在本文介绍的程序中,是把读取的文本文件,用一个richTextBox组件显示出来.要读取文本文件,必须使用到"StreamReader"类,这个类是由名字空 ...

  2. Python源码分析

  3. 移动应用开发者最应该知道的8款SDK

    2017年双11全球狂欢节结束后,据大数据公司统计显示,2017年双11全网销售额达2539.7亿,移动端销售占比91.2%.不难看出,智能手机因随身携带.时刻在线等特点,已取代PC,成为网络生活新的 ...

  4. idea for Mac 代码提示设置

    1 打开idea. 2 command+, 打开设置 ,移除Cyclic Expand Word 的快捷键   3 设置basic的快捷键为 option+/ 4,自动提示大小写敏感关闭 apply ...

  5. MyBatis开发学习记录

    使用MyBatis时主要是完成POJO和SQL的映射规则 MyBatis基本构成: SqlSessionFactoryBuilder SqlSessionFactory SqlSession SqlM ...

  6. ssh、scp免秘钥远程执行命令:expect

    首先安装expect # yum -y install expect 命令格式 # ./expect IP COMM    #expect是独立的工具,所以不能用sh来执行 1 2 3 4 5 6 7 ...

  7. x86平台上的Windows页表映射机制

    首先,在x86架构的处理器上,一个正常页面大小为4KB,非PAE模式下,CR3持有页目录页面的物理地址,PDE和PTE格式相同大小为4字节.此时每个页表页面包含1024个PTE,可以映射1024个页面 ...

  8. 书籍推荐系列之一 -- 《凤凰项目:一个IT运维的传奇故事》

    博客已经完全更新了名字,新的名字,新的开始,想让自走向新的道路是很难的,走出舒适圈说了好久,也是时候开始行动了,今天就从写博客开始. 今天给大家推荐一本书,<凤凰项目:一个IT运维的传奇故事&g ...

  9. Qt creator中文输入—fctix-qt5 源码编译 libfcitxplatforminputcontextplugin.so

    fctix-qt5 的源码有两个地方可以下载: wget https://download.fcitx-im.org/fcitx-qt5/fcitx-qt5-1.0.5.tar.xztar -xJf ...

  10. 组合模式(Composite)

    组合模式(Composite) 组合模式有时又叫部分-整体模式在处理类似树形结构的问题时比较方便,看看关系图: 直接来看代码: [java] view plaincopypublic class Tr ...