gpt4 book ai didi

Primefaces p :confirmDialog inside tabView

转载 作者:行者123 更新时间:2023-12-03 10:04:53 25 4
gpt4 key购买 nike

我现在在 tabView 中使用 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 和 no 的 confirmDialog 仍然有效。我不知道发生了什么,请帮助我,如何让 confirmDialog 在所有 tabView 中显示完整内容

最佳答案

p:confirm 没有实现状态保存,因此它在第一个 JSF 生命周期后失去了它的属性值。它还仅在 View 构建时评估 EL 一次。

如何修复状态保存。

你可以扩展PrimeFaces的ConfirmBehavior,实现saveStaterestoreState,通过behavior-重写faces-config.xml中原有的行为- ID org.primefaces.behavior.ConfirmBehavior。然后您将能够在后续请求中渲染和重新渲染 p:confirm。

如何修复状态保存和重新评估 EL 绑定(bind)属性值。

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

创建 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>

在您的标签库 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 一样使用 my:confirm,但具有状态保存和动态 EL 评估功能。

关于Primefaces p :confirmDialog inside tabView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21509363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com