2014-10-01 151 views
0

夥計們,你能幫助我,我堅持這個問題,我有一個表格,我選擇不同的服務,應該添加到訂單。問題是我無法獲得所選複選框的這些值,甚至在控制檯中也沒有任何錯誤。看起來我的servlet現在從未運行,我嘗試了不同的方法來解決這個問題,但沒有任何幫助。多個複選框與JSP和Spring MVC,如何獲取值

這裏是在jsp頁面:

<!-- SERVICE TABLE --> 
    <form class="form-horizontal" action="/clients/addOrder/${client.id}" method="POST" > 
    <table class="table table-striped table-bordered table-condensed" > 
     <tr> 
      <th><spring:message code="label.serviceId" /></th> 
      <th><spring:message code="label.serviceName" /></th> 
      <th><spring:message code="label.servicePrice" /></th> 
      <th><spring:message code="label.actions" /></th> 

     </tr> 
     <c:forEach var="service" items="${servicesList}"> 
      <tr id="${service.service_id}"> 
       <td><c:out value="${service.service_id}" /></td> 
       <td><c:out value="${service.service_name}" /></td> 
       <td><c:out value="${service.service_price}" /></td> 
       <td><input type="checkbox" name="serviceBox" 
        value="${service.service_id}" /></td> 
      </tr> 
     </c:forEach> 
    </table> 
    <div class="form-group form-group-sm"> 
     <div class="col-sm-offset-2 col-sm-10"> 
     <a class="pull-right"> 
      <button class="btn btn-primary" type="submit"><c:out value="Add order"/></button> 
     </a> 
     </div> 
    </div> 
    <input type="hidden" name="clientId" value="${client.id}"> 
    </form> 

的servlet

@RequestMapping(value = "/addOrder/{clientId}", method = RequestMethod.POST) 
    public String addOrder(@Valid @PathVariable("clientId") Long clientId, @ModelAttribute("serviceBox") String[] services, BindingResult result, 
      Model model) { 
     System.out.println("IN addOrder POST"); 

     if (result.hasErrors()) { 
      AllServicesEvent ase = servicesService 
        .requestAllServices(new RequestAllServicesEvent()); 
      model.addAttribute("servicesList", ase.getServices()); 
      return "addOrderForm"; 
     } 
     System.out.println("NO MISTAKES"); 
     List<Services> servicesList = servicesService.requestService(new RequestServiceEvent(services)).getServicesList(); 
     System.out.println("Service List size: " + servicesList.size()); 
     if (servicesList != null && servicesList.size() > 0) { 
      // update orders amount and total price 
      System.out.println("IN IF METHOD"); 
      ClientUpdatedEvent cue = clientsService.updateClient(new UpdateClientEvent(clientId, servicesList)); 
      System.out.println("AFTER UPDATING CLIENTS"); 
      // add order to orders table, add order id, service id to order_service table 
      OrderCreatedEvent oce = ordersService.addOrder(new CreateOrderEvent(clientId, servicesList)); 
      System.out.println("AFTER ADDING ORDERS"); 
      return "redirect:/clients"; 
     } else { 
      return "addOrderForm"; 
     } 
    } 

在控制檯:

Oct 01, 2014 11:07:34 PM org.apache.coyote.AbstractProtocol start 
INFO: Starting ProtocolHandler ["http-bio-8080"] 
Oct 01, 2014 11:07:34 PM org.apache.coyote.AbstractProtocol start 
INFO: Starting ProtocolHandler ["ajp-bio-8009"] 
Oct 01, 2014 11:07:34 PM org.apache.catalina.startup.Catalina start 
INFO: Server startup in 4926 ms 
Oct 01, 2014 11:07:34 PM org.apache.jasper.compiler.TldLocationsCache tldScanJar 
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 
Hibernate: select clients0_.client_id as client_i1_3_, clients0_.birthdate as birthdat2_3_, clients0_.city as city3_3_, clients0_.country as country4_3_, clients0_.email as email5_3_, clients0_.first_name as first_na6_3_, clients0_.gender as gender7_3_, clients0_.last_name as last_nam8_3_, clients0_.orders as orders9_3_, clients0_.phone as phone10_3_, clients0_.total_income as total_i11_3_ from clients clients0_ 
Hibernate: select clients0_.client_id as client_i1_3_0_, clients0_.birthdate as birthdat2_3_0_, clients0_.city as city3_3_0_, clients0_.country as country4_3_0_, clients0_.email as email5_3_0_, clients0_.first_name as first_na6_3_0_, clients0_.gender as gender7_3_0_, clients0_.last_name as last_nam8_3_0_, clients0_.orders as orders9_3_0_, clients0_.phone as phone10_3_0_, clients0_.total_income as total_i11_3_0_ from clients clients0_ where clients0_.client_id=? 
Hibernate: select services0_.service_id as service_1_4_, services0_.service_name as service_2_4_, services0_.service_price as service_3_4_ from services services0_ 

