SpringMVC入门 -- 参数绑定
一、REST与RESTful
1、简介
(1)REST(Representational State Transfer):表现层状态转移,一种软件架构风格,不是标准。REST描述的是在网络中client和server的一种交互形式,即资源在网络中以某种表现形式进行状态转移。
(2)基于REST构建的API就是Restful风格。
2、相关概念
(1)资源(Resources):指的是网络上的一个具体的信息(文本、图片等),通过一个URL可以唯一的指向它。
(2)表现层(Representational ):将信息表现出来的形式,比如使用txt表示文本,或者html表示文本。
(3)状态转移(State Transfer):客户端每发送一次请求到服务器,服务器响应会涉及到数据以及状态的切换。而HTTP属于一种无状态的协议,所有状态均保存在服务端,客户端想要去操作服务端,则需要通过某种手段(HTTP协议里的动词)使服务端发生状态转移,且显示在表现层。
3、常见HTTP动词
(1)GET(SELECT): 从服务器获取资源(一项或多项)
(2)POST(CREATE): 在服务器新建一个资源
(3)PUT(UPDATE): 在服务器更新资源(客户端提供改变后的完整资源)
(4)DELETE(DELETE):从服务器删除资源。
4、使用样例
(1)浏览器的 form 表单只支持 GET 与 POST请求,不支持 DELETE、PUT 请求。
(2)Spring 3.0后,添加了一个过滤器(HiddenHttpMethodFilter),可以将POST请求转为DELETE、PUT请求,从而整体支持 DELETE、PUT、GET、POST。
(3)发送 PUT 请求和 DELETE 请求的实现步骤:
step1:需要在web.xml配置 HiddenHttpMethodFilter
step2:需要发送 POST 请求
step3:需要在发送 POST 请求时携带一个 name="_method" 的隐藏域, 值为 DELETE 或 PUT
【主要操作:】 【web.xml中配置 过滤器】
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> 【即web.xml】
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"> <servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!--配置过滤器,将post请求转为delete,put-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app> 【index.jsp中配置 请求】
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<html>
<head>
<title>首页</title>
</head>
<body>
<%--测试get请求--%>
<form action="/testRestful/1">
<input type="submit" value="test get"/>
</form> <%--测试post请求--%>
<form action="/testRestful" method="post">
<input type="submit" value="test post"/>
</form> <%--测试post请求,不转为delete、put请求--%>
<form action="/testRestful/1" method="post">
<input type="submit" value="test post2"/>
</form> <%--测试post请求,转为put请求--%>
<form action="/testRestful/1" method="post">
<input type="hidden" name="_method" value="PUT"/>
<input type="submit" value="test put"/>
</form> <%--测试post请求,转为delete请求--%>
<form action="/testRestful/1" method="post">
<input type="hidden" name="_method" value="DELETE"/>
<input type="submit" value="test delete"/>
</form>
</body>
</html> 【HelloController.java 中处理请求:】
package com.lyh.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView; @Controller
public class HelloController { // 测试get请求,获取某条数据
@RequestMapping(value = "/testRestful/{id}", method = RequestMethod.GET)
public ModelAndView testGet(@PathVariable("id") Integer id) {
ModelAndView model = new ModelAndView("hello");
model.getModel().put("msg", id);
model.getModel().put("message", "test get");
return model;
} // 测试post请求,插入某条数据
@RequestMapping(value = "/testRestful", method = RequestMethod.POST)
public ModelAndView testPost() {
ModelAndView model = new ModelAndView("hello");
model.getModel().put("message", "test post");
return model;
} // 测试post请求,不转为delete、put请求
@RequestMapping(value = "/testRestful/{id}", method = RequestMethod.POST)
public ModelAndView testPost2(@PathVariable("id") Integer id) {
ModelAndView model = new ModelAndView("hello");
model.getModel().put("msg", id);
model.getModel().put("message", "test post2");
return model;
} // 测试post请求,转为put请求,更新某条数据
@RequestMapping(value = "/testRestful/{id}", method = RequestMethod.PUT)
public ModelAndView testPut(@PathVariable("id") Integer id) {
ModelAndView model = new ModelAndView("hello");
model.getModel().put("msg", id);
model.getModel().put("message", "test put");
return model;
} // 测试post请求,转为delete请求,删除某条数据
@RequestMapping(value = "/testRestful/{id}", method = RequestMethod.DELETE)
public ModelAndView testDelete(@PathVariable("id") Integer id) {
ModelAndView model = new ModelAndView("hello");
model.getModel().put("msg", id);
model.getModel().put("message", "test delete");
return model;
}
} 【hello.jsp 跳转的页面】
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<html>
<head>
<title>Test Restful</title>
</head>
<body>
<h2>${message}</h2>
<h2>${msg}</h2>
</body>
</html> 【dispatcher-servlet.xml】
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.lyh.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven />
<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/><!--设置JSP文件的目录位置-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
项目结构如下:其余操作可以参考 https://www.cnblogs.com/l-y-h/p/11502440.html
若访问delete、put请求出现405问题,在当前 jsp 页面的 头部添加 isErrorPage="true"。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true"%>
二、方法参数常用注解
1、@RequestMapping
(1)简介
SpringMVC提供的一个注解,用于处理指定的请求。
请求经过DispatcherServlet后,通过@RequestMapping的映射信息确定所需对应的处理方法。
@RequestMapping可以在控制器 的类名前、方法名前定义。
【用法样例:】 package com.lyh.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/test")
public class HelloController { @RequestMapping("/hello")
public String show(){
return "hello";
}
}
(2)参数
value:表示请求的URL。
method:表示请求方法(GET、POST、DELETE、PUT)。
params:表示请求参数。
headers:表示请求头的映射条件。
注:
param1:表示请求必须包含名为 param1 的请求参数。
!param2:表示请求不能包含名为 parma2 的请求参数。
param1 != value1: 表示请求包含名为 param1 的请求参数,且其值不为 value1。
{"param1 = value1", "param2"}:表示请求必须包含param1,param2,且param1的值为value1。 value 地址支持通配符。
? :表示匹配文件名中的一个字符。比如:/hello/? ,可以获取 /hello/2,不能获取/hello/22
* :表示匹配文件名中任意一个字符。比如:/hello/* ,可以获取 /hello/2, 可以获取/hello/22,不能获取/hello/2/2
** :表示匹配多层路径。比如:/hello/**,可以获取/hello/2,可以获取/hello/22,可以获取/hello/2/2. 【用法样例:】 package com.lyh.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; @Controller
@RequestMapping("/test")
public class HelloController { @RequestMapping(value = "/hello", params = {"username", "age != 10"}, method = RequestMethod.GET, headers = "Referer: https://www.baidu.com/")
public String show(){
return "hello";
}
}
2、@PathVariable
(1)简介
SpringMVC提供的一个注解,在方法参数中使用,用于获取参数。
Spring3.0后支持 带占位符的URL,即相当于 /get/{id} 的形式。通过@PathVariable 可以将URL中的占位符参数绑定到方法的参数中。
【用法样例:】 package com.lyh.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; @Controller
public class HelloController { @RequestMapping("/get/{id}")
public String show(@PathVariable("id") Integer id){
return "hello";
}
}
3、@RequestParam
(1)简介
SpringMVC提供的一个注解,在方法参数中使用,用于获取参数。
@RequestParam 用于获取表单参数。等价于request.getParameter("name")。而@PathVariable 用于获取 URL 中的占位符。
(2)参数
value:用于获取参数。
required :表示是否需要该数据,默认为true,若不存在,则会抛出异常。
defaultValue :表示默认的值,对于包装类型,默认为null,可以不设值,对于基本类型数据,需要设个初始值。
【举例:】
【index.jsp】
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<html>
<head>
<title>首页</title>
</head>
<body>
<form action="/testRequestParam/1">
<label for="nickname">姓名</label>
<input type="text" id="nickname" name ="nickname" placeholder="请输入姓名..." /> <label for="password">密码</label>
<input type="password" id="password" name ="password" placeholder="请输入密码..." /> <label for="age">年龄</label>
<input type="text" id="age" name ="age" placeholder="请输入年龄..." /> <label for="salary">工资</label>
<input type="text" id="salary" name ="salary" placeholder="请输入工资..." />
<input type="submit" value="test requestParam"/>
</form>
</body>
</html> 【hello.jsp】
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<html>
<head>
<title>Test RequestParam</title>
</head>
<body>
<h2>${message}</h2>
<h2>${msg}</h2>
</body>
</html> 【HelloController.java】
package com.lyh.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView; @Controller
public class HelloController { @RequestMapping(value = "/testRequestParam/{id}")
public ModelAndView testRequestParam(@PathVariable("id") Integer id, @RequestParam(value = "nickname") String nickname,
@RequestParam(value = "password") String password, @RequestParam(value = "age", required = false) Integer age,
@RequestParam(value = "salary", required = false, defaultValue = "0.0") double salary) {
ModelAndView model = new ModelAndView("hello");
model.getModel().put("msg", id + "," + nickname + "," + password + "," + age + "," + salary);
model.getModel().put("message", "test RequestParam");
return model;
} }
4、@RequestHeader
(1)简介
SpringMVC提供的一个注解,在方法参数中使用,用于获取请求头参数。
【index.jsp】
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<html>
<head>
<title>首页</title>
</head>
<body>
<a href="/testRequestHeader/1">testRequestHeader</a>
</body>
</html> 【hello.jsp】
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<html>
<head>
<title>Test RequestHeader</title>
</head>
<body>
<h2>${message}</h2>
<h2>${msg}</h2>
</body>
</html> 【HelloController.java】
package com.lyh.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView; @Controller
public class HelloController { @RequestMapping(value = "/testRequestHeader/{id}")
public ModelAndView testRequestHeader(@PathVariable("id") Integer id, @RequestHeader("Accept-Language") String language) {
ModelAndView model = new ModelAndView("hello");
model.getModel().put("msg", id + "," + language);
model.getModel().put("message", "test RequestHeader");
return model;
}
}
5、@CookieValue
(1)简介
SpringMVC提供的一个注解,在方法参数中使用,用于获取cookie参数。
【index.jsp】
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<html>
<head>
<title>首页</title>
</head>
<body>
<a href="/testCookieValue/1">testCookieValue</a>
</body>
</html> 【hello.jsp】
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<html>
<head>
<title>Test CookieValue</title>
</head>
<body>
<h2>${message}</h2>
<h2>${msg}</h2>
</body>
</html> 【HelloController.java】
package com.lyh.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView; @Controller
public class HelloController { @RequestMapping(value = "/testCookieValue/{id}")
public ModelAndView testCookieValue(@PathVariable("id") Integer id, @CookieValue("JSESSIONID") String jsessionId) {
ModelAndView model = new ModelAndView("hello");
model.getModel().put("msg", id + "," + jsessionId);
model.getModel().put("message", "test CookieValue");
return model;
} }
6、自动给对象绑定请求参数值
(1)简介
SpringMVC可以根据请求参数名以及对象(POJO,Plain Ordinary Java Object)的属性名 自动匹配,自动为该对象绑定属性值,同时支持级联属性(如:"dept.salary")。
【新建实体类:DeptEntity.java】
package com.lyh.entity; public class DeptEntity {
private String deptName;
private Double salary; public String getDeptName() {
return deptName;
} public void setDeptName(String deptName) {
this.deptName = deptName;
} public Double getSalary() {
return salary;
} public void setSalary(Double salary) {
this.salary = salary;
} @Override
public String toString() {
return "DeptEntity{" +
"deptName='" + deptName + '\'' +
", salary=" + salary +
'}';
}
} 【新建实体类:PersonEntity.java】
package com.lyh.entity; public class PersonEntity {
private String name;
private String password;
private DeptEntity dept; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public DeptEntity getDept() {
return dept;
} public void setDept(DeptEntity dept) {
this.dept = dept;
} @Override
public String toString() {
return "PersonEntity{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
", dept=" + dept +
'}';
}
} 【index.jsp】
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<html>
<head>
<title>首页</title>
</head>
<body>
<form action="/testPOJO/1">
<label for="name">姓名</label>
<input type="text" id="name" name ="name" placeholder="请输入姓名..." /> <label for="password">密码</label>
<input type="password" id="password" name ="password" placeholder="请输入密码..." /> <label for="deptName">部门名</label>
<input type="text" id="deptName" name ="dept.deptName" placeholder="请输入部门名..." /> <label for="salary">工资</label>
<input type="text" id="salary" name ="dept.salary" placeholder="请输入工资..." />
<input type="submit" value="test POJO"/>
</form>
</body>
</html> 【hello.jsp】
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<html>
<head>
<title>Test POJO</title>
</head>
<body>
<h2>${message}</h2>
<h2>${msg}</h2>
</body>
</html> 【HelloController.java】
package com.lyh.controller; import com.lyh.entity.PersonEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView; @Controller
public class HelloController { @RequestMapping(value = "/testPOJO/{id}")
public ModelAndView testPOJO(@PathVariable("id") Integer id, PersonEntity person) {
ModelAndView model = new ModelAndView("hello");
model.getModel().put("msg", id + "," + person);
model.getModel().put("message", "test POJO");
return model;
} }
对于能映射到的属性,会自动赋值,否则为默认值。使用级联赋值时,需要注意变量名不能写错了。
SpringMVC入门 -- 参数绑定的更多相关文章
- springmvc(2)--参数绑定
一.以实例来看springmvc各种参数绑定方式 先定义个dto类: public class RestInDto implements Serializable { private static ...
- (转)SpringMVC学习(六)——SpringMVC高级参数绑定与@RequestMapping注解
http://blog.csdn.net/yerenyuan_pku/article/details/72511749 高级参数绑定 现在进入SpringMVC高级参数绑定的学习,本文所有案例代码的编 ...
- 阶段3 3.SpringMVC·_02.参数绑定及自定义类型转换_1 请求参数绑定入门
请求参数的绑定 参数绑定 创建新的页面 给方法加上注解 前面没有斜线 重新部署项目 传递一个username的值 后台方法接收 重新部署项目 再传一个password的值 再输出password ja ...
- SpringMVC【参数绑定、数据回显、文件上传】
前言 本文主要讲解的知识点如下: 参数绑定 数据回显 文件上传 参数绑定 我们在Controller使用方法参数接收值,就是把web端的值给接收到Controller中处理,这个过程就叫做参数绑定.. ...
- SpringMVC学习--参数绑定
spring参数绑定过程 从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上.springmvc中,接收页面提交的数据是通过方法形参来接收 ...
- SpringMVC(三) —— 参数绑定和数据回显
参数绑定的过程:就是页面向后台传递参数,后台接受的一个过程. 默认支持的参数类型:(就是你在方法上以形参的形式去定义一下的类型,就可以直接使用它) HttpServletRequest HttpSer ...
- springmvc(三) 参数绑定、
前面两章就介绍了什么是springmvc,springmvc的框架原理,并且会简单的使用springmvc以及ssm的整合,从这一章节来看,就开始讲解springmvc的各种功能实现,慢慢消化 --W ...
- SpringMVC中参数绑定
SpringMVC中请求参数的接收主要有两种方式, 一种是基于HttpServletRequest对象获取, 另外一种是通过Controller中的形参获取 一 通过HttpServletReque ...
- SpringMVC学习笔记之二(SpringMVC高级参数绑定)
一.高级参数绑定 1.1 绑定数组 需求:在商品列表页面选中多个商品,然后删除. 需求分析:功能要求商品列表页面中的每个商品前有一个checkbok,选中多个商品后点击删除按钮把商品id传递给Cont ...
随机推荐
- 【编译系统02】编译器 - 语义分析器(semantic)的简单设计思路(变量类与变量表)
当我们分析到 "int n;",说明其已经定义了一个变量,之后又遇到一个 "n=3",我们从哪里去找这个n并且赋值呢? 答案是:通过我们定义的 变量表(Tabl ...
- Spring Boot 2.2.2.RELEASE 版本中文参考文档
写在前面 在我初次接触MongoDB的时候,是为了做一个监控系统和日志分析系统.当时在用Java操作MongoDB数据里的数据的时候,都是在网上查找Demo示例然后完成的功能,相信大家也同样的体会,网 ...
- swift多线程定时器
swift多线程定时器的使用 func countDown(_ timeOut:Int,view: UIView){ var timeout = timeOut let queue:DispatchQ ...
- MyBatis之接口绑定方案及多参数传递
1.说明 所谓的MyBatis接口绑定,指的是实现创建一个接口后,把mapper.xml 由mybatis 生成接口的实现类,通过调用接口对象就可以获取mapper.xml 中编写的sql.在SS ...
- fastadmin安装定时插件报错 ZipArchive::extractTo(): Permission denied
环境linux上直接安装 如果你是在win开发号直接部署的应该是没问题 我是直接在linux安装的 这几天研了下fastadmin 想用他的定时可是在使用的时候报错 ZipArchive::e ...
- Mysql 如何创建一张临时表
mysql 利用 temporary 关键字就可以创建出一个临时表.创建的这张表会在与服务器的会话终止时自动消失 语法:create temporary table tbl_name...; 规则:每 ...
- js中新增的Symbol
在ES6之前,js的的基本数据类型有String Number Null Boolean undefined Object6中数据类型,Symbol是一种新增加的基本数据类型 特性 Symbol 不需 ...
- A1100 Mars Numbers (20 分)
一.技术总结 这一题可以使用map进行想打印存储,因为数据量不是很大,最后直接输出.但是还是觉得没有必要. 主要考虑两个问题,首先是数字转化为字符串,实质就是进制转化,但是有点不同,如果十位有数字,个 ...
- django执行源生的mysql语句
执行源生sql在python中我们可以借助pymysql模块来进行执行源生sql关于pymysql执行源生sql可以看我的另外一篇博文:pymysql应用 本篇博客只介绍django中执行的源生sql ...
- C#读取匿名对象的属性值的方法总结
目录 1.通过反射的方式获取属性值 2.新建个扩展方法,将object转成对应的匿名对象 通过new出匿名对象,可以直接调用该匿名对象的属性名,获取属性值. var objUser = new {Na ...