spring - 400 the request sent by the client was syntactically incorrect -
i have gone through many examples of nature , proposed solutions site, none of solutions provided thereon apply problem. believe error message, 400
, shows when information sent controller mulformed. spent last 2 days cross referrencing project worked on in past, works, cannot pick problem.
@requestmapping(value = {"/", "/home"}, method = requestmethod.get) public string homepage(modelmap model) { model.addattribute("user", getprincipal()); catalog catalog = catalogservice.getcatalogbycategory(catalog.catalogcategory.all); model.addattribute("catalog", catalog); return "welcome"; }
this sends data jstl spring form on jsp follows:
<form:form method="post" modelattribute="catalog"> <form:hidden path="id"/> <form:hidden path="name"/> <form:hidden path="category"/> <form:hidden path="orderitems"/> <div id="products" class="row list-group"> <c:foreach var="orderitem" items="${catalog.orderitems}"> <div class="item col-xs-4 col-lg-4"> <div class="thumbnail"> <img class="group list-group-image" src="http://placehold.it/400x250/000/fff" alt=""/> <div class="caption"> <h4 class="group inner list-group-item-heading"> ${orderitem.name}</h4> <p class="group inner list-group-item-text"> ${orderitem.description} </p> <div class="row"> <div class="col-xs-12 col-md-6"> <p class="lead"> r ${orderitem.price}</p> </div> <div class="col-xs-12 col-md-6"> <label for="${orderitem.id}" class="btn btn-primary">add cart <input type="checkbox" id="${orderitem.id}" name="orderitem.addedtocart" class="badgebox"><span class="badge">✓</span></label> </div> </div> </div> </div> </div> </c:foreach> </div> <div class="row"> <div class="form-group"> <div class="col-sm-12 pull-right"> </div> <div class="col-sm-2 pull-right"> <input type="submit" class="btn btn-default btn-block btn-primary" value="next" name="action" formmethod="post" formaction="confirmlist"/> </div> </div> </div> </form:form>`
at point submit form following listener in controller:
@requestmapping(value = "/confirmlist", method = requestmethod.post) public string confirmlist(@modelattribute catalog catalog, @modelattribute string numberofitemsadded) { list<orderitem> selecteditems = new arraylist<orderitem>(); (orderitem orderitem : catalog.getorderitems()) { if (orderitem.isaddedtocart()) { selecteditems.add(orderitem); } } //model.addattribute("numberofitemsadded", selecteditems.size()); return "welcome"; }
that's it, execution flow not reach controller. exhausting bug because not understand doing wrong here. thank in advance
edit:
catalog.java
@entity @table(name="catalogs") public class catalog{ private long id; //generated value using hibernate ... private string name; //column annotated @column private string category;// column annotated @column private list<orderitem> orderitems;// 1 many mapping //getters , setters here }
i tested code , got http 400 too. thing browser sends not match whith controller method confirmlist expects:
this form data saw in chrome's network tab:
id:1 name:the catalog category:category orderitems:[com.eej.ssba2.model.test.catalog.orderitem@82ea8a, com.eej.ssba2.model.test.catalog.orderitem@f441ae, com.eej.ssba2.model.test.catalog.orderitem@40a13, com.eej.ssba2.model.test.catalog.orderitem@1316c95, com.eej.ssba2.model.test.catalog.orderitem@1cfc05a, com.eej.ssba2.model.test.catalog.orderitem@5d725c, com.eej.ssba2.model.test.catalog.orderitem@ff32b9, com.eej.ssba2.model.test.catalog.orderitem@5b49a4, com.eej.ssba2.model.test.catalog.orderitem@13faf31, com.eej.ssba2.model.test.catalog.orderitem@6d64d] orderitem.addedtocart:on orderitem.addedtocart:on orderitem.addedtocart:on orderitem.addedtocart:on action:next
but controller cannot understand this, orderitems shows tostring() of each orderitem instance , addedtocart not binded orderitem of orderitems list.
you must modify jsp way:
<form:form method="post" modelattribute="catalog"> <form:hidden path="id"/> <form:hidden path="name"/> <form:hidden path="category"/> <!-- form:hidden path="orderitems"/--> <div id="products" class="row list-group"> <c:foreach var="orderitem" items="${catalog.orderitems}" varstatus="status"> <div class="item col-xs-4 col-lg-4"> <div class="thumbnail"> <img class="group list-group-image" src="http://placehold.it/400x250/000/fff" alt=""/> <div class="caption"> <h4 class="group inner list-group-item-heading"> ${orderitem.name}</h4> <form:hidden path="orderitems[${status.index}].name" /> <p class="group inner list-group-item-text"> ${orderitem.description} <form:hidden path="orderitems[${status.index}].description" /> </p> <div class="row"> <div class="col-xs-12 col-md-6"> <p class="lead"> r ${orderitem.price}</p> <form:hidden path="orderitems[${status.index}].price" /> </div> <div class="col-xs-12 col-md-6"> <label for="${orderitem.id}" class="btn btn-primary">add cart <input type="checkbox" id="${orderitem.id}" name="catalog.orderitems[${status.index}].addedtocart" class="badgebox"><span class="badge">✓</span></label> </div> </div> </div> </div> </div> </c:foreach> </div> <div class="row"> <div class="form-group"> <div class="col-sm-12 pull-right"> </div> <div class="col-sm-2 pull-right"> <input type="submit" class="btn btn-default btn-block btn-primary" value="next" name="action" formmethod="post" formaction="confirmlist"/> </div> </div> </div> </form:form>
if so, see message changes in chrome's network tab (or browser using). form data right now:
id:1 name:the catalog category:category orderitems[0].name:orderitemname#0 orderitems[0].description:orderitemdesc#0 orderitems[0].price:0.0 orderitems[1].name:orderitemname#1 orderitems[1].description:orderitemdesc#1 orderitems[1].price:0.43645913001303904 orderitems[2].name:orderitemname#2 orderitems[2].description:orderitemdesc#2 orderitems[2].price:1.7151992716801088 orderitems[3].name:orderitemname#3 orderitems[3].description:orderitemdesc#3 orderitems[3].price:1.303683806806788 orderitems[4].name:orderitemname#4 orderitems[4].description:orderitemdesc#4 orderitems[4].price:2.507039003743686 orderitems[5].name:orderitemname#5 orderitems[5].description:orderitemdesc#5 orderitems[5].price:3.173744751378154 orderitems[6].name:orderitemname#6 orderitems[6].description:orderitemdesc#6 orderitems[6].price:3.183771167856446 catalog.orderitems[6].addedtocart:on orderitems[7].name:orderitemname#7 orderitems[7].description:orderitemdesc#7 orderitems[7].price:6.73370053587355 catalog.orderitems[7].addedtocart:on orderitems[8].name:orderitemname#8 orderitems[8].description:orderitemdesc#8 orderitems[8].price:2.0266022634803216 orderitems[9].name:orderitemname#9 orderitems[9].description:orderitemdesc#9 orderitems[9].price:5.251986962977732 catalog.orderitems[9].addedtocart:on action:next
and see returns http 200 request in fact reaches controller. delete @modelattribute
in controller method too, have been suggested to:
@requestmapping(value = "/confirmlist", method = requestmethod.post) public string confirmlist(catalog catalog, string numberofitemsadded) { list<orderitem> selecteditems = new arraylist<orderitem>(); (orderitem orderitem : catalog.getorderitems()) { if (orderitem.isaddedtocart()) { selecteditems.add(orderitem); } } //model.addattribute("numberofitemsadded", selecteditems.size()); return "catalog"; }
Comments
Post a Comment