Spring 将表单的动作映射到控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12418427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring mapping form's action to the controller
提问by Ashish Abrol
I am new to the Spring Framework and I have created a controller with the method
我是 Spring Framework 的新手,我已经使用该方法创建了一个控制器
@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";
}
This fetch method is called to retrieve the contact details when the user clicks on the link on a jsp
当用户单击 jsp 上的链接时,调用此 fetch 方法以检索联系人详细信息
<td><a href="fetch/${contact.id}" class="edit">Edit</a></td>
It then successfully returns the contact object and displays on the screen for the user to change and save. The form tag of my jsp is like this
然后它成功返回联系人对象并显示在屏幕上供用户更改和保存。我的jsp的form标签是这样的
<form:form method="post" action="add.html" commandName="contact"
id="contact" onsubmit="return validateContact(this)">
Now the problem is when I try to submit the page to another method in the same controller the URL changes to
现在的问题是当我尝试将页面提交到 URL 更改为同一控制器中的另一种方法时
/myapp/app/contacts/fetch/add.html
/myapp/app/contacts/fetch/add.html
whereas it should be
而它应该是
/myapp/app/contacts/add.html
/myapp/app/contacts/add.html
I know there is something which I am not doing correctly but what exactly I am not able to figure out. Appreciate if any one of you could help me resolve the issue
我知道有些事情我没有做对,但究竟是什么我无法弄清楚。感谢你们中的任何人能帮助我解决问题
Thanks AA
谢谢 AA
回答by Ralph
Use
用
<c:url var="addUrl" value="/contacts/add.html"/>
<form:form method="post" action="${addUrl}" commandName="contact"
id="contact" onsubmit="return validateContact(this)">
In general, it is recomended to use c:urlin every application internal instead of direct use of the url in a <a>tag.
通常,建议c:url在每个应用程序内部使用,而不是直接在<a>标签中使用 url 。
回答by Sten
<form:form method="post" servletRelativeAction="/contacts/add" commandName="contact"
id="contact" onsubmit="return validateContact(this)">
Use attribute servletRelativeAction to map to desired controller action. I assume that your desired controller is mapped as '/contacts/add' not 'add.html'. You want to hit the controller not the view.
使用属性 servletRelativeAction 映射到所需的控制器操作。我假设您想要的控制器被映射为“/contacts/add”而不是“add.html”。您想点击控制器而不是视图。
回答by xyz
Use /contacts/add.htmlin actionattribute
Change
/contacts/add.html在action属性
更改中 使用
<form:form method="post" action="add.html" commandName="contact"
id="contact" onsubmit="return validateContact(this)">
to
到
<form:form method="post" action="/contacts/add.html" commandName="contact"
id="contact" onsubmit="return validateContact(this)">

