Я пытаюсь добавить функцию редактирования в свое веб-приложение, но у меня возникли проблемы с использованием @RequestParam
. Параметр, который он получает, равен null, которого это не должно быть. Я надеюсь, кто-то может указать, где я совершил ошибку.@RequestParam is null (Spring MVC)
Вот методы от контроллера:
@RequestMapping(value = "/edit", method = RequestMethod.GET)
public String getEdit(@RequestParam("customerId") Integer customerId, Model model) {
Customer existingCustomer = customerService.retrieveCustomer(customerId);
model.addAttribute("customerAttribute", existingCustomer);
return "edit-customer";
}
@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String postEdit(@RequestParam("customerId") Integer customerId,
@ModelAttribute("customerAttribute") @Valid Customer customer, BindingResult result) {
if (result.hasErrors()) {
return "edit-customer";
}
customer.setCustomerId(customerId);
customerService.editCustomer(customer);
return "redirect:/test/customer/list";
и две страницы JSP
редактировать-customer.jsp:
<body>
<h1>Edit Existing Customer</h1>
<c:url var="saveUrl" value="/test/customer/edit?customerId=${customerAttribute.customerId}" />
<form:form modelAttribute="customerAttribute" method="POST" action="${saveUrl}">
<table>
<tr>
<td><form:label path="customerId">Customer Id:</form:label></td>
<td><form:input path="customerId" disabled="true"/></td>
</tr>
<tr>
<td><form:label path="customerCountry">Customer Country</form:label></td>
<td><form:input path="customerCountry"/></td>
</tr>
<tr>
<td><form:label path="customerName">Customer Name:</form:label></td>
<td><form:input path="customerName"/></td>
</tr>
</table>
<input type="submit" value="Save" />
</form:form>
</body>
view- all-customers.jsp:
<body>
<a href="<c:url value="/test/home"/>">Home</a>
<h1>Customers</h1>
<c:url var="addUrl" value="/test/customer/add" />
<c:url var="editUrl" value="/test/customer/edit?customerId=${customer.customerId}"/>
<c:if test="${!empty customers}">
<a href="${addUrl}">Add</a>
</c:if>
<table style="border: 1px solid; width: 500px; text-align:center">
<thead style="background:#ccc">
<tr>
<th>Customer Id</th>
<th>Customer Country</th>
<th>Customer Name</th>
<th colspan="4"></th>
</tr>
</thead>
<tbody>
<c:forEach items="${customers}" var="customer">
<tr>
<td><c:out value="${customer.customerId}" /></td>
<td><c:out value="${customer.customerCountry}" /></td>
<td><c:out value="${customer.customerName}" /></td>
<td><a href="${editUrl}">Edit</a></td>
</tr>
</c:forEach>
</tbody>
</table>
<c:if test="${empty customers}">
There are currently no customers in the list. <a href="${addUrl}">Add</a> a customers.
</c:if>
</body>
Может кто-нибудь понять, почему Integer customerId
в методе GET
равна нулю?
Спасибо, D
Спасибо, что была проблема :) – dlinx90