2012-07-02 77 views
1

我試圖添加一個編輯功能到我的網絡應用程序,但我遇到了一些問題,使用@RequestParam。它得到的參數是空的,它不應該是。我希望有人能指出我犯了什麼錯誤。@RequestParam爲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> 

視圖 - 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 customerIdGET方法爲空?

謝謝 d

回答

5

您使用${customer.customerId}它的初始化之前:

<!-- you use it here --> 
<c:url var="editUrl" value="/test/customer/edit?customerId=${customer.customerId}"/> 
<c:if test="${!empty customers}"> 
.... 
<tbody> 
<!-- and initialize it here --> 
<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> 

只需設置editUrl內循環:

<c:if test="${!empty customers}"> 
.... 
<tbody> 
<c:forEach items="${customers}" var="customer"> 
    <c:url var="editUrl" value="/test/customer/edit?customerId=${customer.customerId}"/> 
    <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> 

,它應該工作。無論如何,您必須爲每位客戶重置editUrl

+0

謝謝你是這個問題:) – dlinx90

1

可能是因爲您接受customerId爲Integer,請嘗試將其接受爲String。 試試這一個:

@RequestMapping(value = "/edit", method = RequestMethod.GET) 
public String getEdit(@RequestParam("customerId") String customerId, Model model) { 
Customer existingCustomer = customerService.retrieveCustomer(Integer.parseInt(customerId)); 
    model.addAttribute("customerAttribute", existingCustomer); 
    return "edit-customer"; 
} 

@RequestMapping(value = "/edit", method = RequestMethod.POST) 
public String postEdit(@RequestParam("customerId") String customerId, 
     @ModelAttribute("customerAttribute") @Valid Customer customer, BindingResult result) { 
    if (result.hasErrors()) { 
     return "edit-customer"; 
    } 
    customer.setCustomerId(Integer.parseInt(customerId)); 
    customerService.editCustomer(customer); 
    return "redirect:/test/customer/list"; 
相關問題