自定义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)— 使用指南


在本篇博客中主要讲述jsp页面向SpringMVC传递参数,例如:int,String,对象,包装类型,数组,List,Map。为清楚阐述每种类型的变量传递,故针对每种类型参数的传递都单独写了对应的测试方法且在代码明确注释。


JSP页面

先来瞅瞅测试页面index.jsp的效果:

嗯哼,看到了不?——每个测试我也在页面上进行了标注,它们与Controller中的方法是依次对应的。代码如下:

<%@ 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传递包装类型参数</b><br><br>
    <form action="${pageContext.request.contextPath }/user/testUserCustom.do" method="post">
        ID: <input type="text" name="user.id" id="testId">
               姓名:<input type="text" name="user.username" id="testUsername">
               性别:<input type="text" name="user.sex" id="testSex">
               地址:<input type="text" name="user.address" id="testAddress"> <br><br>
            <input type="submit" value="提交">
    </form>

    <hr size="2" color="red" />
    <b>5、测试SpringMVC传递数组类型参数</b><br><br>
    <form action="${pageContext.request.contextPath }/user/testArray.do" method="post">
        选择1:<input type="checkbox" name="integerArray" value="9527" id="ids1">
        选择2:<input type="checkbox" name="integerArray" value="9528" id="ids2">
        选择3:<input type="checkbox" name="integerArray" value="9529" id="ids3"><br> <br>
            <input type="submit" value="提交">
    </form>

    <hr size="2" color="red" />
    <b>6、测试SpringMVC传递List类型参数</b><br><br>
    <form action="${pageContext.request.contextPath }/user/testUserList.do" method="post">
        ID: <input type="text" name="userList[0].id" id="testId0">
               姓名:<input type="text" name="userList[0].username" id="testUsername0">
               性别:<input type="text" name="userList[0].sex" id="testSex0">
               地址:<input type="text" name="userList[0].address" id="testAddress0"><br>
         ID: <input type="text" name="userList[1].id" id="testId1">
               姓名:<input type="text" name="userList[1].username" id="testUsername1">
               性别:<input type="text" name="userList[1].sex" id="testSex1">
               地址:<input type="text" name="userList[1].address" id="testAddress1"><br><br>
            <input type="submit" value="提交">
    </form>

    <hr size="2" color="red" />
    <b>7、测试SpringMVC传递Map类型参数</b><br><br>
    <form action="${pageContext.request.contextPath }/user/testUserMap.do" method="post">
        ID: <input type="text" name="map['id']" id="testId">
               姓名:<input type="text" name="map['username']" id="testUsername">
               性别:<input type="text" name="map['sex']" id="testSex">
               地址:<input type="text" name="map['address']" id="testAddress"> <br><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 java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.domain.User;
import cn.com.domain.UserCustom;

@Controller
@RequestMapping("/user")
public class AnnotationController {

    //1、测试SpringMVC传递int类型参数
    @RequestMapping(value="/testInt")
    public String testInt(Integer id){
        System.out.println("---> id="+id);
        return "test";
    }

    //2、测试SpringMVC传递String类型参数
    @RequestMapping(value="/testString")
    public String testString(String name){
        System.out.println("---> name="+name);
        return "test";
    }

    //3、测试SpringMVC传递Object类型参数
    @RequestMapping(value="/testUser")
    public String testUser(User user){
        System.out.println("---> user="+user);
        return "test";
    }

    //4、测试SpringMVC传递包装类型参数
    @RequestMapping(value="/testUserCustom")
    public String testUserCustom(UserCustom userCustom){
        System.out.println("---> userCustom="+userCustom.getUser());
        return "test";
    }

    //5、测试SpringMVC传递数组类型参数
    @RequestMapping(value="/testArray")
    public String testUserCustom(Integer[] integerArray){
        if(integerArray!=null){
            int length=integerArray.length;
            for(int i=0;i<length;i++){
                System.out.println("---> integerArray[i]="+integerArray[i]);
            }
        }
        return "test";
    }

    //6、测试SpringMVC传递List类型参数
    @RequestMapping(value="/testUserList")
    public String testUserList(UserCustom userCustom){
        if (userCustom!=null) {
            List<User> userList = userCustom.getUserList();
            if(userList!=null){
                for(User user:userList){
                    System.out.println("---> user="+user);
                }
            }
        }
        return "test";
    }

    //7、测试SpringMVC传递Map类型参数
    @RequestMapping(value="/testUserMap")
    public String testUserMap(UserCustom userCustom){
        if (userCustom!=null) {
            Map<String, Object> map = userCustom.getMap();
            Set<Entry<String, Object>> entrySet = map.entrySet();
            Iterator<Entry<String, Object>> iterator = entrySet.iterator();
            while(iterator.hasNext()){
                Entry<String, Object> entry = iterator.next();
                System.out.println(("---> entry="+entry.getKey()+","+entry.getValue()));
            }
        }
        return "test";
    }

}

