gpt4 book ai didi

Spring 将表单的 Action 映射到 Controller

转载 作者:行者123 更新时间:2023-12-04 14:31:41 24 4
gpt4 key购买 nike

我是 Spring Framework 的新手,我已经使用该方法创建了一个 Controller

@RequestMapping("/fetch/{contactId}")
public String getContact(@PathVariable("contactId") Long contactId,
Map<String, Object> map, HttpServletRequest request,
HttpServletResponse response) {

Contact contact = contactService.get(contactId);
map.put("contact", contact);
return "contact";
}

当用户单击 jsp 上的链接时,调用此 fetch 方法以检索联系人详细信息
<td><a href="fetch/${contact.id}" class="edit">Edit</a></td>

然后它成功返回联系人对象并显示在屏幕上供用户更改和保存。我的jsp的form标签是这样的
<form:form method="post" action="add.html" commandName="contact"
id="contact" onsubmit="return validateContact(this)">

现在的问题是当我尝试将页面提交到 URL 更改为同一 Controller 中的另一种方法时

/myapp/app/contacts/fetch/add.html

而它应该是

/myapp/app/contacts/add.html

我知道有些事情我没有做对,但究竟是什么我无法弄清楚。感谢你们中的任何人能帮助我解决问题

谢谢
机管局

最佳答案


<c:url var="addUrl" value="/contacts/add.html"/>
<form:form method="post" action="${addUrl}" commandName="contact"
id="contact" onsubmit="return validateContact(this)">

一般情况下,推荐使用 c:url在每个应用程序内部而不是直接使用 <a> 中的 url标签。

关于Spring 将表单的 Action 映射到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12418427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com