SpringMVC札集(05)——SpringMVC参数回显
自定义View系列教程00–推翻自己和过往,重学自定义View
自定义View系列教程01–常用工具介绍
自定义View系列教程02–onMeasure源码详尽分析
自定义View系列教程03–onLayout源码详尽分析
自定义View系列教程04–Draw源码分析及其实践
自定义View系列教程05–示例分析
自定义View系列教程06–详解View的Touch事件处理
自定义View系列教程07–详解ViewGroup分发Touch事件
自定义View系列教程08–滑动冲突的产生及其处理
探索Android软键盘的疑难杂症
深入探讨Android异步精髓Handler
详解Android主流框架不可或缺的基石
站在源码的肩膀上全解Scroller工作机制
Android多分辨率适配框架(1)— 核心基础
Android多分辨率适配框架(2)— 原理剖析
Android多分辨率适配框架(3)— 使用指南
在上一篇博客中,我们从index.jsp传递参数到Controller中;Controller收到参数后再跳转到test.jsp。即执行流程为:index.jsp —> Controller —> test.jsp。现在我们可以把这些来自index.jsp的参数显示到test.jsp中么?答案是肯定的。这就是本篇博客要讲的参数回显。
简单数据类型的回显
对于简单数据类型,如:Integer、String、Float等使用Model将传入的参数再放到request域实现显示。对于该功能的实现非常简单,我们只需要在Controller的方法中添加Model类型的参数即可,例如:
//1、测试SpringMVC传递int类型参数及其回显
@RequestMapping(value="/testInt")
public String testInt(Integer id,Model model){
System.out.println("---> id="+id);
model.addAttribute("id", id);
return "test";
}
然后我们在其他页面(例如此处的test.jsp)页面中取出该值即可:
1、测试SpringMVC传递int类型参数,回显结果:${id}
Object数据类型的回显
SpringMVC默认支持Object数据回显,SpringMVC自动将形参中的Object重新放回request域中,request的key为Object的类名(请注意,类名首字母小写)。例如,我们在Controller中这么写:
//3、测试SpringMVC传递Object类型参数及其回显
@RequestMapping(value="/testUser")
public String testUser(User user){
System.out.println("---> user="+user);
return "test";
}
然后在test.jsp中取出参数:
3、测试SpringMVC传递Object类型参数,回显结果:${user.id} ${user.username} ${user.sex} ${user.address}
传递数据至下一个页面
我们常常有这样的需求:将数据传递至下一个页面。其实,这个也非常简单可以采用与Model类似的ModelMap即可。例如,我们在Controller中这么写:
//4、测试SpringMVC传递Object至下一个页面
@RequestMapping(value="/testObject")
public String testObject(ModelMap modelMap){
User u=new User();
u.setId(9527);
u.setAddress("北京");
u.setSex("男");
u.setUsername("周星星");
modelMap.put("u", u);
System.out.println("---> user="+u);
return "test";
}
然后在test.jsp中取出参数:
4、测试SpringMVC传递Object至下一个页面,回显结果:${u.id} ${u.username} ${u.sex} ${u.address}<br>
按照惯例,贴出该项目中的关键代码;其余部分请参见上一篇博客,不再赘述
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SpringMVC传递参数</title>
</head>
<body>
<hr size="2" color="red" />
<b>1、测试SpringMVC传递int类型参数及其回显</b><br><br>
<form action="${pageContext.request.contextPath }/user/testInt.do" method="post">
ID:<input type="text" name="id" id="testIntId"> <br><br>
<input type="submit" value="提交">
</form>
<hr size="2" color="red" />
<b>2、测试SpringMVC传递String类型参数及其回显</b><br><br>
<form action="${pageContext.request.contextPath }/user/testString.do" method="post">
姓名:<input type="text" name="name" id="testNameId"> <br><br>
<input type="submit" value="提交">
</form>
<hr size="2" color="red" />
<b>3、测试SpringMVC传递Object类型参数及其回显</b><br><br>
<form action="${pageContext.request.contextPath }/user/testUser.do" method="post">
ID: <input type="text" name="id" id="testId">
姓名:<input type="text" name="username" id="testUsername">
性别:<input type="text" name="sex" id="testSex">
地址:<input type="text" name="address" id="testAddress"> <br><br>
<input type="submit" value="提交">
</form>
<hr size="2" color="red" />
<b>4、测试SpringMVC传递Object至下一个页面</b><br><br>
<form action="${pageContext.request.contextPath }/user/testObject.do" method="post">
<br>
<input type="submit" value="提交" >
</form>
</body>
</html>
Controller
/**
* @author 原创作者:谷哥的小弟
* @blog 博客地址:http://blog.csdn.net/lfdfhl
* @time 创建时间:2017年7月29日 上午9:58:56
* @info 描述信息:SpringMVC回显数据
*/
package cn.com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.com.domain.User;
@Controller
@RequestMapping("/user")
public class AnnotationController {
//1、测试SpringMVC传递int类型参数及其回显
@RequestMapping(value="/testInt")
public String testInt(Integer id,Model model){
System.out.println("---> id="+id);
model.addAttribute("id", id);
return "test";
}
//2、测试SpringMVC传递String类型参数及其回显
@RequestMapping(value="/testString")
public String testString(String name,Model model){
System.out.println("---> name="+name);
model.addAttribute("name", name);
return "test";
}
//3、测试SpringMVC传递Object类型参数及其回显
@RequestMapping(value="/testUser")
public String testUser(User user){
System.out.println("---> user="+user);
return "test";
}
//4、测试SpringMVC传递Object至下一个页面
@RequestMapping(value="/testObject")
public String testObject(ModelMap modelMap){
User u=new User();
u.setId(9527);
u.setAddress("北京");
u.setSex("男");
u.setUsername("周星星");
modelMap.put("u", u);
System.out.println("---> user="+u);
return "test";
}
}
test.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>SpringMVC回显数据</title>
<style type="text/css">
p {
font-size: 40px;
font-family: 宋体;
color: red;
background-color: pink;
}
</style>
</head>
<body>
<p>测试SpringMVC回显数据</p>
<br>
1、测试SpringMVC传递int类型参数,回显结果:${id}<br>
2、测试SpringMVC传递String类型参数,回显结果:${name}<br>
3、测试SpringMVC传递Object类型参数,回显结果:${user.id} ${user.username} ${user.sex} ${user.address}<br>
4、测试SpringMVC传递Object至下一个页面,回显结果:${u.id} ${u.username} ${u.sex} ${u.address}<br>
</body>
</html>
效果图:
SpringMVC札集(05)——SpringMVC参数回显的更多相关文章
- SpringMVC札集(04)——SpringMVC传递参数
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SpringMVC札集(02)——SpringMVC入门完整详细示例(下)
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SpringMVC札集(01)——SpringMVC入门完整详细示例(上)
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SpringMVC学习(四)———— 数据回显与自定义异常处理器
一.数据回显技术 Springmvc默认支持对pojo类型的数据回显,默认不支持简单类型的数据回显 1.1.什么是数据回显? 在信息校验时,如果发生校验错误,那么把校验的数据信息,依然停留在当前页面, ...
- SpringMVC札集(07)——JSON数据
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SpringMVC札集(10)——SSM框架整合
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SpringMVC札集(09)——拦截器
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SpringMVC札集(08)——文件上传
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SpringMVC札集(03)——基于注解的SpringMVC入门完整详细示例
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
随机推荐
- web.xml filter配置
filter介绍: filter主要用于对用户请求request进行预处理,和对Response进行后处理,是个典型的处理链. 详细解析起来就是:Filter对用户请求进行预处理,接着将请求HttpS ...
- 20145312 《Java程序设计》第四周学习总结
20145312 <Java程序设计>第四周学习总结 学习笔记 Chapter 6 6.1何为继承 1.定义:面向对象中子类继承父类,避免重复的行为定义. 6.1.1 继承共同行为 1.以 ...
- LeetCode—— Partition Equal Subset Sum
Question Given a non-empty array containing only positive integers, find if the array can be partiti ...
- BZOJ4767 两双手
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- Java循环语句之 while
生活中,有些时候为了完成任务,需要重复的进行某些动作.如参加 10000 米长跑,需要绕 400 米的赛道反复的跑 25 圈.在 Java 中实现功能时,也经常需要重复执行某些代码,例如,我们为了表示 ...
- wampserver安装及安装中可能遇到的问题
首先wampserver是windows apache Mysql PHP 集成开发环境,即在windows下的apache.php和mysql的服务器.因此wampserver是一个服务器端应用程序 ...
- KVM irqfd and ioeventfd
In previous article vhost architecture we mentioned that vhost and the guest signal each other by ir ...
- es6之Iterator
1.任何数据结构只要部署了Iterator接口(本质是一个指针对象),也就是部署了Symbol.iterator属性,便可以完成遍历操作:数组原生就具备Iterator接口,就可以用for...of遍 ...
- java中使用Lambda表达式的5种语法
1,标准写法 思考下述情况: String[] arr = {"program", "creek", "is", "a" ...
- WPF:理解TileBrush(ImageBrush,DrawingBrush和VisualBrush)
ImageBrush:利用图像绘制区域 ImageBrush 是一种将自身内容定义为图像的 TileBrush,图像通过它的 ImageSource 属性指定. 您可以控制图像的拉伸.对齐和平铺方式, ...