在@RequestMapping中找不到@PathVariable

时间:2014-03-10 21:36:12

标签: spring-mvc controller request

运行项目时遇到问题。页面和控制器配置正确。但是当我添加@RequestMapping(value =“/ edit / {id}”,method = RequestMethod.GET)时,他的抛出异常。

NestedServletException: Request processing failed; nested exception is org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public org.springframework.web.servlet.ModelAndView com.solution.controller.BookListController.edit(java.lang.String,org.springframework.ui.Model)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [id] in @RequestMapping

帮帮我。为什么会抛出这个问题? 我评论“编辑”和“删除”方法时页面工作

我的控制器:

@Controller
@RequestMapping("/BookList.vw")
public class BookListController {

    @Autowired
    private IBookService bookService;


    @RequestMapping(method = RequestMethod.GET)
    protected ModelAndView openMain(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();
        List<Book> books = bookService.listBooks();
        model.put("books", books);

        return new ModelAndView("BookList", "model", model);
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable String id, Model model) {
        Map<String, Object> models = new HashMap<String, Object>();
        models.put("id", id);
        return new ModelAndView("EditBook", "model", models);
    }

    @RequestMapping(value = {"/delete/{id}"})
    public ModelAndView delete(@PathVariable("id") Integer id) {
        bookService.removeBook(id);
        return new ModelAndView(getModelName());
    }
}

我的网页片段:

 <c:forEach items="${model.books}" var="book">
        <tr align="left" height="100%">
            <td>${book.name}</td>
            <td>${book.description}</td>
            <td>${book.year}</td>
            <td></td>
                <%--<td>${book.authorNames}</td>--%>
            <sec:authorize access="hasRole('ROLE_ADMIN')">
                <td>
                    <a href="${pageContext.request.contextPath}/BookList.vw/edit/${book.id}">Edit</a>
                    <a href="${pageContext.request.contextPath}/BookList.vw/delete/${book.id}">Delete</a>
                </td>
            </sec:authorize>
        </tr>
    </c:forEach>

2 个答案:

答案 0 :(得分:2)

好像你忘记了方法头中的声明,请看一下:

@Controller
@RequestMapping("/BookList.vw")
public class BookListController {
    ...

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") String id, Model model) {
        Map<String, Object> models = new HashMap<String, Object>();
        models.put("id", id);
        return new ModelAndView("EditBook", "model", models);
    }
...
}

答案 1 :(得分:0)

@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
public ModelAndView edit(@PathVariable String id, Model model) {
    Map<String, Object> models = new HashMap<String, Object>();
    models.put("id", id);
    return new ModelAndView("EditBook", "model", models);
}

1)例如,而不是调用.... / edit / 005 ...,您正在尝试... / edit ..您丢失了ID,这就是为什么您找不到@PathVariable

2)其他在@PathVarible ...中使用Optional(例如@PathVarible可选ID)