JSF无法在托管bean中设置当前变量实体,以了解用户选择了哪个实体

JSF无法在托管bean中设置当前变量实体,以了解用户选择了哪个实体,jsf,jakarta-ee,primefaces,Jsf,Jakarta Ee,Primefaces,我试图使用Primefaces在JSF中实现对实体的修改 我的主视图列出了以下用户: <p:growl id="growlEditUnit" showDetail="true" life="12000" /> <p:dialog id="dialogEditUnit" header="Edit Unit" widgetVar="editUnitDialog" showEffect="fade" hideEffect="fade" resizable="false" &g

我试图使用Primefaces在JSF中实现对实体的修改

我的主视图列出了以下用户:

<p:growl id="growlEditUnit" showDetail="true" life="12000" />
    <p:dialog id="dialogEditUnit" header="Edit Unit" widgetVar="editUnitDialog" showEffect="fade" hideEffect="fade" resizable="false" >
        <ui:include src="editUnit.xhtml" />
    </p:dialog>

<h:form id="form2">  

        <p:dataTable id="units" var="unit" value="#{unitController.unitsOfLoggedInUser}" >  

            <f:facet name="header">  
                Click Edit or Delete after selecting a unit to modify or remove it  
            </f:facet>  

            <p:column headerText="Code">  
                #{unit.unitCode}  
            </p:column>  

            <p:column headerText="Name">  
                #{unit.unitName}  
            </p:column>  

            <p:column headerText="Semester" >  
                #{unit.semester}  
            </p:column>  

            <p:column headerText="Academic Year">  
                #{unit.academicYear}  
            </p:column>

            <p:column headerText="Twitter Username">  
                #{unit.twitterUsername}  
            </p:column>

            <p:column headerText="Actions">  
                <p:commandButton id="editButton" value="Edit" action="#{unitController.setCurrent(unit)}" update=":dialogEditUnit" oncomplete"editUnitDialog.show()" />  
            </p:column>  

        </p:dataTable> 


    </h:form>

选择一个单元后,单击“编辑”或“删除”以修改或删除该单元
#{unit.unitCode}
#{unit.unitName}
#{单位.学期}
#{单位:学年}
#{unit.twitterUsername}
此视图正确列出了所有数据。但是,当我按下current时,我的目标是根据单击的按钮设置托管bean的当前属性(下面列出的代码)。在此之后,我尝试更新“编辑”对话框,使其充满该单位的值,然后使用oncomplete属性使其可见。但是,单击编辑按钮时,似乎从未调用托管方法setCurrent(unit)。随后,对话框显示为空。有人能帮我解决我做错了什么吗? 我也在发布托管bean代码

@ManagedBean(name = "unitController")
@ViewScoped
public class UnitController implements Serializable {

private Unit current;

private List<Unit> unitsOfLoggedInUser;

@ManagedProperty(value="#{loginController.checkedUser}")
private Lecturer lecturer;

@EJB
private web.effectinet.ejb.UnitFacade ejbFacade;
@EJB
private web.effectinet.ejb.LecturerFacade lecturerFacade;

public UnitController() {
}

@PostConstruct
public void init(){
    if (lecturer.getLecturerId() == null)
        unitsOfLoggedInUser = null;
    else
        unitsOfLoggedInUser = (List<Unit>) lecturer.getUnitCollection();
}

public List<Unit> getUnitsOfLoggedInUser() {

        return unitsOfLoggedInUser;

}

public void setCurrent(Unit current) {
    this.current = current;
}

public Lecturer getLecturer() {
    return lecturer;
}

public void setLecturer(Lecturer lecturer) {
    this.lecturer = lecturer;
}
@ManagedBean(name=“unitController”)
@视域
公共类UnitController实现可序列化{
私人单位电流;
私有列表单元日志用户;
@ManagedProperty(value=“#{loginController.checkedUser}”)
私人讲师;
@EJB
私有web.effectinet.ejb.UnitFacade ejbFacade;
@EJB
私有web.effectinet.ejb.讲师学院讲师学院;
公共单位控制员(){
}
@施工后
公共void init(){
if(讲师.get讲师ID()==null)
unitsOfLoggedInUser=null;
其他的
unitsOfLoggedInUser=(列表)讲师。getUnitCollection();
}
公共列表getUnitsOfLoggedInUser(){
返回loggedinuser单元;
}
公共无效设置电流(单位电流){
这个电流=电流;
}
公共讲师{
返回讲师;
}
公共讲师(讲师){
这个讲师=讲师;
}

呈现commandButton的action属性时,不包含有关
单位
变量值的信息

要将单元传递给托管bean的action方法,则需要在commandButton的
子标记中传递
单元的ID

<p:commandButton action="#{managedBean.actionMethod}" ........>
   <f:param name="unitid" value="#{unit.id}" /> 
</p:commandButton>


从操作方法中,您可以通过名称从
ExternalContext
获取请求参数,这将为您提供在数据表中按commandButton所需的单元ID。

您确定吗?因为我在操作方法
editUnitController.setCurrent(单元)中传递整个单元对象
.unit对象是dataTable元素的一部分,它也定义了该单元所属的按钮。@maple_shaftIt应该可以正常工作。另请参见此答案的第3部分:@BalusC,但我对第3点所说的做了相同的操作,因此通过在支持bean中有一个接受该类对象的方法来传递整个对象。Ho无论如何,该方法没有在后台调用bean@BalusC这只有在使用Servlet 3.0和EL 2.2时才有效,但你是正确的。我不喜欢做假设。@interboy:是的,我知道。请阅读关于你问题的评论。请排除第2点,直到这里有5点:@BalusC从你在下面的答案中所说的,这似乎是一个错误没有正确调用action属性的问题?从上面链接中的所有点来看,似乎只有4个属性适用于我。但是,我尝试按照您的建议解决它(在post构造中设置业务规则并使其成为视图范围),但操作属性仍然没有调用。由于我编辑了该问题,您能看一下新代码吗?还有其他建议吗?谢谢您,您真的确定从未调用过
setCurrent()
方法吗?您调试过吗?设置调试断点或穷人的
System.out.println()
line。另一个可能的原因可能是JavaScript错误。检查浏览器的内置JavaScript控制台。在Chrome/IE9/Firefox+Firebug中按F12。@BalusC当你告诉我检查Java脚本控制台时,我已经返回到SessionBean的第一个版本。但是,我注意到一个非常奇怪的行为。如果Firebug是打开的(在Chrome和Firefox中),我的意思是刚刚打开脚本选项卡,就会调用setCurrent方法。如果关闭,则不会发生任何事情,也不会调用该方法。这可能是什么原因?好的,那么这是一个JS问题。JS控制台中是否有错误?您使用的是什么PF版本?