如何在spring mvc中数据绑定列表选中的复选框

时间:2014-06-02 12:03:33

标签: java hibernate jsp spring-mvc data-binding

您好我是Spring Mvc的新手我首先有3个表分别是角色,资源,角色资源表。我创建了一个jsp,其中显示了一个资源列表以及复选框和用户输入的rolename,我的问题是我需要将rolename插入角色表并生成roleid到具有所选资源id的roleresource表。这里的问题是我无法绑定选中的复选框值这里是我的控制器

  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;
}
    }

这是我的role.java文件

    @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;
}

    }

这是我的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;
}

    }

这是我的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;
}




}

和我的jsp页面

 <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> 

有人可以帮助我如何接收数据并将其保存在两个表中

1 个答案:

答案 0 :(得分:1)

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

从您的代码中,我理解的是,RoleResourcesForm更像是2个实体Resource&amp;的包装器。 Role与另一个对象resources。 我认为,要使用form:checkboxes,您最好在List中为其指定一个对象path

这个变量resources是什么? 如果它确实是RoleResourcesForm包装器中的List对象,那么在项目中,您应该使用

items="${roleResourceForm.resources}"

提交时,它会发送表单模型属性,只显示选中的复选框值。