首页 > 解决方案 > 使用 actionListener 时更新 bean 属性

问题描述

我有一个表单中的对话框。该对话框有一个 inputTextarea 和一个 commandButton。commandButton 使用 actionListener 调用 bean 上的方法。我的问题是 inputTextarea 中的数据对我的 actionListener 的方法不可用。下面显示的注释字段在 bean 上为空。如何在我的 bean 方法中访问它的内容?

这页纸:

<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
            xmlns:f="http://java.sun.com/jsf/core" xmlns:c="http://java.sun.com/jsp/jstl/core"
            xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui"
            template="/common/template.xhtml">
<ui:define name="title">test</ui:define>
<ui:define name="head">
    <h:outputStylesheet name="web0020.css" library="css"/>
</ui:define>
<ui:define name="content">
    <p:panel id="fileUploads" header="File Uploads" style="margin-bottom:20px">
        <h:form id="form">
            <p:messages id="messages" showDetail="false" closable="true">
                <p:autoUpdate/>
            </p:messages>
            <p:dialog header="Approve" widgetVar="approveDlg" modal="true" appendTo="@(body)">
                <p:panelGrid columns="1" layout="grid" styleClass="ui-noborder">
                    <h:outputText value="Approve Submission" style="font-weight:bold;font-size:1.3em"/>
                    <p:outputLabel for="comments" value="Comments:" style="font-weight:bold"/>
                    <p:inputTextarea id="comments" value="#{testView.comments}"
                                     rows="1" cols="100"/>
                    <p:commandButton value="Save"
                                     actionListener="#{testView.approve()}"
                                     icon="ui-icon-check" update=":form:messages"/>
                </p:panelGrid>
            </p:dialog>
            <p:commandButton value="Approve" onclick="PF('approveDlg').show();" icon="fa fa-thumbs-up"
                             update=":form:messages"/>
        </h:form>
    </p:panel>
</ui:define>
</ui:composition>
@Named
@ViewScoped
public class TestView implements Serializable{
    @SuppressWarnings("compatibility:1287963775427900593")
    private static final long serialVersionUID = 1L;
    
    public TestView() {
        super();
    }
    private String comments;
    
    public void approve() {
        try {
        System.out.println("Comment:" + comments); //THIS IS EMPTY

        } catch (Exception e) {
            FacesContext.getCurrentInstance()
                .addMessage(null,
                            new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error! " + e.getMessage(), e.getMessage()));

        }
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public String getComments() {
        return comments;
    }
}

标签: jsfprimefaces

解决方案


对此进行了回答- modal=true 的 Primefaces 对话框无法正常工作。为了制作一个模态表单,你必须使用 appendTo="@(body)" 如果你使用 appendTo,你将在页面的表单之外,所以你必须嵌入一个单独的表单(在主页的表单之外)在你的对话框中


推荐阅读