附:实体类

Class : User

package com.c61.entity;

import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; import com.alibaba.fastjson.annotation.JSONField; public class User {
private Integer id;
private String name;
//@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern="yyyy-MM-dd")//定制在接收请求参数时的日期格式
@JSONField(format="yyyy-MM-dd")//作用在java序列化成json时
private Date birth;
private String dateStr; public String getDateStr() {
return dateStr;
}
public void setDateStr(String dateStr) {
this.dateStr = dateStr;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
this.dateStr=format.format(birth);
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", birth=" + birth + "]";
}
public User(){}
public User(Integer id, String name, Date birth) {
super();
this.id = id;
this.name = name;
this.birth = birth;
} }

Class : ValueObject

package com.c61.entity;

import java.util.List;

public class ValueObject{
private List<User> users;
private String[] ids;
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public String[] getIds() {
return ids;
}
public void setIds(String[] ids) {
this.ids = ids;
}
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- 前端控制器
/=默认的url-pattern
/a/b/c /a /a/d/c
/a/d
/a
/
*注意:此控制器默认加载/WEB-INF下的xxx-servlet.xml文件
:其中xxx等于【DispatcherServlet的配置名】
-->
<servlet>
<servlet-name>mvc61</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc62.xml</param-value>
</init-param>
<!-- 随项目启动而启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc61</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- 专治Post请求参数乱码 -->
<filter>
<filter-name>encoding61</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 将请求的编码方式设置为utf-8 -->
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding61</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

2.配置控制器

Class : ParamController

package com.c61.controller;

import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import com.c61.entity.User;
import com.c61.entity.ValueObject; /**
*
* @author admin
* 收参测试 controller
*/
@Controller
@RequestMapping(value="/mvc")//等价于namespace
public class ParamController {
/**
*
* @param name
* @param id
* @param birth
* @return
* 零散传参:请求参数和方法参数同名即可
*/
//url?id=61&name=zhoushanlin&birth=2016/09/29 11:15:40
@RequestMapping("/param")//等价于<action name="mvc1"
public String testMVC(String name,Integer id,Date birth){
System.out.println(name+" "+id+" "+birth);
return "index";
} /**
*
* @param name
* @param id
* @param birth
* @return
* 零散传参
* 细节:
* @RequestParam(required=true,defaultValue="lime",value="abc")
* required=参数是否是必需传递的,如果参数上加了此注解,则required默认为true
* defaultValue=参数的默认值,如果参数没有收到数据,则取默认值
* value=定制请求参数的名称
* @DateTimeFormat(pattern="yyyy-MM-dd")
* pattern=定制日期的收参格式
*/
//url?id=61&name=zhoushanlin&birth=2016/09/29 11:15:40
@RequestMapping("/param2")//等价于<action name="mvc1"
public String testMVC2(@RequestParam(required=true,defaultValue="lime") String name,
@RequestParam Integer id,
@RequestParam(value="b") @DateTimeFormat(pattern="yyyy-MM-dd")Date birth){
System.out.println(name+" "+id+" "+birth);
return "index";
}
/**
* 以实体为整体
* url?id=61&name=c61&birth=2016/09/29
* *请求参数名 要和 实体属性名同名即可
*/
@RequestMapping("/param3")//等价于<action name="mvc1"
public String testMVC3(User user){
System.out.println(user);
return "index";
}
/**
* url?hobby61=xxx&hobby61=xxx&hobby61=xxxx
* 以数组为整体1
*/
@RequestMapping("/param4")//等价于<action name="mvc1"
public String testMVC4(String[] hobby61){
for(String hobby:hobby61){
System.out.println(hobby);
}
return "index";
}
/**
* 以集合为整体 (了解)
* url?users[0].id=1&users[0].name=c61&users[0].birth=2016-09-29
* &users[1].id=2&users[1].name=c61&users[1].birth=2016-09-29
* &users[2].id=3&users[2].name=c61&users[2].birth=2016-09-29
*/
@RequestMapping("/param5")//等价于<action name="mvc1"
public String testMVC5(ValueObject vo61){
for(User user:vo61.getUsers()){
System.out.println(user);
}
return "index";
}
}

3 配置视图

View : index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
This is my JSP page. <br>
</body>
</html>

零散收参 : 请求参数名 要和 方法参数名同名即可

View : param.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<form method="post" action="${pageContext.request.contextPath}/mvc/param">
<input type="text" name="id"/>
<input type="text" name="name"/>
<input type="text" name="birth"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>

Console :

实体为整体收参 : 请求参数名 要和 实体属性名同名即可

View : param.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<form method="post" action="${pageContext.request.contextPath}/mvc/param3">
<input type="text" name="id"/>
<input type="text" name="name"/>
<input type="text" name="birth"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>

Console :

数组为整体收参 :

View : param.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<form method="post" action="${pageContext.request.contextPath}/mvc/param4">
<label><input name="hobby61" type="checkbox" value="hiking" />徒步旅行</label>
<label><input name="hobby61" type="checkbox" value="swimming" />游泳</label>
<input type="submit" value="提交"/>
</form>
</body>
</html>

Console :

集合为整体收参 :

View : param.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<form method="post" action="${pageContext.request.contextPath}/mvc/param5">
用户A:<br>
<input name="users[0].id" type="text" value="" />
<input name="users[0].name" type="text" value="" />
<input name="users[0].birth" type="text" value="" /><br>
用户B:<br>
<input name="users[1].id" type="text" value="" />
<input name="users[1].name" type="text" value="" />
<input name="users[1].birth" type="text" value="" /><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>

Console :

收参乱码 :

1>get请求:在服务器的配置中添加URIEncoding="utf-8"
2>post请求:在项目中注册CharacterEncodingFilter
<!-- 专治Post请求参数乱码 -->
<filter>
<filter-name>encoding61</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 将请求的编码方式设置为utf-8 -->
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding61</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

啦啦啦

啦啦啦

啦啦啦

SpringMVC -- 梗概--源码--壹--收参的更多相关文章

