0

I have a problem when running project. page and controller configure right. But when I add @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET) his throw exception.

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

Help me. Why this problem is throwed? Page work when I comment "edit" and "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>

2 Answers 2

2

it seems like you have forget the declaration in your method head, please take a 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);
    }
...
}
Sign up to request clarification or add additional context in comments.

1 Comment

in your edit Method you use the Pathvariable annotation but you missing in this annotation the name of the variable, please compare my with yours. thanks
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) for example instead of call ..../edit/005... you are trying .../edit.. you missing your id that's why you getting could not find @PathVariable

2)else use Optional in @PathVarible ... (eg.@PathVarible Optional id)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.