Java 使用Spring测试列表是否为空
我需要测试列表是否为空(表示表为空),以便为每个条件显示不同的panelGroup。我不是Java方面的专家,以下是我正在做的: ManagedBean:Java 使用Spring测试列表是否为空,java,collections,Java,Collections,我需要测试列表是否为空(表示表为空),以便为每个条件显示不同的panelGroup。我不是Java方面的专家,以下是我正在做的: ManagedBean: public List<Platform> getPlatformList() { if(platformList == null){ platformList = new ArrayList<Platform>(); platformList.addAl
public List<Platform> getPlatformList() {
if(platformList == null){
platformList = new ArrayList<Platform>();
platformList.addAll(getPlatformService().getPlatform());
}
return platformList;
}
public void setPlatformList(List<Platform> platformList) {
this.platformList = platformList;
}
public int getFirstedit() {
if(platformList == null){return 1;
// Display add form
}
else {return 0;
// Display update form
}
}
public void setFirstedit(int firstedit) {this.firstedit = firstedit; }
public List getPlatformList(){
if(platformList==null){
platformList=新的ArrayList();
platformList.addAll(getPlatformService().getPlatform());
}
返回平台列表;
}
公共作废设置平台列表(列表平台列表){
this.platformList=平台列表;
}
public int getFirstedit(){
如果(platformList==null){返回1;
//显示添加表单
}
else{返回0;
//显示更新表单
}
}
public void setFirstedit(int firstedit){this.firstedit=firstedit;}
道:
公共平台getPlatformById(int-id){
List List=getSessionFactory().getCurrentSession().createQuery(“从平台,其中Id=?”).setParameter(0,Id).List();
if(list.size()==0)
返回null;
其他的
返回(平台)列表。获取(0);
}
公共列表getPlatform(){
List List=getSessionFactory().getCurrentSession().createQuery(“来自平台”).List();
退货清单;
}
但在index.xhtml中,它总是显示条件1(如列表为空),而在第一次时,这是正确的,我保存了一个新条目,并验证了它在数据库中的存在,但这仍然显示1作为第一次编辑的结果。如果
getPlatformList()
中为空,则实例化platformList
,因此,在getFirstEdit
中,空测试永远不会为真:
if(platformList == null){
platformList = new ArrayList<Platform>();
我删除了
Spring
标签,因为您的问题不是针对它的。
if(platformList == null){
platformList = new ArrayList<Platform>();
public int getFirstedit() {
if(getPlatformList().isEmpty()){ // change here
return 1;
// Display add form
}
else {
return 0;
// Display update form
}
}