java无法在spring mvc中使用get请求调用控制器方法
实际上,我是JavaSpringMVC新手,目前遇到了一个非常简单的问题。基本上,我正在删除表的特定行,但无法调用控制器方法Remove
表的Mycheck.jsp
代码
<table id="customers">
<tr>
<th>ID</th>
<th>Name</th>
<th>Contact</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<c:forEach items="${persons}" var="p">
<tr>
<td>${p.id}</td>
<td>${p.name}</td>
<td>${p.contact}</td>
<td ><a href="#">edit</a></td>
<td><a href="<c:url value='/remove/${p.id}' />" >delete</a></td>
</tr>
</c:forEach>
</table>
我的testController.java
代码
@Controller
public class testController {
@RequestMapping("/check")
public ModelAndView getusers()
{
ModelAndView mv=new ModelAndView("check");
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List list=session.createQuery("from Person").list();
mv.addObject("persons",list);
session.getTransaction().commit();
mv.addObject("message","Welcome");
return mv;
}
@RequestMapping(value = "/remove/{id}", method = RequestMethod.GET)
public ModelAndView removePerson(@PathVariable("id") int id)
{
//System.out.println("It IS RUNNING");
ModelAndView mv=new ModelAndView("check");
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Person P;
P = (Person)session.load(Person.class,id);
session.delete(P);
session.flush();
session.getTransaction().commit();
return mv;
}
}
这是testController
的整个类代码,其中getusers()
方法工作得非常好,而removePerson()
不工作,因此给出了HTTP Status 404 - Not Found
错误
# 1 楼答案
在浪费了一天时间之后,我终于发现我的控制器类没有问题,主要问题在于我的
dispatcher-servlet.xml
映射,我将<prop key="check.htm">test</prop>
更改为<prop key="/check/**">test</prop>
,然后removePerson()
方法工作得非常好