spring mvc - Could not find @PathVariable in @RequestMapping -
i have problem when running project. page , controller configure right. when add @requestmapping(value = "/edit/{id}", method = requestmethod.get) throw exception.
nestedservletexception: request processing failed; nested exception org.springframework.web.bind.annotation.support.handlermethodinvocationexception: failed invoke handler method [public org.springframework.web.servlet.modelandview com.solution.controller.booklistcontroller.edit(java.lang.string,org.springframework.ui.model)]; nested exception java.lang.illegalstateexception: not find @pathvariable [id] in @requestmapping help me. why problem throwed? page work when comment "edit" , "delete" methods
my controller:
@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()); } } my page fragment:
<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>
it seems have forget declaration in method head, please take look:
@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); } ... }
Comments
Post a Comment