【发布时间】:2012-05-28 20:12:39
【问题描述】:
我的选择列表有问题。显示源和目标有效,但是当按下命令按钮时,它会调用选择列表的设置器并清空选择列表的源和目标。之后它调用方法connectTags()。
这种行为的原因是什么,我该如何解决?
<h:form>
<p:pickList id="pickList" value="#{employeeEditController.dualListTags}"
var="tag" itemLabel="#{tag.value}" itemValue="#{tag}">
<f:facet name="sourceCaption">Available</f:facet>
<f:facet name="targetCaption">Connected</f:facet>
</p:pickList>
<p:commandButton value="Save" actionListener="#{employeeEditController.connectTag()}"/>
</h:form>
豆子:
@Named(value = "employeeEditController")
@SessionScoped
public class EmployeeEditController implements Serializable, IEditController {
private Employee selectedEmployee;
private DualListModel<Tags> dualListTags;
@Inject
private EmployeeFacade employeeFacade;
@Inject
private CompanyFacade companyFacade;
@Inject
private TagFacade tagFacade;
public void setSelectedEmployee(Employee selectedEmployee) {
System.out.println("setSelectedEmployee called. Value: " + selectedEmployee.getFirstName());
this.selectedEmployee = selectedEmployee;
this.refreshDualList();
}
private void refreshDualList(){
List<Tags> source = (List<Tags>) this.getCompanyTags();
List<Tags> target = (List<Tags>) this.getTags();
System.out.println("refreshDualList called. \nSource: " + source.size() +
"Target: " + target.size());
this.dualListTags = new DualListModel<Tags>(source, target);
}
public Collection<Tags> getTags(){
Collection<Tags> retVal;
if(this.selectedEmployee != null){
retVal = this.selectedEmployee.getTags();
if(retVal == null){
retVal = new ArrayList<Tags>();
}
} else{
System.out.println("selected emp is null");
retVal = new ArrayList<Tags>();
}
return retVal;
}
public Collection<Tags> getCompanyTags() {
Collection<Tags> retVal = new ArrayList<Tags>();
if(this.companyID == null){
String userstr = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
this.companyID = this.companyFacade.getCompanyIDByUser(userstr);
}
retVal = this.tagFacade.getTagsFromCompanyID(companyID);
return retVal;
}
public void save(){
if(this.selectedEmployee != null){
System.out.println("Save called. Value: " + this.selectedEmployee.getFirstName());
this.employeeFacade.edit(this.selectedEmployee);
}
}
public void connectTag() {
if(this.dualListTags != null){
System.out.println("connectTag() called.\n" + "source: " + this.dualListTags.getSource().size() +
"\ntarget: " + this.dualListTags.getTarget().size());
this.selectedEmployee.setTags((Collection<Tags>)this.dualListTags.getTarget());
this.save();
}
}
public DualListModel<Tags> getDualListTags() {
System.out.println("getDualList called. \nSource :" + this.dualListTags.getSource().size()
+ "\nTargets: " + this.dualListTags.getTarget().size());
return this.dualListTags;
}
public void setDualListTags(DualListModel<Tags> dualListTags) {
System.out.println("setDualList called. \nSource :" + this.dualListTags.getSource().size()
+ "\nTargets: " + this.dualListTags.getTarget().size());
this.dualListTags = dualListTags;
System.out.println("setDualList called after set. \nSource :" + this.dualListTags.getSource().size()
+ "\nTargets: " + this.dualListTags.getTarget().size());
}
}
【问题讨论】:
-
展示示例对您有用吗? primefaces.org/showcase/ui/picklist.jsf 一步一步地构建,直到它以同样的方式失败并且你找到了原因。
-
connectTags 方法从保存在控制器中的对偶列表中获取源和目标。但是 setter 被调用并清空了对偶列表 -
connectTags()返回什么 - 是返回 JavaScript 以在浏览器中运行吗? -
如果你真的想解决这个问题,发sscce。
-
如果您根据此处的 cmets 修改了代码,则必须将更改应用到您的帖子中,否则会产生误导或不相关。此外,一个好主意是清理您的代码,以便出现有问题的区域。如果您不确定其他代码(您应该在此处省略的代码)是否会导致问题,那么请在提交问题之前制作一个示例并对其进行测试。
标签: jsf primefaces