Primefaces p:在tabView中确认对话框

时间:2014-02-02 10:12:25

标签: primefaces dialog confirm tabview

我现在在tabView中使用confirmDialog时遇到问题。 这是我的confirmDialog

<p:confirmDialog global="true" showEffect="fade" hideEffect="explode">
    <h:form>
        <p:commandButton value="Yes" type="button"
            styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
        <p:commandButton value="No" type="button"
            styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
    </h:form>
</p:confirmDialog>

在第一个tabView中,我有一个确认按钮

<p:commandButton value="Import" icon="ui-icon-arrowrefresh-1-w"
    update=":form:growl">
    <p:confirm header="Confirmation" message="Are you sure?" />
</p:commandButton>

在第二个tabView中,我只有那个按钮。

现在我的问题是:在第一个标签中,我的confirmDialog有我想要的全文:标题和消息,但是在第二个标签中,标题和消息都变为“空”。只有按钮yes和confirmDialog仍然有效。我不知道发生了什么,请帮帮我,如何在所有tabView中显示confirmDialog显示全部内容

3 个答案:

答案 0 :(得分:6)

p:确认没有实现状态保存,因此它失去了它的属性&#39;第一个JSF生命周期之后的值。它还在视图构建时仅对EL进行一次评估。

如何修复状态保存。

您可以扩展PrimeFaces&#39; ConfirmBehavior,实现saveStaterestoreState,并通过behavior-id org.primefaces.behavior.ConfirmBehavior覆盖faces-config.xml中的原始行为。然后,您将能够呈现并重新呈现p:确认后续请求。

如何修复状态保存并重新评估EL绑定属性值。

您应该创建自己的my:confirm,因为您需要一个自定义标记处理程序,并且您无法以非丑陋的方式替换另一个taglibrary标记的标记处理程序。

创建ConfirmBehavior。

package my;

import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.component.behavior.ClientBehaviorContext;
import javax.faces.context.FacesContext;

import org.primefaces.behavior.base.AbstractBehavior;
import org.primefaces.component.api.Confirmable;
import org.primefaces.json.JSONObject;

public class ConfirmBehavior extends AbstractBehavior {

    public final static String BEHAVIOR_ID = "my.ConfirmBehavior";

    @Override
    public String getScript(ClientBehaviorContext behaviorContext) {
        FacesContext context = behaviorContext.getFacesContext();
        UIComponent component = behaviorContext.getComponent();
        String source = component.getClientId(context);
        String headerText = JSONObject.quote(this.getHeader());
        String messageText = JSONObject.quote(this.getMessage());

        if (component instanceof Confirmable) {
            String script = "PrimeFaces.confirm({source:\"" + source + "\",header:" + headerText + ",message:"
                    + messageText + ",icon:\"" + getIcon() + "\"});return false;";
            ((Confirmable) component).setConfirmationScript(script);

            return null;
        } else {
            throw new FacesException("Component " + source + " is not a Confirmable. ConfirmBehavior can only be "
                    + "attached to components that implement org.primefaces.component.api.Confirmable interface");
        }
    }

    public String getHeader() {
        return eval(PropertyKeys.header, null);
    }

    public void setHeader(String header) {
        setLiteral(PropertyKeys.header, header);
    }

    public String getMessage() {
        return eval(PropertyKeys.message, null);
    }

    public void setMessage(String message) {
        setLiteral(PropertyKeys.message, message);
    }

    public String getIcon() {
        return eval(PropertyKeys.icon, null);
    }

    public void setIcon(String icon) {
        setLiteral(PropertyKeys.icon, icon);
    }

    public enum PropertyKeys {
        header(String.class), message(String.class), icon(String.class);

        final Class<?> expectedType;

        PropertyKeys(Class<?> expectedType) {
            this.expectedType = expectedType;
        }
    }

    @Override
    protected Enum<?>[] getAllProperties() {
        return PropertyKeys.values();
    }

}

创建ConfirmBehaviorHandler。

package my;

import javax.faces.application.Application;
import javax.faces.view.facelets.BehaviorConfig;
import javax.faces.view.facelets.FaceletContext;
import javax.faces.view.facelets.TagAttribute;

import org.primefaces.behavior.base.AbstractBehaviorHandler;

public class ConfirmBehaviorHandler extends AbstractBehaviorHandler<ConfirmBehavior> {

    private final TagAttribute header;
    private final TagAttribute message;
    private final TagAttribute icon;

