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 ...
随机推荐
- Spring Aop之Cglib实现原理详解
Spring Aop实现对目标对象的代理,AOP的两种实现方式:Jdk代理和Cglib代理.这两种代理的区别在于,Jdk代理与目标类都会实现同一个接口,并且在代理类中会调用目标类中被代理的方法,调用者 ...
- C++ 程序崩溃时生成Dump文件
#include <DbgHelp.h> //生产DUMP文件 int GenerateMiniDump(HANDLE hFile, PEXCEPTION_POINTERS pExcept ...
- redis:Invalid input of type: 'bool' type. Convert to a byte,string or number first
分析:出现此错误的原因是redis版本过高导致的,因此降低redis版本即可 解决: pip install -U redis==2.10.6
- LeetCode——Longest Word in Dictionary through Deleting
1. Question Given a string and a string dictionary, find the longest string in the dictionary that c ...
- counting the buildings - 第一类斯特灵数
2017-08-10 21:10:08 writer:pprp //TLE #include <iostream> #include <cstdio> #include < ...
- 北京联通光猫 F427 路由改桥接的方法
最近安装了一个联通的宽带,赠送的光猫是 中兴 F427,然后联通小哥给安装的时候,直接开启了光猫的路由功能. 不过联通这个光猫实在是太弱了,起码默认的帐号开启的功能实在是太弱了,没法完成以下几个功能: ...
- Pandas 的使用
1. 访问df结构中某条记录使用loc或者iloc属性.loc是按照index或者columns的具体值,iloc是按照其序值.访问类似于ndarray的访问,用序列分别表示一维和二维的位置. 例如: ...
- 洛谷P3601 签到题
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- SSH密钥登陆免密码方法
原帖地址:http://ask.apelearn.com/question/798 用Putty实现A机器远程登陆B机器,具体实现请看链接:http://www.cnblogs.com/ImJerry ...
- 蓄水池抽样算法 Reservoir Sampling
2018-03-05 14:06:40 问题描述:给出一个数据流,这个数据流的长度很大或者未知.并且对该数据流中数据只能访问一次.请写出一个随机选择算法,使得数据流中所有数据被选中的概率相等. 问题求 ...