Spring MVC 处理模型数据
SpringMVC 处理模型数据:
1 controller接收pojo:
<form action="save" method="get">
<label for="">用户名:<input type="text" name="username" /></label>
<label for="">密码:<input type="password" name="password" /></label>
<label for="">年龄:<input type="text" name="age" /></label>
<label for="">邮箱:<input type="text" name="email" /></label>
<label for="">省份:<input type="text" name="address.province" />
</label> <label for="">城市:<input type="text" name="address.city" /></label>
<button>保存</button>
</form>
@RequestMapping("/save")
public String save(User user) {
System.out.println(user);
return "success";
}
2 controller接收model:
@RequestMapping("/edit2")
public String edit1(Model model) {
User user = new User();
user.setUsername("harry");
user.setPassword("123");
user.setAge(17);
user.setEmail("11@qq.com");
Address address = new Address();
address.setProvince("horwards");
address.setCity("potter");
user.setAddress(address);
model.addAttribute("user", user);
return "user/form";
}
3 controller返回modleAndView:
<form action="save" method="get">
<label for="">用户名:<input type="text" name="username" value="${user.username}"/></label>
<label for="">密码:<input type="password" name="password" value="${user.password}"/></label>
<label for="">年龄:<input type="text" name="age" value="${user.age}"/></label>
<label for="">邮箱:<input type="text" name="email" value="${user.email}"/></label>
<label for="">省份:<input type="text" name="address.province" value="${user.address.province}"/></label>
<label for="">城市:<input type="text" name="address.city" value="${user.address.city}"/></label>
<button>保存</button>
</form>
@RequestMapping("/edit")
public ModelAndView edit() {
User user = new User();
user.setUsername("draco");
user.setPassword("123");
user.setAge(17);
user.setEmail("11@qq.com");
Address address = new Address();
address.setProvince("horwards");
address.setCity("malfoy");
user.setAddress(address);
ModelAndView mv = new ModelAndView();
mv.setViewName("user/form");
mv.addObject("user", user);
return mv;
}
Spring MVC 处理模型数据的更多相关文章
- Spring MVC 处理模型数据(@ModelAttribute)
SpringMVC中的模型数据是非常重要的,因为MVC中的控制(C)请求处理业务逻辑来生成数据模型(M),而视图(V)就是为了渲染数据模型的数据. 直白来讲,上面这句话的意思就是:当有一个查询的请求, ...
- ajax使用向Spring MVC发送JSON数据出现 org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported错误
ajax使用向Spring MVC发送JSON数据时,后端Controller在接受JSON数据时报org.springframework.web.HttpMediaTypeNotSupportedE ...
- Spring MVC返回json数据给Android端
原先做Android项目时,服务端接口一直是别人写的,自己拿来调用一下,但下个项目,接口也要自己搞定了,我想用Spring MVC框架来提供接口,这两天便抽空浅学了一下该框架以及该框架如何返回json ...
- Spring MVC前后端数据交互总结
控制器 作为控制器,大体的作用是作为V端的数据接收并且交给M层去处理,然后负责管理V的跳转.SpringMVC的作用不外乎就是如此,主要分为:接收表单或者请求的值,定义过滤器,跳转页面:其实就是ser ...
- Spring MVC @InitBinder 数据绑定 & 数据格式化 & 数据校验
1 数据绑定 2 数据格式化 修改绑定的字段等等操作 日期 - 接收表单日期字符串格式内容.,在实体类加入@DateTimeFormat 数值 原理: DefautFormattingConversi ...
- Spring MVC之JSON数据交互和RESTful的支持
1.JSON概述 1.1 什么是JSON JSON(JavaScript Object Notation,JS对象标记)是一种轻量级的数据交换格式.它是基于JavaScript的一个子集,使用了C.C ...
- Spring MVC 返回json数据 报406错误 问题解决方案
将jackson jar包改为jackson-databind-2.5.0.jar jackson-core-2.5.0.jar jackson-annotations-2.5.0.jar(这个版 ...
- Spring MVC生成JSON数据
以下示例演示如何使用Spring Web MVC框架生成JSON数据格式.首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序: ...
- spring mvc 返回json数据的四种方式
一.返回ModelAndView,其中包含map集 /* * 返回ModelAndView类型的结果 * 检查用户名的合法性,如果用户已经存在,返回false,否则返回true(返回json数据,格式 ...
随机推荐
- JDK中Integer类的进制转换实现
JDK中关于Integer类的进制转换方法很精巧,具体实现如下: final static char[] digits = { '0' , '1' , '2' , '3' , '4' , '5' , ...
- MySQL配置优化需要避免的误区
Caution: Don't overwrite your entire my.cnf at once when tuning MySQL. One or two changes per restar ...
- SWIFT推送之本地推送(UILocalNotification)
本地推送通知是通过实例化UILocalNotification实现的.要实现本地化推送可以在AppDelegate.swift中添加代码实现,本事例是一个当App进入后台时推送一条消息给用户. 1.首 ...
- react 部分ES6写法
react+react-router+antd 栗子:https://github.com/Aquarius1993/reactApp 模块: 1. 引入模块 import React from 'r ...
- IoT 通信协议
/********************************************************************************* * IoT 通信协议 * 说明: ...
- 备份Windows密钥,重装后免费激活系统教程
最重要的一点:在你重装系统之前,一定要先备份Windows密钥,不然重装系统后就查询不了原来的密钥,从而无法免费激活. 1.查询系统密钥(注册表中查询) 打开 “运行”(快捷键 win + R) 打开 ...
- [HRBUST-1688]数论中的异或(思维题)
数论中的异或 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 75(41 users) Total Accepted: 35(30 us ...
- BKL 大内核锁
BKL 大内核锁 BKL是一种递归锁.一个进程可以多次请求一个锁,并不会像自旋锁那么产生死锁. BKL可以在进程上下文中. BKL是有害的. 在内核中不鼓励使用BKL.一个执行线程可以递归的请求锁lo ...
- JAVA中@SuppressWarnings注解的作用
eclipse中开发JAVA代码时,经常会出现编译告警符号,影响代码可读性,也影响调试的断点符号正常显示,可以使用@SuppressWarnings注解来抑制这些告警符号的再现.1.抑制单个类型的告警 ...
- npm 使用
npm 命令: 命令 作用 npm init 产生package.json文件 npm list <package> 查看安装的module版本 npm update <packag ...