    public ConfirmBehaviorHandler(BehaviorConfig config) {
        super(config);
        this.header = this.getAttribute(ConfirmBehavior.PropertyKeys.header.name());
        this.message = this.getAttribute(ConfirmBehavior.PropertyKeys.message.name());
        this.icon = this.getAttribute(ConfirmBehavior.PropertyKeys.icon.name());
    }

    @Override
    protected ConfirmBehavior createBehavior(FaceletContext ctx, String eventName) {
        Application application = ctx.getFacesContext().getApplication();
        ConfirmBehavior behavior = (ConfirmBehavior) application.createBehavior(ConfirmBehavior.BEHAVIOR_ID);

        setBehaviorAttribute(ctx, behavior, this.header, ConfirmBehavior.PropertyKeys.header.expectedType);
        setBehaviorAttribute(ctx, behavior, this.message, ConfirmBehavior.PropertyKeys.message.expectedType);
        setBehaviorAttribute(ctx, behavior, this.icon, ConfirmBehavior.PropertyKeys.icon.expectedType);

        return behavior;
    }

}

在faces-config.xml中注册ConfirmBehavior。

<?xml version="1.0" encoding="utf-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">

    <behavior>
        <behavior-id>my.ConfirmBehavior</behavior-id>
        <behavior-class>my.ConfirmBehavior</behavior-class>
    </behavior>

</faces-config>

在taglibrary my.taglib.xml中注册ConfirmBehaviorHandler。

<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facelettaglibrary_2_2.xsd"
    version="2.2">

    <namespace>http://mycompany.ru/my</namespace>

    <tag>
        <tag-name>confirm</tag-name>
        <behavior>
            <behavior-id>my.ConfirmBehavior</behavior-id>
            <handler-class>my.ConfirmBehaviorHandler</handler-class>
        </behavior>
    </tag>

</facelet-taglib>

现在你可以使用我的:确认,就像你使用p:confirm一样,但是使用状态保存和动态EL评估。

答案 1 :(得分:0)

它对我来说似乎没问题。见下面的例子

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <title>JSF User Dialog</title>
</h:head>
<h:body>
    <h3>This is a JSF view.</h3>
    <h:form id="form">
        <p:confirmDialog global="true" showEffect="fade" hideEffect="explode">
            <h:form>
                <p:commandButton value="Yes" type="button"
                    styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
                <p:commandButton value="No" type="button"
                    styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
            </h:form>
        </p:confirmDialog>

        <p:tabView id="tabView">

            <p:tab id="tab1" title="Tab one ">
                <p:commandButton value="Import" icon="ui-icon-arrowrefresh-1-w"
                    update=":form">
                    <p:confirm header="Confirmation" message="Are you sure?" />
                </p:commandButton>
            </p:tab>

            <p:tab id="tab2" title="Tab two">
                <p:commandButton value="Import" icon="ui-icon-arrowrefresh-1-w"
                    update=":form">
                    <p:confirm header="Confirmation" message="Are you sure?" />
                </p:commandButton>
            </p:tab>


        </p:tabView>
    </h:form>
</h:body>
</html>

<强>输出:

Tab 1 with dialog box Tab 2 with dialog box

如果你发布完整的xhtml代码会很好,所以我们可以看到那里可能出现的问题。

答案 2 :(得分:0)

我遇到了与组件相同的问题。建议的删除动态true的解决方案,但是当我们必须在对话框中工作时不能满足,因为数据不再自动更新,导致字段为空。 如果发生这种情况,您必须执行以下操作。 例:

       <p:commandButton  style="font-size: 12px;width:30px;height:20px;"  icon="ui-icon-trash">
            <p:confirm header="Confirmação" message="Você realmente quer excluir o item de despacho?" icon="ui-icon-alert" />
       </p:commandButton>
       <p:growl id="growl" showDetail="true" />
       <p:confirmDialog global="true" showEffect="fade" hideEffect="explode" style="font-size: 12px;" closeOnEscape="true" widgetVar="confir">
            <p:commandButton id="confirm" value="Sim" ajax="true"  type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check"  style="font-size: 12px;">
                <p:ajax event="click" listener="#{tableExpedicao.excluiItem}" update="confirm" oncomplete="confir.hide()"> </p:ajax>
            </p:commandButton>
            <p:commandButton value="Não" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" style="font-size: 12px;" oncomplete="confir.hide()"/>
     </p:confirmDialog>