2016-03-30 14 views
3

java.lang.IllegalStateException: Weder BindingResult noch ebene Zielobjekt in Ajax-Aufrufjava.lang.IllegalStateException: Weder BindingResult noch ebene Zielobjekt in Ajax-Aufruf

I Thymeleaf und Spring MVC verwende und ich habe einige Probleme mit einer dynamischen Form. Ich habe ein Formular mit einem Selektor, und wenn dies geändert wird, mache ich einen Ajax-Aufruf, der einen anderen Selektor und ein Feld anzeigen sollte.

Ich habe diese Objekte:

public class SaleMini{ 
    private int businessId; 
    private int sellerId; 
    private int productId; 
    private int amount; 
    private int row; 
    private String date; 
    private List<SaleItem> item; 
    //getters and setters 
} 

public class SaleItem{ 
    private int productId; 
    private int amount; 
    private boolean gift; 
    private List<Integer> components; 
    private List<Composition> compositionList; 
    //getters and setters 

}

Mein HTML-Code ist:

<form id="sales" action="#" th:action="@{/sales/add}" method="post">      
    <div class="row"> 
    <div class="form-group col-md-6"> 
     <label class="label-control" th:text="#{label.equipment}"></label> 
     <select th:field="${sales.businessId}" class="form-control" onchange="submitData()"> <!--- Equipment List ---> 
      <option th:each="e : ${equipmentList}" th:value="${e.id}" th:text="${e.name}"></option> 
     </select> 
    </div> 

     <div class="form-group col-md-6"> 
     <label class="label-control" th:text="#{label.seller}"></label> 
     <select th:field="${sales.sellerId}" class="form-control"> 
      <option th:each="s : ${sellerList}" th:value="${s.id}" th:text="${s.name + ' ' + s.surname}"></option> 
     </select> 
     </div> 
    </div> 

    <div id="product-panel" class="row" > 
    <div th:fragment="resultsList"> 
     <div th:each="i,rowStat : ${itemList}"> 
      <p th:text="${i.productId}"></p> 
       <select class="form-control products_select" th:field="${i.productId}" th:onchange="${'javascript:callComposed(' + rowStat.index + ')'}" > 
        <option value="0" >Select Product</option> 
        <option th:each="p : ${productList}" th:value="${p.id}" th:text="${p.name}" th:attr="data-compound=${p.compound},data-generic=${p.genericId}"></option> 
       </select> 
      </div> 
      <a class="btn btn-action" id="btn-add" onclick="submitData()" style="margin-top: 25px !important;"><span class="fa fa-plus fa-btn"></span></a> <!--I should add as many product as I wanted--> 
    </div>      
    </div> 

    <div class="row"> 
     <div class="form-btn"> 
     <input type="submit" th:value="#{label.save.sale}" class="btn btn-custom"/> 
     </div> 
    </div> 
</form> 

Wenn die Ausrüstungsliste ändern ist, muss ich einen Ajax-Aufruf

function submitData(){ 
$.ajax({ 
    'url': 'sales/addRow', 
    'type': 'POST', 
    'data': $('#sales').serialize(), 
    'success': function(result){ 
     $("#product-panel").html(result); 
    }, 
}); 
} 

Die Funktion rufe ich an auf der Steuerung ist:

Das Problem ist, wenn ich submitData() aufrufen.

Ich kann den Anruf an die Steuerung gut tun (submitData() und dann addRow), und es funktioniert, aber wenn ich die Daten bekommen ich habe und Fehler:

java.lang.IllegalStateException: Weder BindingResult noch einfaches Zielobjekt für Bean Name 'i' verfügbar als Anfrageattribut Ich bekomme Daten nach dem Aufruf, aber ich kann nicht auf die Daten mit th zugreifen: Feld

Im HTML-Teil funktioniert das (th: Text):

<p th:text="${i.productId}"></p> 

Aber das nicht (th: f ield), und ich weiß nicht, warum:

<select class="form-control products_select" th:field="${i.productId}" th:onchange="${'javascript:callComposed(' + rowStat.index + ')'}" > 
</select> 

Vielen Dank im Voraus

Antwort

0

Hallo, ich denke, Sie ein paar Details in Ihrer Form fehlen. Mit diesem th: Objekt = „Verkauf“ sagen Sie, was die modelAttribute Ihrer Form sein, und Bezugnahme auf eine Eigenschaft des Objekts zu machen nur * verwenden {attribute.path}

<form id="sales" action="#" th:action="@{/sales/add}" method="post" th:object="sale"> 

Und Bezug auf die Attribute des Verkaufsobjekts Gebrauch zu machen:

<select th:field="*{businessId}" class="form-control" onchange="submitData()"> <!--- Equipment List ---> 
     <option th:each="e : ${equipmentList}" th:value="${e.id}" th:text="${e.name}"></option> 
    </select> 
+0

Danke, aber ich habe versucht bereits, und es ist das gleiche, wenn ich th verwenden: Objekt und ich dann * oder ohne th: Objekt und $ – Zuri

Verwandte Themen