  1. SpringMVC -- 梗概--源码--壹--跳转

    1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ...

  2. SpringMVC -- 梗概--源码--壹--springMVC json处理

    附:实体类 Class : User package com.c61.entity; import java.text.SimpleDateFormat; import java.util.Date; ...

  3. SpringMVC -- 梗概--源码--壹--数据传递

    附:实体类 Class : User package com.c61.entity; import java.text.SimpleDateFormat; import java.util.Date; ...

  4. SpringMVC -- 梗概--源码--贰--RestFul收参(了解) @PathVariable

    1>定制方式: //如下两个路径都可以访问到如下方法,请求路径不同,则name61和pwd61匹配到的值不同 //http://localhost:8989/appname/ful/lime/1 ...

  5. SpringMVC -- 梗概--源码--贰--下载

    1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ...

  6. SpringMVC -- 梗概--源码--贰--上传

    1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ...

  7. SpringMVC -- 梗概--源码--贰--拦截器:Interceptor

    附:实体类 1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app versi ...

  8. SpringMVC -- 梗概--源码--贰--异常管理

    附:实体类 Class : User package com.c61.entity; import java.text.SimpleDateFormat; import java.util.Date; ...

  9. SpringMVC -- 梗概--源码--贰--mvc:annotation-driven

    1>在springMVC的处理流程中,有两个重要组件:HandlerMapping和HandlerAdapter 分别负责解析Handler和执行Handler 2>如果配置了<mv ...

随机推荐

  1. python django查询12306火车票

    逢年过节,想坐个高铁票,都得上12306去买票,但用过的都会发现,它会把临近站点的也筛出来了.但有时我们压根就不会考虑买到临近站点的. 另一方面,在购票高峰期,有可能你要的出发站到目的站都没有票了,这 ...

  2. Python 字典的操作

    #-*- coding:utf-8 -*- people = {"name":"jack","age":18,"addr" ...

  3. office 2013 快速换KEY

    在有些时候,我们总会碰到一些需要更换Office安装Key(序列号.密钥)的情形,例如购买或朋友赠了新的Key等等.网上搜索的话,有很多种更改变换Office 2010序列号办法,今天就来挨个介绍一下 ...

  4. Ogre 编辑器一(MyGUI+Ogre整合与主界面)

    在查看Ogre例子时,想看材质要里的纹理,着色器代码都需要每个去查找,非常麻烦.也想看更新每个Ogre里的对象后有什么效果.然后看到Compositor组件与粒子组件时,想到能实时编辑着色器代码实时更 ...

  5. Numpy 利用数组进行数据处理

    Numpy数组使你可以将许多种数据处理任务表述为简洁的数组表达式(否则需要编写循环). 用数组表达式代替循环的做法,通常被称为矢量化.一般来说,矢量化数组运算要比等价的纯跑一趟湖南快 上一两个数量级( ...

  6. K-SVD算法

    它与K-mean算法原理上是类似的: K-mean 算法: (之前写过:http://www.cnblogs.com/yinheyi/p/6132362.html) 对于初始化的类别中心,可以看作初化 ...

  7. SpringBoot系列十一:SpringBoot整合Restful架构(使用 RestTemplate 模版实现 Rest 服务调用、Swagger 集成、动态修改日志级别)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot整合Restful架构 2.背景 Spring 与 Restful 整合才是微架构的核心,虽然在整 ...

  8. 第11章:sed进阶操作

    第11章:sed进阶操作 sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法 sed命令行格式为 ...

  9. 非常酷的jQuery/HTML5图片滑块特效 带弹性菜单

    新的一周刚刚开始,当我迷迷糊糊坐在办公桌前时,又不自主的去看了一些jQuery和HTML5的应用插件,今天我们来看一款非常酷的jQuery/HTML5图片滑块特效,这款插件的特点是图片上不错的弹性菜单 ...

  10. [mobile angular ui]MAUI中的font awesome图标

    MAUI中用font awesome替换了glyphicon,但是FA中都有哪些可用的图标呢,在网上搜了一张font awesome的对照表,使用时记着把其中的icon-xxx替换为fa-xxx就可以 ...