Jsf 使用actionListener时更新bean属性

Jsf 使用actionListener时更新bean属性,jsf,primefaces,Jsf,Primefaces,我有一个窗体中的对话框。该对话框有一个InputExtArea和一个commandButton。commandButton使用actionListener调用bean上的方法。我的问题是InputExtArea中的数据对actionListener的方法不可用。下面显示的注释字段在bean上为空。如何在bean的方法中访问它的内容 页面: <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="

我有一个窗体中的对话框。该对话框有一个InputExtArea和一个commandButton。commandButton使用actionListener调用bean上的方法。我的问题是InputExtArea中的数据对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>

回答这个-。为了制作一个模式表单,你必须使用appendTo=“@(body)”,如果你使用appendTo,你将在页面的表单之外,因此你必须在你的对话框中嵌入一个单独的表单(在主页的表单之外)

Hmm它提交整个表单,因此InputExtArea内容应该在那里。如果将h:窗体从对话框外部移动到对话框内部会发生什么情况?我将窗体移动到对话框内部,它成功了!但不确定如何继续,因为我需要引用对话框外的其他项目,如消息。为什么会这样?另一个更新,如果我删除对话框上的appendTo=“@(body)”,它也会起作用。我不得不加上这一点,因为当对话框打开时,它使整个页面成为模态,包括对话框。这是一个基本的东西。我必须附加到body中才能创建一个对话框模态,并且它必须在对话框中有自己的形式才能工作。
@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;
    }
}