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. windows form参数传递过程

    三.windows form参数传递过程 在Windows 程序设计中参数的传递,同样也是非常的重要的. 这里主要是通过带有参数的构造函数来实现的, 说明:Form1为主窗体,包含控件:文本框text ...

  2. ztree树形菜单的增加删除修改和换图标

    首先需要注意一点,如果有研究过树形菜单,就会发现实现删除和修改功能特别简单,但是增加却有一点复杂.造成这个现象是ztree树形菜单的历史遗留问题.大概是之前的版本没有增加这个功能,后来的版本加上了这个 ...

  3. 第八章 Redis数据库结构与读写原理

    注:本文主要参考自<Redis设计与实现> 1.数据库结构 每一个redis服务器内部的数据结构都是一个redisDb[],该数组的大小可以在redis.conf中配置("dat ...

  4. python绘制树枝

    python是解释型语言,下面的程序深刻的说明了这个问题. import turtle def branch(length,level): if level<=0: return turtle. ...

  5. mysql完整版

    mysql: dbs 数据库系统 bdms 数据库管理系统 bda 数据库管理员 db 数据库 dba通过dbms来操作db! 关系型数据库和非关系型数据库 登录mysql mysql -h主机地址 ...

  6. How Many Tables 简单并查集

    Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to kn ...

  7. HTML第二课——css

    请关注公众号:自动化测试实战 先给大家提个建议,就是用sublime编辑器来编写.用其他的也无所谓,我只是建议,因为这个会帮你自动补全很多代码. css概念 css叫层叠样式表.意思就是一层一层的叠加 ...

  8. WPF 多线程异常抛送到UI线程

    无论是winform还是WPF,在.NET 2.0之后 只要是多线程中产生了异常都会导致程序强制结束. 那么我们一般的做法是将未知的多线程的异常抛送到UI线程去,然后进行处理.. 正确的多线程中的异常 ...

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

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

  10. oracle 与sql serve 获取随机行数的数据

    Oracle 随机获取N条数据    当我们获取数据时,可能会有这样的需求,即每次从表中获取数据时,是随机获取一定的记录,而不是每次都获取一样的数据,这时我们可以采取Oracle内部一些函数,来达到这 ...