java - Jsp form post pass an empty object to Spring controller -
in current controller fill userwrapper collection of users haven't access use application , send attribute jsp render.
@requestmapping(value = "/cockpit") public string cockpit(model model, userwrapper receiveduserwrapper) { siftoutenabled(receiveduserwrapper); try { list<user> users = userservice.findalluserswhonotaccepted(); userwrapper userwrapper = new userwrapper(); userwrapper.setusers(users); model.addattribute("wrapper", userwrapper); model.addattribute("users", userwrapper.getusers()); } catch (usersnotfoundexception e) { } return "cockpit"; }
in jsp exists checkboxes binded user objects of userwrapper collection users. jsp page like:
<form:form method="post" commandname="wrapper" action="cockpit"> <table> <tr> <th>id</th> <th>mail</th> <th>pass</th> <th>registrationdate</th> <th></th> </tr> <c:foreach items="${users}" var="user" varstatus="loop"> <tr> <td>${user.id}</td> <td>${user.email}</td> <td>${user.password}</td> <td>${user.registrationdate}</td> <td><form:checkbox path="users[${loop.index}].enabled" /> </tr> </c:foreach> </table> <input type="submit" name="submit" value="submit" /> </form:form>
this jsp page render proper result:
id mail pass registrationdate 1 market@test.ra pass 2016-02-02 21:46:40.0 2 tara@ra.ra passwd 2016-02-03 20:37:18.0
near every line exists checbox, when check lines, , send post controller, there catch these values? checkboxes work.
[user [id=0, email=null, password=null, enabled=true], user [id=0, email=null, password=null, enabled=false]]
why values null or 0? here userwrapper class, , user consrtuctor:
public class user implements serializable { ... public user(long id, string email, string password, date registrationdate, boolean enabled) { this.setemail(email); this.setpassword(password); this.registrationdate = registrationdate; this.setenabled(enabled); roles = new hashset<>(); } ... } public class userwrapper { private boolean checked; private list<user> users; ... }
update ask: how send object controller jsp, change object there , send changed object controller...
update
public class springmvcintializer extends abstractannotationconfigdispatcherservletinitializer { @override protected class<?>[] getrootconfigclasses() { return new class[] { applicationconfig.class }; } @override protected class<?>[] getservletconfigclasses() { return new class<?>[] { webmvcconfig.class }; } @override protected string[] getservletmappings() { return new string[] { "/" }; } }
problem in jsp code, there checkbox sends form data in name , value pair. hence values checkbox. try updating jsp follows.
<td>${user.id}<input type="hidden" name="users[${loop.index}].id" value="${user.id}"/></td> <td>${user.email}<input type="hidden" name="users[${loop.index}].email" value="${user.email}"/></td> <td>${user.password}<input type="hidden" name="users[${loop.index}].password" value="${user.password}"/></td> <td>${user.registrationdate}<input type="hidden" name="users[${loop.index}].registrationdate" value="${user.registrationdate}"/></td> <td><form:checkbox path="users[${loop.index}].enabled" />
edited
for dates need customdateeditor registered in controller class as:
@initbinder public void initbinder(webdatabinder binder) { customdateeditor editor = new customdateeditor(new simpledateformat("mm/dd/yyyy"), true); binder.registercustomeditor(date.class, editor); }
Comments
Post a Comment