Spring Mvc中传递参数方法之url/requestMapping详解
作者:果感 时间:2021-11-03 05:48:46
前言
相信大家在使用spring的项目中,前台传递参数到后台是经常遇到的事, 我们必须熟练掌握一些常用的参数传递方式和注解的使用,本文将给大家介绍关于Spring Mvc中传递参数方法之url/requestMapping的相关内容,分享出来供大家参考学习,话不多说,直接上正文。
方法如下
1. @requestMapping: 类级别和方法级别的注解, 指明前后台解析的路径。 有value属性(一个参数时默认)指定url路径解析,method属性指定提交方式(默认为get提交)
@RequestMapping(value = "/testing")
public class QuestionSetDisplayController extends BaseController {}
@RequestMapping(value = "/applicant/recover")
public BaseModel recover(String cellphone) throws OTPException {
return userService.recover(cellphone);
}
2. @RequestParam: 请求参数规则注解。 value属性匹配前台传递的参数(一个参数时默认),required属性此字段是否必须传值(boolean,默认为true),defaultValue此参数的默认值(存在此参数时,说明前台不必需传递参数,required为false)
@RequestMapping("/login") //url: /login?name=tom
public String login(@RequestParam(value="age",required=false,defaultValue="24") String agenum,@RequestParam("name") String name){
return "hello";
}
3. @PathVariable: url参数注解, 一般用于从url中获取参数
@RequestMapping(value = "/system/getAllCodeTableData/{category}", method = RequestMethod.GET) //前台url: '/system/getAllCodeTableData/APPLICANT_ENGLISH'
public List<CodeTableModel> getCodeTableModelByCategory(@PathVariable String category) throws OTPException {<br> return codeTableService.getCodeTableModelByCategory(category); <br>}
4. 特殊的 属性编辑器 在前台到后台data日期类型等的转化会出错,此时我们需要属性编辑器进行属性的转化 //日期传递参数会产生异常,因此在传递时间参数时,需要进行类型转换,在初始化时进行数据的绑定与转化
@RequestMapping(value="/todate/{data}",method=RequestMethod.GET)
public String todate(@PathVariable("data") Date date){
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
return "start";
}
@InitBinder //初始化参数绑定, 日期类型的转化,
public void initBinder(ServletRequestDataBinder binder){
binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
来源:http://www.cnblogs.com/nelson-hu/p/6595522.html
标签:springmvc,参数传递,url
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
macOS下Spring Boot开发环境搭建教程
2023-11-05 15:50:59
![](https://img.aspxhome.com/file/2023/1/85541_0s.jpg)
SpringBoot使用ApplicationEvent&Listener完成业务解耦
2021-10-19 06:36:41
解决SpringMVC、tomcat、Intellij idea、ajax中文乱码问题
2023-10-20 10:16:23
![](https://img.aspxhome.com/file/2023/5/72625_0s.jpg)
android 获取视频,图片缩略图的具体实现
2023-07-28 00:20:39
Java实战之在线寄查快递系统的实现
2023-08-10 21:52:23
![](https://img.aspxhome.com/file/2023/2/58182_0s.png)
Android 自定义密码输入框实现代码
2023-02-11 09:18:40
![](https://img.aspxhome.com/file/2023/2/88712_0s.png)
盘点几种常见的java排序算法
2023-09-17 10:13:10
![](https://img.aspxhome.com/file/2023/0/78260_0s.png)
基于Android平台实现拼图小游戏
2021-07-27 16:48:12
![](https://img.aspxhome.com/file/2023/5/95965_0s.jpg)
Java Maven高级之插件开发详解
2023-05-11 19:10:02
![](https://img.aspxhome.com/file/2023/6/79306_0s.png)
java中Executor,ExecutorService,ThreadPoolExecutor详解
2023-10-31 10:50:56
详解java WebSocket的实现以及Spring WebSocket
2023-11-24 13:16:25
![](https://img.aspxhome.com/file/2023/5/59885_0s.png)
java接口私有方法实现过程解析
2021-10-28 01:51:23
Java的字符串中对子字符串的查找方法总结
2022-12-16 17:16:31
Java线程池的几种实现方法和区别介绍
2023-12-12 12:46:04
一篇文章带你复习java知识点
2023-01-15 09:39:38
![](https://img.aspxhome.com/file/2023/0/78230_0s.png)
java中怎样表示圆周率
2022-11-30 11:20:00
![](https://img.aspxhome.com/file/2023/8/79848_0s.png)
C#面向对象的23种设计模式介绍
2023-01-23 18:02:50
![](https://img.aspxhome.com/file/2023/5/83225_0s.jpg)
C# 爬虫简单教程
2022-04-24 14:10:53
![](https://img.aspxhome.com/file/2023/0/72470_0s.png)
Java打印斐波那契前N项的实现示例
2022-12-03 21:18:10
Spring Boot缓存实战 EhCache示例
2023-08-30 12:23:35
![](https://img.aspxhome.com/file/2023/3/58213_0s.png)