【问题标题】:@RequestParam is null (Spring MVC)@RequestParam 为空(Spring MVC)
【发布时间】:2012-07-02 10:17:08
【问题描述】:

我正在尝试向我的网络应用程序添加一个编辑功能,但我在使用 @RequestParam 时遇到了一些问题。它得到的参数是 null ,它不应该是。我希望有人能指出我在哪里犯了错误。

以下是控制器的方法:

@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页面

edit-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>

查看所有客户.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>

谁能看到为什么GET 方法中的Integer customerId 为空?

谢谢你, D

【问题讨论】:

    标签: java spring model-view-controller jsp spring-mvc


    【解决方案1】:

    您在初始化之前使用${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

    【讨论】:

    • 谢谢你的问题:)
    【解决方案2】:

    这可能是因为您将 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";
    

    【讨论】:

      猜你喜欢
      • 2016-02-25
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多