我正在尝试创建一个简单的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);
}
}
但在创建主题后,我收到错误在DispatcherServlet中找到名为'appServlet'的URI [/ simpleblog / details / 9]的HTTP请求没有映射。我无法理解什么是错误的,因为HTTP请求是用注释映射的。它适用于 create()和 saveNewTopic()函数,但不适用于 details()函数。
答案 0 :(得分:2)
路径变量的语法是{foo}
,而不是(foo)
:
@RequestMapping(value = "/details/{topicId}", method = RequestMethod.GET)
public ModelAndView details(@PathVariable(value="topicId") int id)