gpt4 book ai didi

java - Spring找不到HTTP请求的映射

转载 作者:行者123 更新时间:2023-12-02 07:55:58 25 4
gpt4 key购买 nike

我正在尝试创建一个简单的网络应用程序,它允许用户创建主题并对其进行评论。这个想法是,启动一个主题后,用户被重定向到该主题的页面。

@Controller
public class HomeController {

@RequestMapping(value = "/create", method = RequestMethod.GET)
public ModelAndView create(Locale locale, Model model)
{
Topic newTopic = new Topic();
logger.info("HomeControlller: Create");
List<Tag> tagList = newTopic.getTagLict();
Hashtable modelData = new Hashtable();
modelData.put("newTopic", newTopic);
modelData.put("tagList", tagList);

return new ModelAndView("create", modelData);

}

@RequestMapping(value = "/create", method = RequestMethod.POST)
public String saveNewTopic(@ModelAttribute("newTopic")Topic topic, BindingResult result, Model model)
{
validate(topic, result);
// Go to the "Show topic@ page
return "redirect:details/"+service.saveTopic(topic);
}

@RequestMapping(value = "/details/(topicId)", method = RequestMethod.GET)
public ModelAndView details(@PathVariable(value="topicId") int id)
{
logger.info("HomeControlller: Details: Found a method");
Topic topicById = service.findTopicByID((long) id);
logger.info("HomeControlller: Details: Performing redirect");
return new ModelAndView("/topic/", "model", topicById);
}


}

但是在创建主题后,我收到错误在名称为“appServlet”的 DispatcherServlet 中未找到带有 URI [/simpleblog/details/9] 的 HTTP 请求的映射。我不明白出了什么问题,因为 HTTP 请求是用注释映射的。它适用于 create()saveNewTopic() 函数,但不适用于 details() 函数。

最佳答案

路径变量的语法是{foo},而不是(foo):

@RequestMapping(value = "/details/{topicId}", method = RequestMethod.GET)
public ModelAndView details(@PathVariable(value="topicId") int id)

关于java - Spring找不到HTTP请求的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9645215/

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