gpt4 book ai didi

java - Vaadin 表 - AddNestedContainerProperty

转载 作者:太空宇宙 更新时间:2023-11-04 09:31:08 25 4
gpt4 key购买 nike

public class LocationBasedRole extends AbstractEntity{
@ManyToMany(fetch=FetchType.LAZY)
private Set<Role> roles=new HashSet<Role>();

@ManyToMany(fetch=FetchType.LAZY)
private Set<Location> locations=new HashSet<Location>();
}


public class Role extends AbstractEntity{
private String name;
}


public class Location extends AbstractEntity{
private String location;
}

我有一个名为 locationBasedRole 的实体,它有 2 个名为 Roles 和 Locations 的属性。角色和位置都与 locationBasedRole 具有 @ManyToMany 关系。
现在我想在 Vaadin 表中拥有每个属性的一个属性。
应该是这样的,

public class UserForm extends OgsAbstractForm<User>{

MTable<LocationBasedRole> locationBasedRoleTable = new MTable<LocationBasedRole>().withHeight("100%").withWidth("100%");


@Override
protected Component createContent() {

Set<LocationBasedRole> lbRoles=new HashSet<LocationBasedRole>();
roles.addAll(locationBasedRoleFasade.findAll());

BeanItemContainer<LocationBasedRole> bean=new BeanItemContainer<LocationBasedRole>(LocationBasedRole.class);

//It returns an error on the next both lines and I know the reason, but don't know how to solve it.
// If it was no ManyToMany relation and the properties weren't a collection, it would work
bean.addNestedContainerProperty("roles.name");
bean.addNestedContainerProperty("locations.location");

bean.removeContainerProperty("persistent");
bean.removeContainerProperty("id");

bean.addAll(lbRoles);

locationBasedRoleTable.setContainerDataSource(bean);

return new VerticalLayout(locationBasedRoleTable);
}
}

当我从 NestedContainerProperties 中删除属性时,它至少会在表中显示一些内容。

bean.addNestedContainerProperty("roles");
bean.addNestedContainerProperty("locations");

我需要任何帮助!

提前致谢!

最佳答案

所以,如果我理解你的问题是正确的,你希望将 BeanItemContainer-Entity 的集合显示在每一列中吗?

我认为有两种可能性。

选项 1 - 对 Set 使用包装类并使用 addNestedContainerBean

一种可能是不在 LocationBasedRole 中使用 Set,而是使用扩展 HashSet 的包装类。然后您可以使用 addNestedContainerBean 方法。

我与 BeanItemContainer-Entity 团队创建了一个小示例

public class Team {

private String teamName;
private Members teamMembers;

public String getTeamName() {
return teamName;
}

public void setTeamName(String teamName) {
this.teamName = teamName;
}

public Members getTeamMembers() {
return teamMembers;
}

public void setTeamMembers(Members teamMembers) {
this.teamMembers = teamMembers;
}
}

由姓名和团队成员组成。后者的类型为Members:

public class Members extends HashSet<TeamMember> {

public String getMembers() {
return this.stream()
.map(member -> member.getFirstName() + " " + member.getLastName())
.collect(Collectors.joining(","));
}

}

这是 Set 的简单包装器,其中包含 TeamMember 的实例:

public class TeamMember {

private String firstName;
private String lastName;
private Integer age;

// getters and setters
}

正如您在 Members 类中看到的,有一个方法 getMembers 返回一个字符串,其中包含以逗号分隔的团队成员姓名列表。

如果我们现在使用 addNestedContainerBean("teamMembers") Vaadin 会尝试显示 Members 类中包含的所有属性。 Vaadin 会认为 getMembers 是名为 Members 的 String 属性的 getter,因此会为其生成一列。

Vaadin 还会显示“空”列,因为它会找到 SetisEmpty 方法,并认为 empty 是要在列中显示的属性。因此我们告诉 Vaadin 删除该列。

我的示例的最终代码如下所示:

protected Component createContent() {

Set<Team> teams=new HashSet<>();
for (int teamCounter = 0; teamCounter < 5; teamCounter++) {
Team team = createTeam();
addMembersToTeam(5, team);
teams.add(team);
}

BeanItemContainer<Team> bean=new BeanItemContainer<>(Team.class);

bean.addNestedContainerBean("teamMembers");
bean.removeContainerProperty("teamMembers.empty");

bean.addAll(teams);

teamTable.setContainerDataSource(bean);

return new VerticalLayout(teamTable);
}

结果如下:

Option 1

选项 2 - 创建假 getter 并使用 addNestedContainerProperty

为此,您唯一要做的就是扩展您的 BeanItemContainer-Entity (LocationBasedRole) 并为您想要在列中显示的每个 Set 创建一个假 getter。在您的示例中,这两个假 getter 可能是 public String getTheRoles()public String getTheLocations()。然后您可以使用 bean.addNestedContainerProperty("theRoles")bean.addNestedContainerProperty("theLocations")

在我的示例中,我的 TeamMember 类(与您的 Role/Location 类相对应)仍然类似于上面的选项:

 public class TeamMember {

private String firstName;
private String lastName;
private Integer age;

// getters and setters
}

我的Team类(您的LocationBasedRole)看起来像:

public class Team {

private String teamName;
private Set<TeamMember> teamMembers;

public String getTeamName() {
return teamName;
}

public void setTeamName(String teamName) {
this.teamName = teamName;
}

public Set<TeamMember> getTeamMembers() {
return teamMembers;
}

public void setTeamMembers(Set<TeamMember> teamMembers) {
this.teamMembers = teamMembers;
}

public String getMembers() {
if (teamMembers != null) {
return teamMembers.stream()
.map(member -> member.getFirstName() + " " + member.getLastName())
.collect(Collectors.joining(","));
} else {
return "No members";
}
}
}

现在您可以告诉 vaadin 添加(不存在的)属性“members”,Vaadin 将找到 getter getMembers 并使用它来生成列。我们还必须告诉 vaadin 不要显示原始的“teamMembers”属性。所以最终的代码是:

protected Component createContent() {

Set<Team> teams=new HashSet<>();
for (int teamCounter = 0; teamCounter < 5; teamCounter++) {
Team team = createTeam();
addMembersToTeam(5, team);
teams.add(team);
}

BeanItemContainer<Team> bean=new BeanItemContainer<>(Team.class);

bean.addNestedContainerProperty("members");
bean.removeContainerProperty("teamMembers");

bean.addAll(teams);

teamTable.setContainerDataSource(bean);

return new VerticalLayout(teamTable);
}

结果如下:

option 2

关于java - Vaadin 表 - AddNestedContainerProperty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57089332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com