英文:
duplicate url in spring form
问题
我有一个控制器,它的角色是将客户添加到我的列表中,我有两个方法 showFormForAdd()
用于接收参数, saveCustomer()
用于保存客户并返回列表页面,我有一个父请求映射名为 /c
。问题是,当我点击提交按钮时,它会重复映射URL,如 /c/customer/c/customer/savecustomer
,我无法保存我的客户。我发现,如果我将 action="c/customer/savecustomer"
更改为 action="savecustomer"
,它就能正常工作,但我不知道为什么以及一开始出了什么问题。我正在使用JSP和Spring 5 MVC。
控制器:
@Controller
@RequestMapping("/c")
public class CustomerController {
@GetMapping("/customer/showFormForAdd")
public String showFormForAdd(Model theModel) {
Customer theCustomer = new Customer();
theModel.addAttribute("customer", theCustomer);
return "customer-form";
}
@PostMapping("/customer/savecustomer")
public String saveCustomer(@ModelAttribute("customer") Customer theCustomer) {
customerService.saveCustomer(theCustomer);
return "redirect:/c/customer/list";
}
}
JSP页面:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/style.css"/>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/add-customer-style.css"/>
</head>
<body>
<div id="wrapper">
<div id="header">
<h2>CRM CUSOMER MANAGER</h2>
</div>
</div>
<div id="container">
<form:form action="c/customer/savecustomer" modelAttribute="customer" method="post">
<form:hidden path="id"/>
<table>
<tbody>
<tr>
<td><label>First Name</label></td>
<td><form:input path="firstName"/></td>
</tr>
<tr>
<td><label>Last Name</label></td>
<td><form:input path="lastName"/></td>
</tr>
<tr>
<td><label>Email</label></td>
<td><form:input path="email"/></td>
</tr>
<tr>
<td><label></label></td>
<td><input type="submit" value="save" class="save"></td>
</tr>
</tbody>
</table>
</form:form>
<div style="clear: both"></div>
<p>
<a href="${pageContext.request.contextPath}/customer/list">
Back to the list
</a>
</p>
</div>
</body>
</html>
日志:
31-Jul-2020 09:42:23.928 WARNING [http-nio-8080-exec-4] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/javacongif_war/] in DispatcherServlet with name 'dispatcher'
希望这些帮助你解决问题。如果你有更多问题,请随时提问。
英文:
i have a controller that has a role to add the customer to my list and i have two methods showFormForAdd()
to take a parameter and saveCustomer()
for saving a customer and comeback to the list page and i have a parent request mapping named /c
. the problem is that when i click on my submit button it duplicate the mapping url like this /c/customer/c/customer/savecustomer
and i am unable to save my customer.i found out it works fine if i change action="c/customer/savecustomer"
to action="savecustomer"
but i have no idea why and what was wrong with that in the first place.i am using jsp and spring 5 mvc.
contoller
@Controller
@RequestMapping("/c")
public class CustomerController {
@GetMapping("/customer/showFormForAdd")
public String showFormForAdd(Model themodel){
Customer thecustomer=new Customer();
themodel.addAttribute("customer",thecustomer);
return "customer-form";
}
@PostMapping("/customer/savecustomer")
public String saveCustomer(@ModelAttribute("customer") Customer thecustomer){
customerService.saveCustomer(thecustomer);
return "redirect:/c/customer/list";
}
}
jsp page
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/style.css"/>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/add-customer-style.css"/>
</head>
<body>
<div id="wrapper">
<div id="header">
<h2>CRM CUSOMER MANAGER</h2>
</div>
</div>
<div id="container">
<%-- <form:form action="/customer/savecustomer" --%>
<form:form action="c/customer/savecustomer" modelAttribute="customer" method="post" >
<form:hidden path="id"/>
<table>
<tbody>
<tr>
<td><label>First Name</label></td>
<td><form:input path="firstName"/></td>
</tr>
<tr>
<td><label>last Name</label></td>
<td><form:input path="lastName"/></td>
</tr>
<tr>
<td><label>email </label></td>
<td><form:input path="email"/></td>
</tr>
<tr>
<td><label> </label></td>
<td><input type="submit" value="save" class="save"></td>
</tr>
</tbody>
</table>
</form:form>
<div style="clear: both"></div>
<p>
<a href="${pageContext.request.contextPath}/customer/list">
back to the form </a>
</p>
</div>
</body>
</html>
log
31-Jul-2020 09:42:23.928 WARNING [http-nio-8080-exec-4] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/javacongif_war/] in DispatcherServlet with name 'dispatcher'
答案1
得分: 0
当您在URL开头不使用斜杠时,它会将URL添加到当前URL中(这称为相对URL)。
将<form:form action="c/customer/savecustomer"
更改为<form:form action="/c/customer/savecustomer"
以使用绝对URL。
英文:
when you use url without / in the beginning it adds the url to the current url (it is called relative url)
change <form:form action="c/customer/savecustomer"
to <form:form action="/c/customer/savecustomer"
in order to use absolute url
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论