有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring找不到HTTPrequest的映射

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

@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()saventopic()函数,但不能使用details()函数


共 (1) 个答案

  1. # 1 楼答案

    path变量的语法是{foo},而不是(foo)

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