问题描述
我有一个的facelet TAGFILE并需要根据是否指定与否的属性来呈现不同的组件。我试了一下,如下,
I have a Facelet tagfile and need to render different components depending on whether the attribute is specified or not. I tried it as below,
<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:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:pe="http://primefaces.org/ui/extensions"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:panelGrid columns="1">
<p:outputLabel value="test1" rendered="#{empty myParam}" />
<p:outputLabel value="test2" rendered="#{not empty myParam}" />
</h:panelGrid>
</ui:composition>
哪个用作下面,
<mt:myTag myParam="#{myManagedBean.someProperty}" />
然而,它没有工作。它采用#{} myManagedBean.someProperty的评估值
。如果它是空的,那么它仍然显示测试1
。我如何检查 myParam
属性实际上是被设置或不?
However, it didn't work. It takes the evaluated value of #{myManagedBean.someProperty}
. If it's empty, then it still shows test1
. How can I check if the myParam
attribute is actually being set or not?
推荐答案
有taghandler类检查当前的facelet上下文的变量映射某个属性的presence,并设置一个布尔再创建一个自定义标签在范围内的facelet指示所需属性的presence。最后使你的TAGFILE使用它。
Create another custom tag with a taghandler class which checks the presence of a certain attribute in the variable mapper of the current Facelet context, and sets a boolean in the Facelet scope indicating the presence of the desired attribute. Finally make use of it in your tagfile.
例如
<my:checkAttributePresent name="myParam" var="myParamPresent" />
<h:panelGrid columns="1">
<p:outputLabel value="test1" rendered="#{not myParamPresent}" />
<p:outputLabel value="test2" rendered="#{myParamPresent}" />
</h:panelGrid>
通过这个标记处理程序:
With this tag handler:
public class CheckAttributePresentHandler extends TagHandler {
private String name;
private String var;
public CheckAttributePresentHandler(TagConfig config) {
super(config);
name = getRequiredAttribute("name").getValue();
var = getRequiredAttribute("var").getValue();
}
@Override
public void apply(FaceletContext context, UIComponent parent) throws IOException {
context.setAttribute(var, context.getVariableMapper().resolveVariable(name) != null);
}
}
这是将在下面注册
您 .taglib.xml
:
<tag>
<tag-name>checkAttributePresent</tag-name>
<handler-class>com.example.CheckAttributePresentHandler</handler-class>
<attribute>
<name>name</name>
<required>true</required>
<type>java.lang.String</type>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<type>java.lang.String</type>
</attribute>
</tag>
这篇关于条件呈现TAGFILE取决于是否指定该属性或不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!