简体   繁体   中英

unable to link resource with controller class by using PathVariable technique in spring mvc

this is my index page:

 <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <h1>Employee List</h1> <table border="2" width="70%" cellpadding="2"> <tr><th>Name</th><th>Address</th> <th>City</th><th>Cars</th></tr> <c:forEach var="emp" items="${list}"> <tr> <td>${emp.name }</td> <td>${emp.address }</td> <td>${emp.city }</td> <td>${emp.cars}</td> <td><a href="editemp/${emp.name }">Edit</a> <td><a href="deleteemp/${emp.name }">Delete</a> </tr></c:forEach> </table> <a href="empform">Add New Employee</a>" 

problem is when i click on other link than it works fine but when i click on editemp or deleteemp link,than it shows me error:The requested resource is not available

This is my controller class:

 package first; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller public class EmpController { @Autowired EmployeeDao dao; @RequestMapping("/empform") public ModelAndView showForm() { return new ModelAndView("empform","command",new Employee()); } @RequestMapping(value="/save",method=RequestMethod.POST) public ModelAndView save(@ModelAttribute("emp") Employee employee) { dao.saveEmployee(employee); return new ModelAndView("redirect:/viewemp"); } @RequestMapping("/viewemp") public ModelAndView viewemp() { List<Employee> list=dao.getAllEmployee(); return new ModelAndView("viewemp","list",list); } @RequestMapping(value="/editemp/{name}",method=RequestMethod.GET) public ModelAndView edit(@PathVariable String name) { Employee e=dao.getbyName(name); return new ModelAndView("empeditform","command",e); } @RequestMapping(value="/saveedit",method=RequestMethod.POST) public ModelAndView saveedit(@ModelAttribute("emp")Employee employee) { dao.updateEmployee(employee); return new ModelAndView("redirect:/viewemp"); } @RequestMapping(value="/deleteemp/{name}",method=RequestMethod.GET) public ModelAndView delete(@PathVariable String name) { dao.delete(name); return new ModelAndView("redirect:/viewemp"); } } 

Please tell me,why it is unable to map with controller method specified in controller class.....thanks in advance

One thing to try for your links to to update them to use the context path:

<td><a href="${pageContext.request.contextPath}/editemp/${emp.name}">Edit</a>
<td><a href="${pageContext.request.contextPath}/deleteemp/${emp.name}">Delete</a>

This post has a few other ways that you can specify your routes in a jsp page: [ How to use relative paths without including the context root name?

Also, for the controller routes to be registered, you will need to set component scan to look for any annotations:

<context:component-scan base-package="namespace to controller class" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM