我正在使用Primefaces,我遇到的问题是没有触发setPropertyActionListener,因此没有设置视图作用域托管bean的属性。
我的观点:
<p:column>
<p:commandLink value="Supprimer" oncomplete="confirmation.show()" >
<f:setPropertyActionListener value="#{car}" target="#{typeMB.selectedType}" />
</p:commandLink>
</p:column>
managed-bean具有selectedType属性,其中既有getter又有setter。
我的托管bean:
@ManagedBean(name="typeMB")
@ViewScoped
public class TypeManagedBean implements Serializable {
private static final long serialVersionUID = 1L;
private Type newtype;
private Type selectedType;
@ManagedProperty(value="#{TypeDao}")
GenericDao<Type> typeDAO;
public TypeManagedBean(){
newtype = new Type();
}
public List<Type> getList_types() {
return typeDAO.readAll();
}
public void setTypeDAO(GenericDao<Type> typeDAO) {
this.typeDAO = typeDAO;
}
public GenericDao<Type> getTypeDAO() {
return typeDAO;
}
public Type getNewtype() {
return newtype;
}
public void setNewtype(Type newtype) {
this.newtype = newtype;
}
public Type getSelectedType() {
if(selectedType != null)
System.out.println("get : le selected type : "+selectedType.getLibelle());
return selectedType;
}
public void setSelectedType(Type selectedType) {
this.selectedType = selectedType;
System.out.println("set le selected type : "+selectedType.getLibelle());
}
}
我能做些什么来达到我的目的?
答案 0 :(得分:3)
根据<p:commandLink>
,process
部分以及Primefaces (3.5) user's guide发表的声明,@all
属性的默认值为process="@this"
,这意味着整个页面将被提交。因此,此提交可能存在一些验证错误,从而阻止调用侦听器方法。否则,它应该按照您发布的代码按预期工作。
对上述假设的一个很好的测试是放置{{1}}属性。因为'与要执行的链接相关联的操作,必须部分提交链接本身',正如BalusC在Primefaces lead on this forum中完美解释的那样,我们需要添加属性来进行测试。
要检查的另一件事是您的命令组件属于表单,并且您的视图不包含嵌套表单。
答案 1 :(得分:1)
以下代码正在运行:
Managed bean:
package app.so.dev.web.controller;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import app.so.dev.web.model.Student;
@ManagedBean(name = "so15344819")
@ViewScoped
public class SO15344819 implements Serializable {
private static final long serialVersionUID = 6686378446131077581L;
private List<Student> students;
private Student selectedStudent;
@PostConstruct
public void init() {
students = new ArrayList<Student>();
students.add(new Student("Student 1"));
students.add(new Student("Student 2"));
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public Student getSelectedStudent() {
return selectedStudent;
}
public void setSelectedStudent(Student selectedStudent) {
this.selectedStudent = selectedStudent;
}
}
和xhtml:
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui" template="/WEB-INF/templates/globalTemplate.xhtml">
<ui:define name="title">15344819</ui:define>
<ui:define name="content">
<p:growl id="growl" showDetail="true" />
<h:form id="form">
<p:dataTable id="students" value="#{so15344819.students}" var="student">
<p:column>
<p:commandButton id="selectButton" update=":form:display" oncomplete="studentDialog.show()" icon="ui-icon-search" title="View">
<f:setPropertyActionListener value="#{student}" target="#{so15344819.selectedStudent}" />
</p:commandButton>
</p:column>
</p:dataTable>
<p:dialog header="Student Detail" widgetVar="studentDialog" resizable="false" id="studentDlg"
showEffect="fade" hideEffect="explode" modal="true">
<h:panelGrid id="display" columns="2" cellpadding="4" style="margin:0 auto;">
<h:outputText value="Name:" />
<h:outputText value="#{so15344819.selectedStudent.name}" style="font-weight:bold"/>
</h:panelGrid>
</p:dialog>
</h:form>
</ui:define>
</ui:composition>
环境:
<强> Primefaces Showcase 强>