java - Spring Thymeleaf how to bind values of checkboxes to a collection field -
i want generate, or bind, project
object in spring using html form processed thymeleaf. works far, rolesneeded
list field filled roles ticking checkboxes isn't working, yet.
the project class
package com.floriantoenjes.instateam.model; import javax.persistence.*; import java.util.list; @entity public class project { @id @generatedvalue(strategy = generationtype.identity) private int id; private string name; private string description; private string status; @manytomany private list<role> rolesneeded; @manytomany private list<collaborator> collaborators; public project() { } public int getid() { return id; } public void setid(int id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getdescription() { return description; } public void setdescription(string description) { this.description = description; } public string getstatus() { return status; } public void setstatus(string status) { this.status = status; } public list<role> getrolesneeded() { return rolesneeded; } public void setrolesneeded(list<role> rolesneeded) { this.rolesneeded = rolesneeded; } public list<collaborator> getcollaborators() { return collaborators; } public void setcollaborators(list<collaborator> collaborators) { this.collaborators = collaborators; } }
the role class
package com.floriantoenjes.instateam.model; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; @entity public class role { @id @generatedvalue(strategy = generationtype.identity) private integer id; private string name; public role() { } public integer getid() { return id; } public void setid(integer id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } }
the controller
package com.floriantoenjes.instateam.web.controller; import com.floriantoenjes.instateam.model.project; import com.floriantoenjes.instateam.model.role; import com.floriantoenjes.instateam.service.projectservice; import com.floriantoenjes.instateam.service.roleservice; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import java.util.list; @controller public class projectcontroller { @autowired projectservice projectservice; @autowired roleservice roleservice; @requestmapping("/add") public string newprojectform(model model) { list<role> roles = roleservice.findall(); model.addattribute("project", new project()); model.addattribute("roles", roles); return "edit_project"; } @requestmapping(value = "/add", method = requestmethod.post) public string addproject(project project) { projectservice.save(project); return "redirect:/index"; } }
the template
<!doctype html> <html> <head th:replace="layout :: head('edit project')"></head> <body> <header> <div class="container"> <div class="site-header"> <a class="logo" href="index.html">instateam</a> <a class="new-project button icon-left" href="#"><i class="material-icons">add</i> new project</a> </div> </div> </header> <nav> <ul> <li class="selected"><a href="index.html">projects</a></li> <li><a href="collaborators.html">collaborators</a></li> <li><a href="roles.html">roles</a></li> </ul> </nav> <section> <div class="container wrapper"> <form th:object="${project}" action="" method="post"> <div> <label for="project_name"> project name:</label> <input type="text" th:field="*{name}" name="project_name"/> </div> <div> <label for="project_description">project description:</label> <textarea rows="4" th:field="*{description}" name="project_description"></textarea> </div> <div> <label for="project_status">project status:</label> <div class="custom-select"> <span class="dropdown-arrow"></span> <select th:field="*{status}" name="project_status"> <option value="active">active</option> <option value="archived">archived</option> <option value="not_started">not started</option> </select> </div> </div> <div> <label for="project_roles">project roles:</label> <ul class="checkbox-list"> <li th:each="role : ${roles}"> <input type="checkbox" th:field="*{rolesneeded}" name="project_roles" th:value="${role.id}"/> <span class="primary" th:text="${role.name}"></span> </li> </ul> </div> <div class="actions"> <input type="submit" value="save" class="button"/> <a href="#" class="button button-secondary">cancel</a> </div> </form> </div> </section> </body> </html>
as can see rolesneeded
collection , want able tick checkboxes roles , on submitting form generate project object roles assigned "rolesneeded" collection. have put right *{rolesneeded}
, {role.id}
doesn't work.
right getting following error:
there unexpected error (type=bad request, status=400). validation failed object='project'. error count: 1
hopefully has suggestion how solve or perhaps how able more detailed error message.
kind regards, florian
i had write own spring converter convert "string" "role", mark class @component , create @bean. worked charm.
@component public class stringroleconverter implements converter<string, role> { @override public role convert(string source) { role role = new role(); int id = integer.parseint(source); role.setid(id); return role; } @bean public conversionservice getconversionservice() { conversionservicefactorybean bean = new conversionservicefactorybean(); set<converter> converters = new hashset<>(); converters.add(new stringroleconverter()); bean.setconverters(converters); return bean.getobject(); } }
Comments
Post a Comment