java - 尝试打开编辑表单以在 Spring 中编辑对象但返回 404
问题描述
我正在尝试创建一个编辑表单并使用所选对象值预填充表单,但它返回的 HTTP 状态为 404,并且我收到的错误消息是
org.springframework.web.servlet.DispatcherServlet.noHandlerFound 没有映射 GET /AgentsCRUD/agent/edit/1;jsessionid=07E2EC08848D0C9C9346DC67563DDF1F
我认为我所拥有的非常合乎逻辑,但我一定搞砸了映射。
获取 AllAgent.jsp 中的代码
<spring:message code="welcome.message" />
<body>
<table style="width:100%">
<tr>
<th align="left"><spring:message code="label.agentId" /></th>
<th align="left"><spring:message code="label.name" /></th>
<th align="left"><spring:message code="label.fax" /></th>
<th align="left"><spring:message code="label.phone" /></th>
<th align="left"><spring:message code="label.email" /></th>
<th align="left"><spring:message code="label.datejoined" /></th>
<th align="left"><spring:message code="label.sales" /></th>
<th align="left"><spring:message code="label.actions" /></th>
</tr>
<c:forEach items="${agentList}" var="agent">
<tr>
<td>${agent.agentId}</td>
<td>${agent.name}</td>
<td>${agent.fax}</td>
<td>${agent.phone}</td>
<td>${agent.email}</td>
<td>
<a href="\AgentsCRUD\agent\delete?agentId=${agent.agentId}"><spring:message code="label.delete" /></a>
<spring:url value="/agent/edit/${agent.agentId}" var="editURL"/>
<a href="${editURL}"><spring:message code="label.edit" /></a>
<a href="\AgentsCRUD\agent\add"><spring:message code="label.insert" /></a>
</td>
</tr>
</c:forEach>
</table>
</body>
控制器 AgentController.java 中的代码
@GetMapping("/edit")
public ModelAndView EditAnAgent(@QueryParam("agentId") int agentId) {
return new ModelAndView("/editAgent", "agent", service.getAgentById(agentId));
}
@GetMapping("/editAgent")
public ModelAndView editAgent(@Valid @ModelAttribute("agent") Agents a, BindingResult result, ModelMap model) {
if (result.hasErrors()) {
return new ModelAndView("/editAgent");
}
service.editAgent(a);
return new ModelAndView("redirect:/agent");
}
模型 AgentService.java 中的代码
public static Agents getAgentByID(int agentId) {
EntityManager em = DBUtil.getEMF().createEntityManager();
Agents a = null;
try {
a = em.createNamedQuery("Agents.findByAgentId", Agents.class)
.setParameter("agentId", (agentId))
.getSingleResult();
} catch (Exception ex) {
System.out.println("Error in getting property details: " + ex);
} finally {
em.clear();
}
return a;
}
public void editAgent(Agents a) {
EntityManager em = DBUtil.getEMF().createEntityManager();
EntityTransaction transaction = em.getTransaction();
try {
transaction.begin();
em.merge(a);
transaction.commit();
} catch (Exception e) {
System.out.println(e);
} finally {
em.close();
}
}
editAgent.jsp 中的代码
<form:form method="POST" action="/AgentsCRUD/agent/editAgent" modelAttribute="agent">
<table>
<tr>
<td><form:label path="agentId"><spring:message code="label.agentId" /></form:label></td>
<td><form:input path="agentId"/></td>
<td style="color:red"><form:errors path="agentId"/> </td>
</tr>
<tr>
<td><form:label path="name"><spring:message code="label.name" /></form:label></td>
<td><form:input path="name" /></td>
<td style="color:red"><form:errors path="name"/> </td>
</tr>
<tr>
<td><form:label path="phone"><spring:message code="label.phone" /></form:label></td>
<td><form:input path="phone"/></td>
<td style="color:red"><form:errors path="phone"/> </td>
</tr>
<tr>
<td><form:label path="fax"><spring:message code="label.fax" /></form:label></td>
<td><form:input path="fax"/></td>
<td style="color:red"> <form:errors path="fax"/> </td>
</tr>
<tr>
<td><form:label path="email"><spring:message code="label.email" /></form:label></td>
<td><form:input path="email"/></td>
<td style="color:red"> <form:errors path="email"/> </td>
</tr>
<tr>
<td><form:label path="username"><spring:message code="label.username" /></form:label></td>
<td><form:input path="username"/></td>
<td style="color:red"> <form:errors path="username"/> </td>
</tr>
<tr>
<td><form:label path="password"><spring:message code="label.password" /></form:label></td>
<td><form:input path="password"/></td>
<td style="color:red"> <form:errors path="password"/> </td>
</tr>
<tr>
<spring:message code="submit.button" var="labelSubmit"></spring:message>
<td><input type="submit" value="${labelSubmit}"/></td>
</tr>
</table>
</body>
</form:form>
解决方案
在错误中,给出的 url 是 .../edit/1 其中 1 是路径参数,
但是在获取映射中,您正试图将其作为查询参数读取。
尝试这个。
@GetMapping("/edit/{agentId}")
Patient getEmployee(@PathParam("agentId") int agentId) ;
推荐阅读
- javascript - 页面重新加载后用户会话删除 - React / Passport
- r - 在二进制因变量上运行 OLS
- python - 如何将一个数据框中的列堆叠到另一个数据框中的列上
- python - 生成图像文件而不将整个图像保存在内存中
- arrays - 如何在不知道深度的情况下遍历多维数组?
- java - 执行时找不到 Jar 类
- docker - 无法将烧瓶应用程序作为 docker 映像运行
- pytorch - 导入错误 default_collate 未定义
- typescript - 是否可以使用 TypeScript 防止自动转换为字符串?
- sql - 无法连接到 libpq 中的 postgresql