Spring mvc @PathVariable

问题描述 投票:106回答:8

你能给我一个简短的解释和一个在春天mvc使用@PathVariable的样本吗?请说明您如何输入网址? 我正在努力获得正确的网址来显示jsp页面。谢谢。

spring-mvc
8个回答
202
投票

你可以说,假设你想写一个网址来获取一些订单

www.mydomain.com/order/123

其中123是orderId。

所以现在你将在spring mvc控制器中使用的url看起来像

/order/{orderId}

现在可以将订单ID声明为路径变量

@RequestMapping(value = " /order/{orderId}", method=RequestMethod.GET)
public String getOrder(@PathVariable String orderId){
//fetch order
}

如果您使用网址www.mydomain.com/order/123,则orderId变量将由弹簧值123填充

另请注意,PathVariable与requestParam不同,因为pathVariable是URL的一部分。使用请求参数的相同网址看起来像www.mydomain.com/order?orderId=123

API DOC Spring Official Reference


10
投票

看看下面的代码片段。

@RequestMapping(value="/Add/{type}")
public ModelAndView addForm(@PathVariable String type ){
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("addContent");
    modelAndView.addObject("typelist",contentPropertyDAO.getType() );
    modelAndView.addObject("property",contentPropertyDAO.get(type,0) );
    return modelAndView;
}

希望它有助于构建您的代码。


5
投票

如果您有包含路径变量的url,例如www.myexampl.com/item/12/update,其中12是id,create是您要用于指定执行intance的变量,使用单个表单进行更新和创建,你在控制器中执行此操作。

   @RequestMapping(value = "/item/{id}/{method}" , RequestMethod.GET)
    public String getForm(@PathVariable("id") String itemId ,  @PathVariable("method") String methodCall , Model model){
  if(methodCall.equals("create")){
    //logic
}
 if(methodCall.equals("update")){
//logic
}

return "path to your form";
}

2
投票

@PathVariable用于从URL获取值

例如:提出一些问题

www.stackoverflow.com/questions/19803731

这里有一些问题id作为URL中的参数传递

现在要在controller中获取此值,您只需要在方法参数中传递@PathVariable即可

@RequestMapping(value = " /questions/{questionId}", method=RequestMethod.GET)
    public String getQuestion(@PathVariable String questionId){
    //return question details
}

1
投票

注释,指示方法参数应绑定到URI模板变量。支持RequestMapping带注释的处理程序方法。

@RequestMapping(value = "/download/{documentId}", method = RequestMethod.GET)
public ModelAndView download(@PathVariable int documentId) {
    ModelAndView mav = new ModelAndView();
    Document document =  documentService.fileDownload(documentId);

    mav.addObject("downloadDocument", document);
    mav.setViewName("download");

    return mav;
}

0
投票

我们假设您点击了一个网址www.example.com/test/111。现在你必须为你的控制器方法检索值111(这是动态的)。届时你将使用@PathVariable,如下所示:

@RequestMapping(value = " /test/{testvalue}", method=RequestMethod.GET)
public void test(@PathVariable String testvalue){
//you can use test value here
}

因此,从URL中检索变量值


0
投票

它是用于映射/处理动态URI的注释之一。您甚至可以为URI动态参数指定正则表达式,以仅接受特定类型的输入。

例如,如果使用唯一编号检索图书的URL将是:

URL:http://localhost:8080/book/9783827319333

可以使用@PathVariable获取URL最后一个表示的数字,如下所示:

@RequestMapping(value="/book/{ISBN}", method= RequestMethod.GET)

public String showBookDetails(@PathVariable("ISBN") String id,

Model model){

model.addAttribute("ISBN", id);

return "bookDetails";

}

简而言之,另一个是从Spring中的HTTP请求中提取数据。


-1
投票

看看下面的代码片段。

@RequestMapping(value = "edit.htm", method = RequestMethod.GET) 
    public ModelAndView edit(@RequestParam("id") String id) throws Exception {
        ModelMap modelMap = new ModelMap();
        modelMap.addAttribute("user", userinfoDao.findById(id));
        return new ModelAndView("edit", modelMap);      
    }

如果您希望整个项目看看它是如何工作的,那么从下面的链接下载它: -

UserInfo Project on GitLab

© www.soinside.com 2019 - 2024. All rights reserved.