繁体   English   中英

无法在Spring MVC中使用PathVariable技术将资源与控制器类链接

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

这是我的索引页:

 <%@ 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>" 

问题是当我单击其他链接而不是它可以正常工作时,但是当我单击editemp或deleteemp链接时,它向我显示错误:请求的资源不可用

这是我的控制器类:

 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"); } } 

请告诉我,为什么无法使用控制器类中指定的控制器方法进行映射。

尝试使用链接更新链接以使用上下文路径的一件事:

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

这篇文章还有其他几种方法可以在jsp页面中指定路由:[ 如何在不包括上下文根名称的情况下使用相对路径?

另外,对于要注册的控制器路由,您将需要设置组件扫描以查找任何注释:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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