【问题标题】:how to databind list selected checkbox in spring mvc如何在spring mvc中绑定列表选中的复选框
【发布时间】:2014-07-22 13:05:48
【问题描述】:

嗨,我是 Spring Mvc 的新手,我有 3 个表,首先是角色、资源、角色资源表。我创建了一个 jsp,其中显示了资源列表以及复选框以及用户输入的角色名称,我的问题是我需要将角色名称插入角色表并生成角色 ID 到具有选定资源 ID 的角色资源表。这里的问题是我无法绑定选定的复选框值这是我的控制器

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

    }

这是我的资源.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> 

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

【问题讨论】:

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


    【解决方案1】:
    <springform:checkboxes    path="resources" items="${resources}" itemLabel="urlName" itemValue="resourceId"/>
    

    根据我的理解,根据您的代码,RoleResourcesForm 更像是两个实体 Resource 和 Role 与另一个对象 resources 的包装。 我认为,要使用form:checkboxes,你最好在path 中给它一个对象List。

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

    items="${roleResourceForm.resources}"
    

    当你提交它时,它会发送带有仅选中复选框值的表单模型属性。

    【讨论】:

    • 嗨 ROROROOROROR 资源是一个列表,其中包含资源实体的 id 和 urlname
    • 也请告诉我如何创建对象列表
    • 你的意思是把id和urlname放在同一个类里,还有这个类的列表?
    • @Vikas 我不认为表单标签可以将您的输入转换为自定义定义的对象,您可以使用提交的 urlname 来获取对应的 ID
    猜你喜欢
    • 2016-05-27
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    相关资源
    最近更新 更多