Springmvc-crud-04错误(路径变量)
2024-09-30 09:31:35
错误:
原因:接收不到restful风格请求的参数(id值),需要添加路径变量注解
@RequestMapping(value="/book/{id}",method=RequestMethod.DELETE)
public String deleteBook(@PathVariable("id")int id) {
bookSerice.deleteBook(id);
return "redirect:/book";
}
关于路径变量的知识点:
第①种情况(单个路径参数):
@RequestMapping(value = "/book/{id}", method = RequestMethod.DELETE)
public String deleteBook(@PathVariable(value = "id") int id) {
System.out.println("删除图书 id ->" + id);
return "redirect:/index.jsp";
}
/book/{id} :{id}表示路径参数占位符
@PathVariable(value = "id") int id:表示把请求路径book/{id} id所表示的值。注入到方法参数的int id中。
当@PathVariable注解中没有标记占位符名称的时候,默认使用对应的参数名做为路径参数的名称取值。
请记住:@PathVariable标记的路径变量,不能为空,必须有值
第②种情况(多个路径参数):
@RequestMapping(value = "/book/{name}/{price}/", method = RequestMethod.POST)
public String addBook(@PathVariable("name") String name,
@PathVariable("price") BigDecimal price) {
System.out.println(name);
System.out.println(price);
System.out.println("添加图书");
return "redirect:/index.jsp";
}
在一次请求中,请求路径不是只能有一个路径参数。它可以有多个。
/book/{name}/{price}/:这里就表示这个book后面可以跟两个路径参数---{name}和{price}---这两个路径参数分别注入到请求方法的 String name,和Bigdecimal price两个参数中
@PathVariable("name") String name——表示把请求路径中的name路径参数注入到请求方法的name参数中。
@PathVariable("price") BigDecimal price——表示把请求路径中的price路径参数注入到请求方法的price参数中。
最新文章
- sz rz SecureCRT
- nodejs redis 发布订阅机制封装
- Android应用安全开发之浅谈网页打开APP
- std::vector<;bool>;中的坑
- android-8~23 View.java - dispatchTouchEvent源码
- Velocity语言的介绍
- BZOJ4152The Captain[DIjkstra]
- logstash 配置文件实例
- Apple Swift编程语言入门教程
- 如何在redhat下安装WineQQ
- Number
- mongod的主要参数解释
- Prime Land
- Toy Storage
- break point
- java虚拟机学习-慢慢琢磨JVM(2-1)ClassLoader的工作机制
- 内核空间内存申请函数kmalloc kzalloc vmalloc的区别
- [record]WebLogic域之创建-文本界面
- CAP理论与分布式事务解决方案
- Python WebSocket长连接心跳与短连接