数据提交

1. 前端的参数与controller中的参数名一致

可以直接用

package com.wang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/user")
public class UserController { //localhost: 8080/user/t1?name=xxx
//前端的参数名与此处一致,可以直接用
@GetMapping("/t1")
public String test(String name, Model model) {
//1. 接受前端参数
System.out.println("接受到前端的参数为: " + name);
//2. 将返回的结果传递给前端
model.addAttribute("msg", name);
return "test";
}
}

2. 前端的参数与controller中的参数名不一致

推荐无论一不一样, 都加上@RequestParam("前端的参数名")

package com.wang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; @Controller
@RequestMapping("/user")
public class UserController { // //localhost: 8080/user/t1?name=xxx
// //前端的参数名与此处一致,可以直接用
// @GetMapping("/t1")
// public String test(String name, Model model) {
// //1. 接受前端参数
// System.out.println("接受到前端的参数为: " + name);
// //2. 将返回的结果传递给前端
// model.addAttribute("msg", name);
// return "test";
// } //localhost: 8080/user/t1?username=xxx
//前端的参数名与此处不一致, 需要加注解
@GetMapping("/t1")
public String test(@RequestParam("username") String name, Model model) {
//1. 接受前端参数
System.out.println("接受到前端的参数为: " + name);
//2. 将返回的结果传递给前端
model.addAttribute("msg", name);
return "test";
}
}

3. 前端接收的是一个对象

package com.wang.controller;

import com.wang.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; @Controller
@RequestMapping("/user")
public class UserController { //前端接收的是一个对象 : id, name, age
//localhost: 8080/user/t1?id=XXX&name=xxx&age=xxx
/*
1. 接受前端用户传递的参数, 判断参数的名字, 假设名字直接在方法上, 可以直接使用
2. 假设传递的是一个对象User, 匹配User对象中的字段名: 如果名字一致则OK, 否则, 匹配不到
*/ @GetMapping("/t3")
public String test3(User user, Model model) { System.out.println(user); model.addAttribute("msg", user.toString()); return "test";
}
}

4. 总结

1. 接受前端用户传递的参数, 判断参数的名字, 假设名字直接在方法上, 可以直接使用
2. 假设传递的是一个对象User, 匹配User对象中的字段名: 如果名字一致则OK, 否则, 匹配不到

5. 数据显示到前端

1. 通过ModelAndView

public class ControllerTest1 implements Controller {
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//返回一个模型视图对象
ModelAndView mv = new ModelAndView();
mv.addObject("msg","ControllerTest1");
mv.setViewName("test");
return mv;
}
}

2. 通过Model

@RequestMapping("/ct2/hello")
public String hello(@RequestParam("username") String name, Model model){
   //封装要显示到视图中的数据
   //相当于req.setAttribute("name",name);
   model.addAttribute("msg",name);
   System.out.println(name);
   return "test";
}

3. 通过ModelMap

graph LR
id1[LinkedHashMap]
id2[ModelMap]
id3[Model]

id1 --继承了LinkedHashMap, 拥有LinkedHashMap的全部功能--> id2
id2 --精简版, 大部分情况下我们直接使用Model--> id3

@GetMapping("/t4")
public String test4 (@RequestParam("username") String name, ModelMap map) { map.addAttribute("msg", name);
System.out.println(name); return "test";
}

4. 对比

  • Model 只有寥寥几个方法只适合用于储存数据,简化了新手对于Model对象的操作和理解

  • ModelMap 继承了 LinkedMap ,除了实现了自身的一些方法,同样的继承 LinkedMap 的方法和特性

  • ModelAndView 可以在储存数据的同时,可以进行设置返回的逻辑视图,进行控制展示层的跳转

