@RequestMapping无法捕获网址

时间:2014-03-12 15:08:39

标签: forms spring-mvc servlets controller

我用表格声明控制器。但是当我使用Delete url时,请捕获404错误

我有一个代码表:

<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/${book.id}">Delete</a>
                    <a href="${pageContext.request.contextPath}/EditBook.vw/${book.id}">Edit</a>
                </td>
            </sec:authorize>
        </tr>
    </c:forEach>

我的控制员:

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

    @Autowired
    private IBookService bookService;

    public String getModelName() {
        return "BookList";
    }

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

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

    @RequestMapping(value = {"/delete"}, method = RequestMethod.POST)
    public ModelAndView delete(@RequestParam("id") Integer id) throws Exception {
        bookService.removeBook(id);
        return new ModelAndView(getModelName());
    }
}

通过第二个URL我也抓到404错误。我不明白为什么。 EditBook控制器:

@Controller
@RequestMapping("/EditBook.vw")
public class EditBookController{

    @Autowired
    private IBookService bookService;

    @Autowired
    private IAuthorService authorService;


    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    protected ModelAndView openMain(@PathVariable Integer id) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();
        List<Book> books = bookService.listBooks();
        Book book = new Book();
        for (Book book1 : books) {
            if(book1.getId() == id)
                book = book1;
        }

        model.put("book", book);

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

我尝试在POST,Get和其他之间更改它..帮助我?请

2 个答案:

答案 0 :(得分:1)

   @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable Integer id) throws Exception {
        bookService.removeBook(id);
        return new ModelAndView(getModelName());
    }

你能试试吗?

答案 1 :(得分:0)

您的控制器声明只有post请求才会执行delete()方法。你应该使用

<form method="post" action="the_delete_URL">
而不是       因为href不是帖子。