【发布时间】:2020-07-31 15:01:49
【问题描述】:
我有一个控制器,负责将客户添加到我的列表中,我有两种方法 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="/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>
日志
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'
【问题讨论】: