【问题标题】:Bean method on rowEdit event of p:dataTable is not invokedp:dataTable 的 rowEdit 事件上的 Bean 方法未被调用
【发布时间】:2015-03-19 12:51:15
【问题描述】:

我正在尝试从 <p:dataTable> n 调用 bean 方法编辑行,但问题是在 rowEdit 事件上未调用 bean 方法。如果有人能给我解决方案,那将是可观的。

另外,我的 bean 已经在 View Scope 中,甚至不能在 session 范围内工作......我已经尝试了所有三个范围。

我的代码如下:

<h:form id="commentList">
    <p:dataTable id="commentTable" editable="true" paginator="true" rows="10" var="comment" value="#{commentAction.list(uID)}" class="table table-striped table-bordered table-hover commentTable" widgetVar="commentListTable">
        <p:ajax event="rowEdit" listener="#{commentAction.updateCommentStatus}"/>
        <p:column filterBy="#{comment.commentId}" footerText="" headerText="Comment Id" filterMatchMode="contains" sortBy="#{comment.commentId}">
            <h:outputText value="#{comment.commentId}" id="commentId"/>
        </p:column>
        <p:column filterBy="#{comment.selectedText}" headerText="Selected Text" sortBy="#{comment.selectedText}">
            <h:outputText value="#{comment.selectedText}" id="selectedText"/>
        </p:column>
        <p:column filterBy="#{comment.commentText}" headerText="Comment" sortBy="#{comment.commentText}">
            <h:outputText value="#{comment.commentText}" id="commentText" escape="false"/>
        </p:column>
        <p:column filterBy="" headerText="Comment From" sortBy="">
            <h:outputText value="" id="commentFrom"/>
        </p:column>
        <p:column filterBy="#{comment.insertedOn}" headerText="Date/Time" sortBy="#{comment.insertedOn}">
            <h:outputText value="#{comment.insertedOn}" id="insertedOn"/>
        </p:column>
        <p:column filterBy="#{comment.commentStatus}" headerText="Comment Status" sortBy="#{comment.commentStatus}">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{comment.commentStatus}" id="commSatus"/>
                </f:facet>
                <f:facet name="input">
                    <h:selectOneMenu value="#{commentAciton.commentStatus}" id="commentStatus" class="commentSelectBox">
                        <f:selectItem itemLabel="UpdateStatus" itemDisabled="true"/>
                        <f:selectItem itemValue="Open" itemLabel="Open"/>
                        <f:selectItem itemValue="Close" itemLabel="Close"/>
                        <f:selectItem itemValue="Cancel" itemLabel="Cancel"/>
                    </h:selectOneMenu>
                </f:facet>
            </p:cellEditor>
        </p:column>
        <p:column style="width:32px">
            <p:rowEditor />
        </p:column>
    </p:dataTable>
</h:form>

而bean方法是:

public void updateCommentStatus(RowEditEvent event) {
    try {
        logger.info("comment Iddddddddddd: " + commentId);
        logger.info("comment Statusssssss: " + this.commentStatus);
        Comment comment = (Comment) event.getObject();
        logger.info("new value: " + comment.getCommentStatus());
    } catch (Exception ex) {
        logger.info("Exception caught in updateCommentStatus in CommentAction ..." + ex.getMessage());
        ex.printStackTrace();
    }
}

【问题讨论】:

  • 无法解析 XHTML 代码。标签&lt;p:dataTable&gt; 不整洁,最后缺少&gt;。这是你的真实代码吗?在这种情况下,&lt;p:ajax&gt; 中的 listener 不相关但不会导航到所述导航案例结果 success。它基本上期望在关联的支持 bean 中有一个 void 方法。与action 不同,返回类型将被简单地忽略。
  • @Tiny : > 的标签写错了...同时从 bean 方法中删除了返回标签,函数仍然没有被调用...编辑了上面的 bean 方法...但是现在我得到了例外:
  • com.sun.faces.context.AjaxExceptionHandlerImpl handlePartialResponseError SEVERE: javax.faces.model.NoRowAvailableException 有人可以帮忙
  • bean的作用域是什么? (在这种情况下,它应该是视图范围 - @ViewScoped 或更大,如果需要)。
  • 是的,它只是查看范围

标签: jsf jsf-2 datatable roweditor


【解决方案1】:

通过将列表检索代码从 getter 方法替换为 postConstruct 注释中的 init() 来解决 @tiny 问题。 以下是更正后的代码

<p:dataTable id="commentTable" editable="true" paginator="true" rows="10" var="comment" value="#{commentAction.commentList}" class="table table-striped table-bordered table-hover commentTable" widgetVar="commentListTable">
<p:ajax event="rowEdit" listener="#{commentAction.updateCommentStatus}"/>
  ....
  ....
  ....
</p:dataTable>
@PostConstruct
    public void init(){
        logger.info("urs: "+ursId);
        CommentManager commentManager = new CommentManager();
        try {
            commentList = commentManager.list(1); //hardcoding ryt now
        } catch (Exception ex) {
            logger.info("Exception caught in init in CommentAction ..." + ex.getMessage());
            ex.printStackTrace();
        }
    }
    public void updateCommentStatus(RowEditEvent event){
        try{
            CommentManager commentManager = new CommentManager();
            Comment comment = (Comment)event.getObject();
            logger.info("*****************************************************");  
            logger.info("comment Iddddddddddd: " +comment.getCommentId());
            logger.info("comment Statusssssss: " +comment.getCommentStatus());
            commentManager.updateCommentStatus(comment);
        } catch(Exception ex){
            logger.info("Exception caught in updateCommentStatus in CommentAction ..." + ex.getMessage());
            ex.printStackTrace();
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    相关资源
    最近更新 更多