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数据,格式 ...
随机推荐
- 获取当前进程目录 GetCurrentDirectory() 及 获取当前运行模块路径名GetModuleFileName()
GetCurrentDirectory 获得的是当前进程的活动目录(资源管理器决定的),可以用SetCurrentDirectory 修改的. 转自 http://m.blog.csdn.net/bl ...
- input 文件上传
<button class="blueButton fileinput-button" style="width:165px;" @click=" ...
- jboss 5.1 启动问题解决
在安装好后启动时可能遇到这样的情况: ERROR [AbstractKernelController] Error installing to Instantiated: name=Attachmen ...
- HDU 4135:Co-prime(容斥+二进制拆分)
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- 记录几个ubuntu环境下的php相关的路径
php路径 /usr/bin/php phpize5 /usr/bin/phpize5 php5-fpm /usr/sbin/php5-fpm php所有的配置文件 /etc/php5/fpm 重启p ...
- bool dfs 解决单一解问题的优越性
dfs的返回值类型可以是int 或者 void .bool 由void 与 int 作为返回值类型的dfs在得到解之后不能立即返回,即使你加上语句if(key)return;也要在得到解之后一点点返 ...
- 第二百七十六节,MySQL数据库,【显示、创建、选定、删除数据库】,【用户管理、对用户增删改查以及授权】
MySQL数据库,[显示.创建.选定.删除数据库],[用户管理.对用户增删改查以及授权] 1.显示数据库 SHOW DATABASES;显示数据库 SHOW DATABASES; mysql - 用户 ...
- hibernate连接oracle12c数据库报:java.sql.SQLException: ORA-01017: 用户名/口令无效; 登录被拒绝。(用户名/口令在oracle客户端以及cmd命令都能登入)
报错信息: 2017-09-22 15:40:07,354 WARN [org.hibernate.cfg.SettingsFactory] - Could not obtain connection ...
- jsp页面编写锚点,和html页面编写锚点
html锚点的编写方式,在jsp中不兼容.因此在写动态网页时,需要注意 一:html页面中的锚点编写方式 HTML锚点 <a href="#abc">goto1< ...
- shell学习笔记汇总
1.shell脚本中函数使用 函数定义在前,调用在后,顺序反了就没有效果了.函数调用为:函数名 参数列表 函数内部通过以下变量访问函数的参数:shell脚本函数中: $0: 这个脚本的名字 $n: 这 ...