【问题标题】:unable to link resource with controller class by using PathVariable technique in spring mvc无法通过在 spring mvc 中使用 PathVariable 技术将资源与控制器类链接
【发布时间】:2017-05-18 20:11:21
【问题描述】:

这是我的索引页:

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

请告诉我,为什么它无法与控制器类中指定的控制器方法进行映射.....提前谢谢

【问题讨论】:

    标签: java html spring hibernate spring-mvc


    【解决方案1】:

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

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

    这篇文章有一些其他的方法可以让你在 jsp 页面中指定你的路由: [How to use relative paths without including the context root name?

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 2017-11-16
      • 2018-04-26
      • 2017-03-07
      • 1970-01-01
      • 2016-04-19
      • 2011-12-21
      相关资源
      最近更新 更多