我正在 Spring 测试一些功能。我的目标是编辑表中的记录。下面是显示现有记录的jsp代码。
<table border="1">
<tr>
<td>ID</td>
<td>First Name</td>
<td>Last Name</td>
<td>Telephone</td>
<td>Email</td>
<td></td>
<td></td>
</tr>
<c:forEach var="emp" items="${employees}">
<tr>
<td>${emp.id}</td>
<td>${emp.firstName}</td>
<td>${emp.lastName}</td>
<td>${emp.telephone}</td>
<td>${emp.email}</td>
<td><a href="<c:url value="getEmpForUpdate/${emp.id}" />">Update</a></td>
<td><a>Delete</a></td>
</tr>
</c:forEach>
</table>
我猜我在上面的代码中使用的 hiper link 有问题。
<a href="<c:url value="getEmpForUpdate/${emp.id}" />">Update</a>
当我点击记录时,URL 将如下所示。
http://localhost:9876/SpringAnnotationDemo_Tomcat/getEmpForUpdate/4
编辑记录的显示形式。下面是我用来编辑记录的代码。
<form:form method="POST" action="/updateEmployee">
<table>
<tr>
<td><form:label path="firstName">F Name</form:label></td>
<td><form:input path="firstName" /></td>
</tr>
<tr>
<td><form:label path="lastName">L Name</form:label></td>
<td><form:input path="lastName" /></td>
</tr>
<tr>
<td><form:label path="telephone">Phone</form:label></td>
<td><form:input path="telephone" /></td>
</tr>
<tr>
<td><form:label path="email">E-Mail</form:label></td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
编辑上面的详细信息后,当我点击输入时,URL 应该如下所示 http://localhost:9876/SpringAnnotationDemo_Tomcat/updateEmployee
但是像下面这样 http://localhost:9876/SpringAnnotationDemo_Tomcat/getEmpForUpdate/updateEmployee
My controller method is like below
@RequestMapping(value="/updateEmployee",method = RequestMethod.POST)
public ModelAndView updateEmployeeUsingObject(@ModelAttribute Employee employee){
ModelAndView modelAndView = new ModelAndView();
System.out.println(employee.getId());
return modelAndView;
}
你能告诉我哪里错了吗?为什么 URL 不是预期的。
最佳答案
一旦你导航编辑你的页面是在 url http://localhost:9876/SpringAnnotationDemo_Tomcat/getEmpForUpdate所以你需要将你的操作更新/更改为 "../updateEmployee"
因为它是相对 url
或者更好的是你给 url 类似的东西
<form:form action="${pageContext.request.contextPath}/updateEmployee" method="post">
关于java - 请求将在 spring : 中转到错误的处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39869856/