当前位置:网站首页>URL address mapping configuration
URL address mapping configuration
2022-07-26 00:05:00 【Li Linnan】
URL Address mapping configuration
@RequestMapping
Through annotation @RequestMapping Bind the request address to the method , You can declare... At the class level and method level . Class level annotations are responsible for mapping a specific request path to a controller , take url Bind to class ; Mapping can be refined through method level annotations , Be able to map a specific request path to a specific method , take url Bind to the method of the class .
Map single URL
@RequestMapping("") or @RequestMapping(value="")
package com.xxxx.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/** * URL Address mapping configuration * @RequestMapping(" Mapping address ") * Declaration on method , Indicates that the request path is bound to the method in the class * Declare at the class level , Represents the path and class binding of the request * * Common forms * Method path / Method path * Class path + Method path / Class path / Method path */
@Controller
public class UrlController {
/** * @RequestMapping Declare on the method , Map single URL * Access address :( If there is a class path that needs to be written in front of the method path ) * http://ip:port/springmvc01/url01 * @return */
@RequestMapping("url01")
public ModelAndView url01(){
ModelAndView mv = new ModelAndView();
// Set up model data
mv.addObject("hello","url01");
mv.setViewName("hello");
return mv;
}
/** * Whether to add... At the beginning of the path Slash "/" All possible * @RequestMapping("/ Request path ") And @RequestMapping(" Request path ") All possible * Advice and , Such as :@RequestMapping("/url02") * Access address :( If there is a class path that needs to be written in front of the method path ) * http://ip:port/springmvc01/url02 * @return */
@RequestMapping("/url02")
public ModelAndView url02(){
ModelAndView mv = new ModelAndView();
// Set up model data
mv.addObject("hello","url02");
mv.setViewName("hello");
return mv;
}
}
Map multiple URL
@RequestMapping({"",""}) or @RequestMapping(value={"",""})
package com.xxxx.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UrlController {
/** * @RequestMapping Declare on the method , Map multiple URL * Support one method to bind multiple url The operation of * Access address :( If there is a class path that needs to be written in front of the method path ) * http://ip:port/springmvc01/url03_01 * http://ip:port/springmvc01/url03_02 * @return */
@RequestMapping({
"/url03_01","/url03_02"})
// @RequestMapping(value = {"/url03_01","/url03_02"})
public ModelAndView test03(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("hello","url03");
modelAndView.setViewName("hello");
return modelAndView;
}
}
mapping URL On the controller
For classes , This address is the parent path of all methods in the class that respond to requests .
package com.xxxx.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("url")
public class UrlController {
/** * @RequestMapping Declare on the class , All methods in the class take this address as the parent path * Declaration level : * Class level + The method level (/ Class path / Method path ) * Access address : http://ip:port/springmvc/url/url04 * @return */
@RequestMapping("/url04")
public ModelAndView url04(){
ModelAndView mv = new ModelAndView();
// Set up model data
mv.addObject("hello","url04");
mv.setViewName("hello");
return mv;
}
}

Set up URL Request mode of mapping
The request method is not set by default , stay HTTP The most common request method in the request is GET、POST, There are other ways , Such as :DELETE、PUT、HEAD etc. .
Can pass method Property to set the supported request method , Such as method=RequestMethod.POST; Such as setting multiple request modes , Enclose with curly braces , Separated by commas .
package com.xxxx.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("url")
public class UrlController {
/** * Set request method * adopt method The request method supported by the property setting method , Default GET Request and POST All requests are supported . * Set the request mode , You can only request . * Access address :( Only use POST Request access ) * http://ip:port/springmvc01/url/url05 * @return */
@RequestMapping(value = "url05",method = RequestMethod.POST)
public ModelAndView url05(){
ModelAndView mv = new ModelAndView();
// Set up model data
mv.addObject("hello","url05");
mv.setViewName("hello");
return mv;
}
}
Browser defaults to Get Request mode .

PostMan test Get request

PostMan test Post request 
Map by parameter name URL
package com.xxxx.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("url")
public class UrlController {
/** * Access by parameter name * Access through parameters * Access address :http://ip:port/springmvc01/url?url06 * @return */
@RequestMapping(params = "url06")
public ModelAndView url06(){
ModelAndView mv = new ModelAndView();
// Set up model data
mv.addObject("hello","url06");
mv.setViewName("hello");
return mv;
}
}

边栏推荐
- Pyqt5 rapid development and actual combat.pdf sharing
- Binary tree -- 222. Number of nodes of a complete binary tree
- Piziheng embedded: the method of making source code into lib Library under MCU Xpress IDE and its difference with IAR and MDK
- Dead letter queue and message TTL expiration code
- Storage of data in memory
- ShardingSphere数据分片
- Quick sorting of top ten sorting
- Binary tree - 112. Path sum
- STM32 timer
- 二叉树——101. 对称二叉树
猜你喜欢

Leetcode200-查找岛屿数量详解

C语言实战之猜拳游戏

BOM browser object model

“动物币”凶猛,陷阱还是机遇?2021-05-12
![牛客/洛谷——[NOIP2003 普及组]栈](/img/95/871b1c6f492b467bffd25912304b44.gif)
牛客/洛谷——[NOIP2003 普及组]栈

Kubernetes网络插件详解 - Calico篇 - 概述

Binary tree - 530. Minimum absolute difference of binary search tree

Sequence traversal II of leetcode107 binary tree

Call nailing API and report an error: the signature sent by the robot is expired; Solution: please keep the signature generation time and sending time within timestampms

Yolov3 trains its own data set
随机推荐
二叉树——530.二叉搜索树的最小绝对差
@The underlying principle of Autowired annotation
[one library] mapbox GL! A map engine out of the box
STM32 pit encountered when using timer to do delay function
Song of statistics lyrics
BOM 浏览器对象模型
LeetCode_55_跳跃游戏
Lua脚本编写Wireshark插件解析第三方私有协议
LeetCode 沙胡同系列 -- 63. 不同路径 II
STM32 timer
Vscode format JSON file
1223. Dice simulation range DP
Binary tree -- 111. Minimum depth of binary tree
Getaverse,走向Web3的远方桥梁
Stm32 systeminit trap during simulation debugging
复盘:推荐系统—— 负采样策略
老旧笔记本电脑变服务器(笔记本电脑+内网穿透)
二叉树——257. 二叉树的所有路径
Responsibility chain model of behavioral model
Android solves the risk of database injection vulnerability