2023-01-19
一、@RequestMapping注解位置
1、書寫在類上面
(1)作用:為當前類設定映射URL
(2)注意:不能單獨使用,需要與方法上的@RequestMapping配合使用
2、書寫在方法上面
(1)作用:為當前方法設定映射URL
(2)注意:可以單獨使用
3、示例代碼
(1)day09_springMVC/src/main/java/“com.hh.controller.EmployeeController”
@Controller @RequestMapping("/EmpController") public class EmployeeController { @RequestMapping("/saveEmp") public String saveEmp(){ System.out.println("添加員工資訊"); return "success"; } }
(2)day09_springMVC/src/main/webapp/WEB-INF/pages/empList.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>員工串列頁面</title> </head> <body> <h2>員工串列頁面</h2> <a th:href="@{/EmpController/saveEmp}">添加員工資訊</a> </body> </html>
二、@RequestMapping注解屬性
1、value屬性
(1)型別:String[]
(2)作用:設定URL資訊
2、path屬性
(1)型別:String[]
(2)作用:與value屬性作用一致
3、method屬性
(1)型別:RequestMethod[]
RequestMethod[]:是一個列舉型別
(2)作用:為當前URL(類或方法)設定請求方式
(3)注意:
①默認情況:所有請求方式均支持
②如請求方式不支持,會如下報錯
405【Request method 'GET' not supported】
4、params
(1)型別:String[]
(2)作用:為當前URL設定請求引數
(3)注意:如設定指定請求引數,但URL中未攜帶指定引數,會報如下錯誤
400【Parameter conditions "lastName=lisi" not met for actual request parameters】
5、headers
(1)型別:String[]
(2)作用:為當前URL設定請求頭資訊
(3)注意:如設定指定請求頭,但URL中未攜帶請求頭,會報如下錯誤
404:請求資源未找到
三、@RequestMapping支持Ant風格的路徑
1、常用通配符
(1)?:匹配一個字符
(2)*:匹配任意字符
(3)**:匹配多層路徑
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/542255.html
標籤:Java