而不是結果我只有與網頁:

HTTP Status 404 - /clients/addOrder/1 

-------------------------------------------------------------------------------- 

type Status report 

message /clients/addOrder/1 

description The requested resource is not available. 

UPD:這樣它的工作原理:

@RequestMapping(value = "/addOrder/{clientId}", method = RequestMethod.POST) 
public String addOrder(@PathVariable("clientId") Long clientId) { 
    System.out.println("IN addOrder POST, id" + clientId); 
    return "addOrderForm"; 
} 

所以,唯一的問題是如何在正確的方式獲得的servlet checkboxed值。

+0

您requestmapping是'/ addOrder/{}的clientId'和你從提交到'/客戶/ addOrder/$ {client.id}'。這對我來說並不合適。 – 2014-10-01 20:19:37

+0

我開始檢查空的servlet。它的工作原理與這些參數:\t @RequestMapping(值= 「/ addOrder/{的clientId}」,方法= RequestMethod.POST) \t公共字符串addOrder(@PathVariable( 「的clientId」)長的clientId){ \t \t的System.out .println(「IN addOrder POST,id」+ clientId); \t \t return「addOrderForm」; \t} – Cooler 2014-10-01 20:43:56

回答

2

那麼它通常被認爲是不好的做法,只給出一個鏈接在StackOverflow答案,但真的,RTFM! Spring參考手冊在usage of checkboxes上有一段。

以下是一部分摘錄:

<form:form> 
     <table> 
      <tr> 
       <td>Interests:</td> 
       <td> 
        <%-- Approach 2: Property is of an array or of type java.util.Collection --%> 
        Quidditch: <form:checkbox path="preferences.interests" value="Quidditch"/> 
        Herbology: <form:checkbox path="preferences.interests" value="Herbology"/> 
        Defence Against the Dark Arts: <form:checkbox path="preferences.interests" 
         value="Defence Against the Dark Arts"/> 
       </td> 
      </tr> 
     </table> 
    </form:form> 

關聯的模型是:

public class Preferences { 

    private String[] interests; 

    public String[] getInterests() { 
     return interests; 
    } 

    public void setInterests(String[] interests) { 
     this.interests = interests; 
    } 
} 

因此,使用<form:checkbox/>路徑除非你有一個很好的理由不這樣做,並使用模型包含一個數組,而不是一個一個數組

+0

哇!有用!塞爾,你是最棒的!你做了我的一天,非常感謝! – Cooler 2014-10-01 23:02:39

+0

如果我不知道複選框的數量,該怎麼辦?我很困惑 – athena 2015-09-16 21:19:43

0

<script> 
 
$(function() { 
 
\t \t $("#selectAll").click(function(){ 
 
\t \t  $('input:checkbox:not(:disabled)').prop('checked', this.checked); 
 
\t \t }); 
 
\t }) 
 
</script>
<table class="noborder" cellspacing="0" border="0" width="100%"> 
 
\t \t \t <tr> 
 
\t \t \t <td><form:label path="selectAll" id="h1"></form:label></td> 
 
\t \t \t <td><form:checkbox path="selectAll" id="selectAll" onclick="selectall(this);"/></td> 
 
\t \t \t <td id="h2">Sr. No.</td> 
 
\t \t \t <td id="h3">Login Name</td> 
 
\t \t \t </tr> 
 
\t \t \t <c:forEach var="loginNameList" items="${loginNameList}" varStatus="status"> 
 
\t \t \t \t <tr> 
 
\t \t \t \t \t <td><form:label path="selectAll" id="h1"></form:label></td> 
 
\t \t \t \t \t <td><form:checkbox path="lockedItems" value="${loginNameList.uId}" id="lockedItems${status}"/></td> 
 
\t \t \t \t \t <td>${status.count}</td> 
 
\t \t \t \t \t <td>${loginNameList.loginName}</td> 
 
\t \t \t \t </tr> 
 
\t \t \t </c:forEach> 
 
\t \t \t <tr> 
 
\t \t \t <td></td> 
 
\t \t \t <td><input type="submit" value="Unlock"></td> 
 
\t \t \t <td></td> 
 
\t \t \t <td></td> 
 
\t \t \t </tr> 
 
\t \t \t </table>