gpt4 book ai didi

java - 在 spring mvc 3 中使用路径变量

转载 作者:行者123 更新时间:2023-12-01 13:40:55 26 4
gpt4 key购买 nike

我正在从Spring in Action 3rd Edition学习Spring MVC并遇到了路径变量的使用。根据书中给出的示例,我不清楚它是如何工作的,请帮助我理解这里的概念:

@RequestMapping(method=RequestMethod.POST)
public String addSpitterFromForm(@Valid Spitter spitter, BindingResult bindingResult) {
if(bindingResult.hasErrors()){
return"spitters/edit";
}
spitterService.saveSpitter(spitter);
return "redirect:/spitters/" + spitter.getUsername();
}

As for the path that it’s redirecting to, it’ll take the form of /spitters/{username} where {username} represents the username of the Spitter that was just submitted. For example, if the user registered under the name habuma, then they’d be redirected to /spitters/habuma after the form submission.

在上面的语句中,它表示请求被重定向到 /spitters/habuma,其中 habuma 是用户名。

@RequestMapping(value="/{username}",method=RequestMethod.GET)
public String showSpitterProfile(@PathVariable String username, Model model){
model.addAttribute(spitterService.getSpitter(username));
return "spitters/view";
}

For example, if the request path is /username/habuma, then habuma will be passed in to showSpitterProfile() for the username.

这里说 showSpitterProfile() 方法处理 /username/habuma 的请求,这与前面提到的语句相矛盾。

看起来第一个语句本身是正确的,但请告诉我方法 showSpitterProfile 是否处理两个 URL,即 /splitters/habuma/username/habuma/spitters/用户名/habuma

最佳答案

如果类级别上的@RequestMapping(您的问题中未显示)仅是@RequestMapping("/spitter),则没有/username路径组件“)。书上可能有错别字。正确的句子是:

For example, if the request path is /spitter/habuma, then habuma will be passed in to showSpitterProfile() for the username.

关于java - 在 spring mvc 3 中使用路径变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20786394/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com