小结:

  • 执行流程:index.jsp —> Controller —> test.jsp
  • jsp页面中的name属性的值应与Controller中方法参数的名字相对应。比如在第一个测试示例中jsp中为:name="id" 在Controller中方法参数为:Integer id
  • 在测试三中,jsp中的name属性的值即为Object对象的属性,比如id,username等
  • 在测试四种,测试包装类型与测试三很类似

在该项目中还用到了其它代码,在此一并贴出。


User

/**
* @author 原创作者:谷哥的小弟
* @blog   博客地址:http://blog.csdn.net/lfdfhl
* @time   创建时间:2017年7月29日 上午10:59:56
* @info   描述信息:User类
*/
package cn.com.domain;

public class User {

    private Integer id;
    private String username;
    private String sex;
    private String address;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", sex=" + sex
                + ", address=" + address + "]";
    }

}

UserCustom

/**
* @author 原创作者:谷哥的小弟
* @blog   博客地址:http://blog.csdn.net/lfdfhl
* @time   创建时间:2017年7月29日 上午11:38:46
* @info   描述信息:UserCustom类
*/
package cn.com.domain;

import java.util.List;
import java.util.Map;

public class UserCustom {

    private User user;
    private List<User> userList;
    private Map<String, Object> map;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }
}

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>
</body>
</html>

SpringMVC札集(04)——SpringMVC传递参数的更多相关文章

  1. SpringMVC札集(05)——SpringMVC参数回显

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  2. SpringMVC札集(01)——SpringMVC入门完整详细示例(上)

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  3. SpringMVC札集(02)——SpringMVC入门完整详细示例(下)

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  4. SpringMVC札集(07)——JSON数据

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  5. SpringMVC札集(10)——SSM框架整合

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  6. SpringMVC札集(09)——拦截器

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  7. SpringMVC札集(08)——文件上传

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  8. SpringMVC札集(03)——基于注解的SpringMVC入门完整详细示例

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  9. SpringMVC札集(06)——转发和重定向

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

随机推荐

  1. cl.exe 命令行编译sqlite3 sqlite3.dll及sqlite3.exe

    有点被宇宙最强的ide惯坏了,封装的太好,不能像gcc那样一步步了解其原理,其实强大的vs背后也有类似gcc的cl.exe 看到How To Compile SQLite http://sqlite. ...

  2. 照着官网来安装openstack pike之安装dashboard

    上文提到了利用命令行下使用openstack的命令来创建虚拟机,这里选择安装dashboard来安装基于web界面的openstack平台 利用dashboard界面来创建虚拟机 dashboard这 ...

  3. JAVA多线程本质分析

    多线程是Java开发中的重中之重,其重要性和难度,可见一斑.掌握并精通多线程开发,是每一个程序员的必修之课.哪怕中间的过程很痛苦,只要坚持了,并最终豁然开朗了,都是一种升华. 多线程的优化:合理利用C ...

  4. maven(一)maven自带的插件

    关于org.apache.maven.plugins 前言 maven提供了很多插件给我们使用,解释3个java环境常用的maven插件, maven-jar-plugin, maven-compli ...

  5. 【ML数学知识】极大似然估计

    它是建立在极大似然原理的基础上的一个统计方法,极大似然原理的直观想法是,一个随机试验如有若干个可能的结果A,B,C,... ,若在一次试验中,结果A出现了,那么可以认为实验条件对A的出现有利,也即出现 ...

  6. 随机数的生成 - rand(), srand()

    2017-08-20  17:43:29 writer:pprp 我们采用随机数可以对我们的算法进行大数据检验 /* name : 简单的随机数生成算法 writer : pprp declare : ...

  7. base64 原理

    Base64编码之所以称为Base64,是因为其使用64个字符来对任意数据进行编码,同理有Base32.Base16编码.标准Base64编码使用的64个字符为: 这64个字符是各种字符编码(比如AS ...

  8. gdb 调试coredump文件过程:

    第一步:首先需要一个进程的coredump文件,怎么搞出coredump文件呢? 1. ps -fax|grep                 进程名称 找到进程的pid 2.gdb -p pid ...

  9. Learning Perl 第九章习题第二题

    把输入文件中的所有Fred换成Larry, 不区分大小写. 知识点 1. 文本文件读写 2. 简单的正则替换 3. unless 的用法 4. $_ 的用法

  10. 【转】正向代理vs反向代理

    正向代理 正向代理:是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将 ...