SpringMVC-数据提交的更多相关文章

  1. SpringMVC之ajax与表单 Post 数据提交差异小结

    最近在写一个富文本框的后台数据服务的时候遇到一些关于 ajax 提交与 表单提交的比较特殊的案例,这里拿来跟大家分享,希望能让大家有所启发. 1. 首先是常见表单提交在SpringMVC的控制器中的代 ...

  2. SpringMVC数据验证

    SpringMVC数据验证——第七章 注解式控制器的数据验证.类型转换及格式化——跟着开涛学SpringMVC 资源来自:http://jinnianshilongnian.iteye.com/blo ...

  3. 《Java从入门到放弃》入门篇:springMVC数据传递

    springMVC中的数据传递方式与JSP和Struts2相比,更加的简单.具体有什么样的区别呢?我们通过下面这张图来对比就知道了. 随手画的,有些错别字,不用太在意..... 接下来,进入正题,sp ...

  4. springMVC数据交互

    控制器 作为控制器,大体的作用是作为V端的数据接收并且交给M层去处理,然后负责管理V的跳转.SpringMVC的作用不外乎就是如此,主要分为:接收表单或者请求的值,定义过滤器,跳转页面:其实就是ser ...

  5. SpringMVC数据格式化

    SpringMVC数据格式化 1. 使用Formatter格式化数据 Converter可以将一种类型转换成另一种类型,是任意Object之间的类型转换. Formatter则只能进行String与任 ...

  6. 四种常见的 POST-------- content-type数据提交方式

    HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TRACE.CONNECT 这几种.其中 POST 一般用来向服务端提交数据,本文 ...

  7. ligerui_实际项目_003:form中添加数据,表格(grid)里面显示,最后将表格(grid)里的数据提交到servlet

    实现效果: "Form"中填写数据,向本页"Grid"中添加数据,转换成Json数据提交,计算总和,Grid文本框可编辑,排序 图片效果: 总结: //disp ...

  8. jsp 页面json数据提交到后台spring处理举例

    0 前台: 'type'        : 'post', 'contentType' : 'application/json;charset=UTF-8', 'data'        : JSON ...

  9. 数据提交成功后如何避免alert被window.location.reload()影响

    数据提交成功用alert提示,但页面立马就重载了 alert("提交成功!"); window.location.reload(); 如何避免? 方法一: setTimeout 延 ...

  10. jsp多模块相同数据提交到后台之数据处理

    最近在写一个java多模块表单提交,起初想的只是一个简单的form表单提交,写的时候发现不是真简单.多个相同类型数据提交到后台接收的问题很困难. 于是,和人进行深入的讨论,感觉j以json的格式提交时 ...

随机推荐

  1. C#LeetCode刷题之#581-最短无序连续子数组( Shortest Unsorted Continuous Subarray)

    问题 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 输入: [2, 6, 4, 8, 10, ...

  2. Flutter 打包程序 build android apk

    Step-1 Java 路径 找到java路径, 可使用[flutter doctor -v] Step-2: 进入目录 找到路径后 C:\Program Files\Java\jre1.8.0_23 ...

  3. golang 中的struct理解

    golang实验代码 package main import("fmt") type Stu struct{ name string age int } func (stu *St ...

  4. 使用对称加密来加密Spring Cloud Config配置文件

    补充 使用Spring Cloud Config加密功能需要下载JCE扩展,用于生成无限长度的密文.链接:http://www.oracle.com/technetwork/java/javase/d ...

  5. Android Studio 突然无法识别真机问题

    最近在赶项目,今天AS突然疯狂跟我作对,森气!! 平时连接手机没有问题,今天突然各种识别不到真机!! 1.数据线,check.没有问题. 2.重启AS,还是不行. 3.安装驱动,行不通. 4.已经弹出 ...

  6. 上手了RabbitMQ?再来看看它的交换机(Exchange)吧

    人生终将是场单人旅途,孤独之前是迷茫,孤独过后是成长. 楔子 本篇是消息队列RabbitMQ的第三弹. RabbitMQ的入门和RabbitMQ+SpringBoot的整合可以点此链接进去回顾,今天要 ...

  7. Spark QuantileDiscretizer 分位数离散器

    1.概念 接收具有连续特征的列,并输出具有合并分类特征的列.按分位数,对给出的数据列进行离散化分箱处理. 和Bucketizer(分箱处理)一样也是:将连续数值特征转换为离散类别特征.实际上Class ...

  8. CAS和锁的相关面试题

    CAS 锁 锁的四种状态和升级 锁的四种状态:无锁.偏向锁.轻量级锁和重量级锁 无锁 无锁就是没有真正意义上的上锁,所有的线程还是能访问并修改同一个资源,但是通过算法控制,实现同时只有一个线程修改成功 ...

  9. 个人项目(WordCount C语言)

    WordCount程序(C语言) Github地址:https://github.com/peter-ye-code/WordCount 一.题目描述 实现一个简单而完整的软件工具(源程序特征统计程序 ...

  10. 第7篇scrum冲刺(5.27)

    一.站立会议 1.照片 2.工作安排 成员 昨天已完成的工作 今天的工作安排 困难 陈芝敏  学习云开发,云函数调用以及数据的前后端传递  今天实现云词库搭建,随机获取并显示,对云开发有更深的认识   ...