Spring mvc @PathVariable [英] Spring mvc @PathVariable
问题描述
你能给我一个在 spring mvc 中使用 @PathVariable
的简要说明和示例吗?请包括您输入网址的方式?
我正在努力获取正确的 url 来显示 jsp 页面.谢谢.
Can you give me a brief explanation and a sample in using @PathVariable
in spring mvc? Please include on how you type the url?
I'm struggling in getting the right url to show the jsp page. Thanks.
推荐答案
假设你想写一个url来获取某个订单,你可以说
suppose you want to write a url to fetch some order, you can say
www.mydomain.com/order/123
其中 123 是 orderId.
where 123 is orderId.
所以现在你将在 spring mvc 控制器中使用的 url 看起来像
So now the url you will use in spring mvc controller would look like
/order/{orderId}
现在可以将订单 id 声明为路径变量
Now order id can be declared a path variable
@RequestMapping(value = " /order/{orderId}", method=RequestMethod.GET)
public String getOrder(@PathVariable String orderId){
//fetch order
}
如果您使用 url www.mydomain.com/order/123,那么 orderId 变量将在 spring 时由值 123 填充
if you use url www.mydomain.com/order/123, then orderId variable will be populated by value 123 by spring
另请注意,PathVariable 与 requestParam 不同,因为 pathVariable 是 URL 的一部分.使用请求参数的相同 url 看起来像 www.mydomain.com/order?orderId=123
Also note that PathVariable differs from requestParam as pathVariable is part of URL.
The same url using request param would look like www.mydomain.com/order?orderId=123
这篇关于Spring mvc @PathVariable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!