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 处理模型数据的更多相关文章

  1. Spring MVC 处理模型数据(@ModelAttribute)

    SpringMVC中的模型数据是非常重要的,因为MVC中的控制(C)请求处理业务逻辑来生成数据模型(M),而视图(V)就是为了渲染数据模型的数据. 直白来讲,上面这句话的意思就是:当有一个查询的请求, ...

  2. 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 ...

  3. Spring MVC返回json数据给Android端

    原先做Android项目时,服务端接口一直是别人写的,自己拿来调用一下,但下个项目,接口也要自己搞定了,我想用Spring MVC框架来提供接口,这两天便抽空浅学了一下该框架以及该框架如何返回json ...

  4. Spring MVC前后端数据交互总结

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

  5. Spring MVC @InitBinder 数据绑定 & 数据格式化 & 数据校验

    1 数据绑定 2 数据格式化 修改绑定的字段等等操作 日期 - 接收表单日期字符串格式内容.,在实体类加入@DateTimeFormat 数值 原理: DefautFormattingConversi ...

  6. Spring MVC之JSON数据交互和RESTful的支持

    1.JSON概述 1.1 什么是JSON JSON(JavaScript Object Notation,JS对象标记)是一种轻量级的数据交换格式.它是基于JavaScript的一个子集,使用了C.C ...

  7. Spring MVC 返回json数据 报406错误 问题解决方案

    将jackson jar包改为jackson-databind-2.5.0.jar  jackson-core-2.5.0.jar  jackson-annotations-2.5.0.jar(这个版 ...

  8. Spring MVC生成JSON数据

    以下示例演示如何使用Spring Web MVC框架生成JSON数据格式.首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序: ...

  9. spring mvc 返回json数据的四种方式

    一.返回ModelAndView,其中包含map集 /* * 返回ModelAndView类型的结果 * 检查用户名的合法性,如果用户已经存在,返回false,否则返回true(返回json数据,格式 ...

随机推荐

  1. magento如何在首页显示产品

    1.首先现在magento后台创建一个新的分类,记下这个分类的 ID 号码.使用这个新建的分类来管理你的首页产品,这个分类设置为前台不可见.这样就不会显示在你的分类菜单中了,但使用代码调用的时候却会显 ...

  2. ubuntu18.04 server配置静态ip (转载)

    原文地址: https://blog.csdn.net/mossan/article/details/80381679 最新发布的ubuntu18.04 server,启用了新的网络工具netplan ...

  3. 实验吧—隐写术——WP之 欢迎来到地狱

    解压压缩包后出现三个文件.jpg .zip .docx 发现图片打开不显示,用winhex打开发现缺少文件头,将jpg的文件头补上 保存后图片可以打开 是一个百度网盘的链接,注意l和1的区别,网盘里是 ...

  4. ACM中的取模

    取模本身的性质:(之前有一篇博客写过)三则运算(+,-,*)过程中的取模与最后的取模一样(前提是最后不超long long(或int) 范围,所以为防止超范围,直接对三则运算中的过程取模) 然后就是A ...

  5. 《DSP using MATLAB》Problem 4.2

    用matlab不会证,惭愧.

  6. java导出Excel 好文收藏

    http://www.cnblogs.com/Damon-Luo/p/5919656.html https://www.cnblogs.com/klguang/p/6425422.html

  7. ACM常用算法

    数据结构 栈,队列,链表 哈希表,哈希数组 堆,优先队列 双端队列 可并堆 左偏堆 二叉查找树 Treap 伸展树 并查集 集合计数问题 二分图的识别 平衡二叉树 二叉排序树 线段树 一维线段树 二维 ...

  8. MySQL--”自然键”和”代理键”优缺点

    ##=====================================================================================## 在数据库表设计中会纠 ...

  9. All the Apache Streaming Projects: An Exploratory Guide

    The speed at which data is generated, consumed, processed, and analyzed is increasing at an unbeliev ...

  10. 无法对 数据库'XXXXX' 执行 删除,因为它正用于复制

    无法对 数据库'XXXXX' 执行 删除,因为它正用于复制. (.Net SqlClient Data Provider) 使用以下方式一般可以解决 sp_removedbreplication 'X ...