简体   繁体   中英

how to databind list selected checkbox in spring mvc

Hi I am new to Spring Mvc i have 3 tables first is role,resources,roleresource tables respectively. I have a created a jsp in which a list of resources are shown along with check boxes and also rolename where user enters,my prob is i need to insert rolename to role table and generated roleid to roleresource table with selected resources ids. problem here is i am not able to bind selected checkbox values here is my controller

  package com.steadyground.controller;
  import java.util.ArrayList;
  import java.util.List;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.ModelMap;
  import org.springframework.validation.BindingResult;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import com.steadyground.constants.URLConstants;
  import com.steadyground.entity.Organization;
  import com.steadyground.entity.Resources;
  import com.steadyground.form.RoleResourceForm;
  import com.steadyground.service.ResourcesService;

 @Controller
 public class RoleResoucesMappingController {

@Autowired
private ResourcesService resourcesService;



@RequestMapping(value = URLConstants.ROLERESOURCEMAPPING_PAGE, method = RequestMethod.GET)
public String landRoleResourceMapping(ModelMap map) 
{
    map.addAttribute("roleResourceForm",new RoleResourceForm());

    return "roleResourcesMapping";
}

@RequestMapping(value = URLConstants.ROLERESOURCEMAPPING_ADD, method = RequestMethod.POST)
public String createRoleResourceMapping(@ModelAttribute(value="roleResourceForm") RoleResourceForm roleResourceForm, BindingResult result, ModelMap map) 
{
    System.out.println(roleResourceForm.getResources());
    System.out.println(roleResourceForm.getResources().size());
    //System.out.println(roleResourceForm.getRole().getRoleResources());

    return "roleResourcesMapping";
}

@ModelAttribute("resources")
public List<Resources> getAllResources() {
    List<Resources> listResources = new ArrayList<Resources>();
    listResources = resourcesService.getAllResources(); 
    return listResources;
}
    }

here is my role.java file

    @Entity
    @Table(name = "role", catalog = "steadyground")
    public class Role implements java.io.Serializable {

private Integer roleId;
private String roleName;


private Set<RoleResource> roleResources = new HashSet<RoleResource>(0);

public Role() {
}

public Role(String roleName) {
    this.roleName = roleName;
}

public Role(String roleName, String applicationName,
        Set<RoleResource> roleResources) {
    this.roleName = roleName;
    this.roleResources = roleResources;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "role_id", unique = true, nullable = false)
public Integer getRoleId() {
    return this.roleId;
}

public void setRoleId(Integer roleId) {
    this.roleId = roleId;
}

@Column(name = "role_name", nullable = false, length = 100)
public String getRoleName() {
    return this.roleName;
}

public void setRoleName(String roleName) {
    this.roleName = roleName;
}


@OneToMany(fetch = FetchType.LAZY, mappedBy = "role")
public Set<RoleResource> getRoleResources() {
    return this.roleResources;
}

public void setRoleResources(Set<RoleResource> roleResources) {
    this.roleResources = roleResources;
}

    }

here is my resources.java

    @Entity
    @Table(name = "resources", catalog = "steadyground")
    public class Resources implements java.io.Serializable {

private Integer resourceId;
private String url;
private String urlName;

public Resources() {
}

public Resources(String url, String urlName) {
    this.url = url;
    this.urlName = urlName;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "resource_id", unique = true, nullable = false)
public Integer getResourceId() {
    return this.resourceId;
}

public void setResourceId(Integer resourceId) {
    this.resourceId = resourceId;
}

@Column(name = "url", length = 100)
public String getUrl() {
    return this.url;
}

public void setUrl(String url) {
    this.url = url;
}

@Column(name = "url_name", length = 200)
public String getUrlName() {
    return this.urlName;
}

public void setUrlName(String urlName) {
    this.urlName = urlName;
}

    }

here is my roleresource.java

  @Entity
  @Table(name="role_resource"
  ,catalog="steadyground"
  )
  public class RoleResource  implements java.io.Serializable {


 private Integer roleResourceId;
 private Role role;
 private Integer resourceId;

public RoleResource() {
}

public RoleResource(Role role, Integer resourceId) {
   this.role = role;
   this.resourceId = resourceId;
}

 @Id @GeneratedValue(strategy=IDENTITY)


@Column(name="role_resource_id", unique=true, nullable=false)
public Integer getRoleResourceId() {
    return this.roleResourceId;
}

public void setRoleResourceId(Integer roleResourceId) {
    this.roleResourceId = roleResourceId;
}

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="role_id")
public Role getRole() {
    return this.role;
}

public void setRole(Role role) {
    this.role = role;
}


@Column(name="resource_id")
public Integer getResourceId() {
    return this.resourceId;
}

public void setResourceId(Integer resourceId) {
    this.resourceId = resourceId;
}




}

and my jsp page

 <springform:form method="post" action="createRoleResourcesMapping"  class="form-horizontal" commandName="roleResourceForm" >
     <div class="span12">
        <div class="center">
            <div class="control-group span6">
                <label class="control-label" for="Role_Id">Role Id</label>
                <div class="control-group span6">
                    <label class="control-label" for="url_Name">Role Name</label>   

                    <div class="controls">
                        <div class="span12">
                            <springform:input path="role.roleName"/>
                        </div>
                    </div>
                </div>


                <div class="page-header position-relative"></div>

                <table id="sample-table-2" class="table table-striped table-bordered table-hover">
                    <thead>
                        <tr>
                            <th>Resources</th>
                        </tr>
                    </thead>

                    <tbody>
                        <tr>
                            <td><springform:checkboxes    path="resources" items="${resources}" itemLabel="urlName" itemValue="resourceId"/> 
                            </td></tr></tbody>
                        </table>

                    <div class="controls">
                        <div class="span12">
                            <input type="submit" class="btn btn-primary" value="Submit">
                            <input type="button" class="btn " value="Cancel">
                        </div>
                    </div>

            </div>
        </div>
 </springform:form> 

could someone help me in how to receive the data and save it in two tables

<springform:checkboxes    path="resources" items="${resources}" itemLabel="urlName" itemValue="resourceId"/>

From your code, what I've understood, the RoleResourcesForm is more like a wrapper of the 2 entities Resource & Role with another object resources . I think, to use form:checkboxes you better give it an object List in the path .

And what's this variable resources ? If it's really an List object in the RoleResourcesForm wrapper, in the items, you should use

items="${roleResourceForm.resources}"

When you commit it, it will send the form model attribute with only checked checkbox values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM