保存时PrimeFaces对话框中的空属性

时间:2014-12-10 16:43:30

标签: jsf primefaces

问题在于两个文本字段发送空字符串而不是键入的文本。他们在对话中。托管bean是视图范围。此命令按钮打开对话框:

<p:commandButton id="btn-open_dialog-name_query" 
                 oncomplete="PF('dialog_name_query').show();"
                 style="width: 209px; text-align: center;"
                 value="Save Query" />

对话框是:

<p:dialog id="dlg_name_query" style="width:500px;" widgetVar="dialog_name_query" header="Save Query" hideEffect="fade" position="center center" resizable="false" showEffect="fade">
    <p:panelGrid>
        <p:row>
            <p:column>
                <p:outputLabel for="nameQuery" value="Name" />
            </p:column>
            <p:column>
                <p:inputText id="nameQuery" value="#{dashboardDetailedMB.query.name}" />
            </p:column>
        </p:row>
        <p:row>
            <p:column>
                <p:outputLabel for="descriptionQuery" value="Description" />
            </p:column>
            <p:column>
                <p:inputText id="descriptionQuery" value="#{dashboardDetailedMB.query.description}" />
            </p:column>
        </p:row>
        <p:row>
            <p:column>
                <p:commandButton id="btn-save_query-client" value="Save to current client">
                    <p:ajax event="click" listener="#{dashboardDetailedMB.saveQueryClient}" update="msg" />
                </p:commandButton>
            </p:column>
        </p:row>

        <p:row>
            <p:column>
                <p:commandButton id="btn-save_query-user" value="Save to current user">
                    <p:ajax event="click" listener="#{dashboardDetailedMB.saveQueryUser}" update="msg" />
                </p:commandButton>
            </p:column>
        </p:row>
    </p:panelGrid>
</p:dialog>

方法saveQueryClient

public void saveQueryClient() {
    try {
        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();
        HttpServletRequest request = (HttpServletRequest) ec.getRequest();
        HttpSession session = (HttpSession) request.getSession(false);
        String querySql = (String) session.getAttribute("querySql");
        query.setQuery(querySql);
        queryBean.add(query);
        queryBean.insertClientHasQuery(getClientId(), queryBean.getLastRow().getId());
        jsfUtils.messages("INFO", "Query inserted!", "");
    } catch (Exception ex) {
        ex.printStackTrace(System.out);
        jsfUtils.messages("ERROR", "Error when saving query to client!", "");
    }
}

1 个答案:

答案 0 :(得分:0)

p:commandButton内置了ajax功能,因此无需使用p:ajax。我相信您的问题是p:ajax默认为process="@this"(在这种情况下仅表示commandButton),因此p:inputText将不会被提交。但p:commandButton默认为process="@form",这就是您所需要的:

<p:commandButton id="btn-save_query-client" 
             value="Save to current client" 
             action="#{dashboardDetailedMB.saveQueryClient}" 
             update="msg" />

此外,您的表单似乎在您的对话框周围,我发现将表单放在对话框中并将对话框放在主窗体下面的页面中是最安全的,但是如果它工作,则无需更改它。