前期准备
使用环境
- JDK:1.8
- Tomcat:9.0.3
- Spring:5.2.8
- Maven:3.6.3
- 编译器:IntelliJ IDEA 2019
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>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
ApplicationContext.xml配置(Spring核心配置文件)
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 开启spring注解驱动-->
<context:component-scan base-package="com.cjh"/>
<!-- 开启mvc注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
请求
1. 请求的发送与接收
以下写的浏览器和服务器的方式是相对应的,比如第一种请求就对应着第一种接收
1)浏览器发送请求的方式:
第一种:请求资源名(找的是具体的处理类),请求的方法上写上@RequestMapping注解
- 这种形式要求请求的处理类下只有一个方法
- 第二种:请求资源名?method=具体执行方法(找的是具体的处理类中的方法)
- 第三种:请求资源名(找的是具体的执行方法)-----常用
2)服务器接收请求的方式:
- 不论是对应浏览器的哪种发送方式,都需要使用@RequestMapping注解
- 第一种:在类上写上@RequestMapping(请求资源名)注解
- 第二种:在类上写上@RequestMapping(请求资源名)注解,在方法上也要写上@RequestMapping(params = {method=方法名})注解(method也可以换成其他名字)
- 第三种:直接在方法上写上@RequestMapping(请求资源名)
3)@RequestMapping注解中的其他方法
- path/value:用来存储请求资源名
- params:要求浏览器必须发送的参数,参数的形式是{"key=value",""},
- method:要求浏览器请求的方法(GET/POST)
- headers:要求浏览器必须携带的请求头({"Accept-Language",""})
- 注意:后面三个一旦写明,就是要求浏览器必须携带的东西,如果没有携带,那么服务器是不处理的
4)具体示例代码:
第一种方式:
- JSP
<%@ page contentType="text/html; charset=UTF-8" language="java" %> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>cai jin hong</title> <style> </style> </head> <body> <a href="userController.do">测试第一种请求方式</a> </html>
- java
@Controller @RequestMapping("userController.do") public class UserController { @RequestMapping public void testOne(){ System.out.println("test方法执行了"); } }
- 测试结果:浏览器显示404,服务器控制台打印出test方法执行了
第二种方式:
- JSP
<%@ page contentType="text/html; charset=UTF-8" language="java" %> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>cai jin hong</title> <style> </style></head> <body> <a href="userController.do?method=testOne">测试userController下的testOne方法</a><br> <a href="userController.do?method=testTwo">测试userController下的testTwo方法</a> </body> </html>
- java
@Controller @RequestMapping("userController.do") public class UserController { @RequestMapping(params = {"method=testOne"}) public void testOne(){ System.out.println("testOne方法执行了"); } @RequestMapping(params = {"method=testTwo"}) public void testTwo(){ System.out.println("testTwo方法执行了"); } }
- 测试结果:浏览器显示404,服务器控制台打印出输出的语句
第三种方式:
- JSP:
<%@ page contentType="text/html; charset=UTF-8" language="java" %> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>cai jin hong</title> <style> </style></head> <body> <a href="testOne.do">testOne方法</a><br> <a href="testTwo.do">testTwo方法</a> </body> </html>
- Java:
@Controller public class UserController { @RequestMapping("testOne.do") public void testOne(){ System.out.println("testOne方法执行了"); } @RequestMapping("testTwo.do") public void testTwo(){ System.out.println("testTwo方法执行了"); } }
- 测试结果:浏览器显示404,服务器端控制台正常打印
@RequestMapping的其他用法
- JSP:
<%@ page contentType="text/html; charset=UTF-8" language="java" %> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>cai jin hong</title> <style> </style></head> <body> <a href="testOne.do?user=123&password=123">测试参数携带的请求</a><br> <a href="testOne.do">测试不携带参数的请求</a> <%-- a标签的请求方式是GET请求--%> <a href="testGetMethod.do">测试服务器要求的请求方式</a> <a href="testPostMethod.do">测试服务器要求的请求方式</a> <a href="testHeader.do">测试服务器要求的携带的请求头</a> </body> </html>
- Java:
@Controller public class UserController { //请求必须携带user和password这两个参数key,而且必须有value值 @RequestMapping(value = "testOne.do", params = {"user=123", "password=123"}) public void testOne(){ System.out.println("这是测试是否携带参数的请求方法"); } @RequestMapping(value = "testGetMethod.do", method = {RequestMethod.GET}) public void testGetMethod(){ System.out.println("这是测试Get的请求方法"); } @RequestMapping(value = "testPostMethod.do", method = {RequestMethod.POST}) public void testPostMethod(){ System.out.println("这是测试Post的请求方法"); } @RequestMapping(value = "testHeader.do", headers = {"Accept-Language"}) public void testHeader(){ System.out.println("这是测试是否携带请求头"); } }
- 测试结果:
测试参数携带的请求
- 浏览器显示404,客户端正常打印
测试不携带参数的请求
- 浏览器显示400报错,不满足客户端要求的格式
测试服务器要求的GET请求方式
- 浏览器显示404,客户端正常打印
测试服务器要求的POST请求方式
- 浏览器显示405,请求的资源要求用Post方式,不支持GET请求
测试服务器要求携带的请求头
- 浏览器显示404,客户端正常打印
2. 请求参数的处理
1)方法中传入变量
- 要求传入的变量与浏览器发送请求传递的参数key一致,即便key只有一个
- 利用@RequestParam("key"),那么只需注解里面的key和浏览器传递的参数key一致即可
代码如下:
- JSP:
<%@ page contentType="text/html; charset=UTF-8" language="java" %> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>cai jin hong</title> <style> </style></head> <body> <form action="testOne.do" method="post"> <input type="text" name="account" value=""><br> <input type="text" name="password" value=""><br> <input type="text" name="balance" value=""><br> <input type="submit" value="submit"> </form> <form action="testTwo.do" method="post"> <input type="text" name="account" value=""><br> <input type="text" name="password" value=""><br> <input type="text" name="balance" value=""><br> <input type="submit" value="submit"> </form> </body> </html>
- Java:
@Controller public class UserController { //方法中传入变量:参数名与请求传递参数的名字一致 @RequestMapping("testOne.do") public void testOne(String account, String password, float balance){ System.out.println("testOne:"+ "account = " + account + " password = " + password + " balance" + balance); } //方法中传入变量:参数名与请求传递参数的名字不一致 @RequestMapping("testTwo.do") public void testTwo(@RequestParam("account") String xxx, @RequestParam("password") String yyy, @RequestParam("balance") float zzz){ System.out.println("testTwo:" + "account = " + xxx + " password = " + yyy + " balance" + zzz); } }
- input标签里的内如自己输入,点击submit,在后端的控制台上就能看到打印出来的结果
2)方法中传入实体对象
直接用对象接收,要求对象中的属性名与浏览器传递过来的参数key一致
- 代码如下:
- JSP:(代码太多了,这里只给出form表单)
<form action="testThree.do" method="post"> <input type="text" name="account" value=""><br> <input type="text" name="password" value=""><br> <input type="text" name="balance" value=""><br> <input type="submit" value="submit"> </form>
- Java:
public class User { private String account; private String password; private Float balance; public User(String account, String password, Float balance) { System.out.println("User:带参的构造方法"); this.account = account; this.password = password; this.balance = balance; } public User(){ System.out.println("User:无参的构造方法"); } public void init(){ System.out.println("初始化了"); } public void destroy(){ System.out.println("被销毁了"); } @Override public String toString() { return "User{" + "account='" + account + ''' + ", password='" + password + ''' + ", balance=" + balance + '}'; } public String getAccount() { return account; } public void setAccount(String account) { System.out.println("set方法调用了"); this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Float getBalance() { return balance; } public void setBalance(Float balance) { this.balance = balance; } }
@Controller public class UserController { @RequestMapping("testThree.do") //方法中传入实体对象:要求对象中的属性名与浏览器传递过来的参数key一致 public void testThree(User user){ System.out.println("testThree:"+ user); } }
对象套对象的情况:Spring会根据传递过来的参数,将对象里对象属性自动包装(如果参数名和属性名都对应的话)
- 代码如下:
- JSP:
<form action="testThree.do" method="post"> account:<input type="text" name="account" value=""><br> password:<input type="text" name="password" value=""><br> balance:<input type="text" name="balance" value=""><br> address:<input type="text" name="address" value=""><br> <input type="submit" value="submit"> </form>
- Java:(User里面套着Address)
public class Address { private String address; public Address(){} public Address(String address){ this.address = address; } @Override public String toString() { return "Address{" + "address='" + address + ''' + '}'; } public void setAddress(String address) { this.address = address; } public String getAddress() { return address; } } public class User { private String account; private String password; private Float balance; private Address address; public User(String account, String password, Float balance) { System.out.println("User:带参的构造方法"); this.account = account; this.password = password; this.balance = balance; } public User(){ System.out.println("User:无参的构造方法"); } public void init(){ System.out.println("初始化了"); } public void destroy(){ System.out.println("被销毁了"); } @Override public String toString() { return "User{" + "account='" + account + ''' + ", password='" + password + ''' + ", balance=" + balance + ", address=" + address + '}'; } public String getAccount() { return account; } public void setAccount(String account) { System.out.println("set方法调用了"); this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Float getBalance() { return balance; } public void setBalance(Float balance) { this.balance = balance; } public void setAddress(Address address) { this.address = address; } public Address getAddress() { return address; } }
@Controller public class UserController { //方法中传入实体对象:对象里面依赖了其他对象 @RequestMapping("testFour.do") public void testFour(User user){ System.out.println("testFour:" + user); } }
- 注意:输入中文会出现乱码的问题,因为这里没有处理中文字符集
对象套list集合<对象>:
- 代码如下:
- JSP:(addressList[i]代表addressList集合中的一个对象,通过.获取对象的属性,addressList是user对象中的属性名)
<form action="testFive.do" method="post"> account:<input type="text" name="account" value=""><br> password:<input type="text" name="password" value=""><br> balance:<input type="text" name="balance" value=""><br> address1:<input type="text" name="addressList[0].address" value=""><br> address2:<input type="text" name="addressList[1].address" value=""><br> <input type="submit" value="submit"> </form>
- Java:(Address类代码不变)
public class User { private String account; private String password; private Float balance; private List<Address> addressList; public User(String account, String password, Float balance) { System.out.println("User:带参的构造方法"); this.account = account; this.password = password; this.balance = balance; } public User(){ System.out.println("User:无参的构造方法"); } public void init(){ System.out.println("初始化了"); } public void destroy(){ System.out.println("被销毁了"); } @Override public String toString() { return "User{" + "account='" + account + ''' + ", password='" + password + ''' + ", balance=" + balance + ", addressList=" + addressList + '}'; } public String getAccount() { return account; } public void setAccount(String account) { System.out.println("set方法调用了"); this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Float getBalance() { return balance; } public void setBalance(Float balance) { this.balance = balance; } public void setAddressList(List<Address> addressList) { this.addressList = addressList; } public List<Address> getAddressList() { return addressList; } }
@Controller public class UserController { //方法中传入实体对象:对象里面有list集合 @RequestMapping("testFive.do") public void testFive(User user){ System.out.println("testFive:" + user); } }
3)方法中传入Map集合
- 直接在方法参数前加上@RequestParam注解(这里就不写了)
4)方法中传入Request、Response对象
- 代码如下:
@RequestMapping("testFive.do") public void testFive(HttpServletRequest request, HttpServletResponse response){ System.out.println(request.getParameter("account")); System.out.println(response); }
本篇文章到这就结束了,因为篇幅比较长,把响应的处理放在下一篇文章中!!!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。