【问题标题】:How to make a grid of JSF composite component?如何制作 JSF 复合组件的网格?
【发布时间】:2016-04-18 12:42:40
【问题描述】:

我在 panelGrids 中有很多 outputLabel 和 inputText 对

<h:panelGrid columns="2">
  <h:outputLabel value="label1" for="inputId1"/>
  <h:inputText id="inputId1/>

  <h:outputLabel value="label2" for="inputId2"/>
  <h:inputText id="inputId2/>

  ...
</h:panelGrid>

我想对所有这些行为都有一些行为:比如每个 inputText 的验证相同或大小相同。所以我创建了一个复合组件,它只包含一个 outputLabel 和一个 inputText

<my:editField value="field1"/>
<my:editField value="field2"/>

但是现在当我将它们放入 gridPanel 时,它们不会根据标签文本的长度对齐。我明白为什么,但我不知道如何解决。

【问题讨论】:

  • 我认为它们现在呈现在一个单列表中(之前它们是在一个两列表中)。一种解决方法可能是在您的复合组件中使用h:panelGrid,并且第一列足够大(尽管它有点难看;-)。
  • 感谢您的澄清!你看到了问题。我虽然有这个想法,但正如你所说:这很丑陋,你以后会遇到问题,因为渲染了许多表并且它们不相互依赖,所以它们可以由布局管理器独立移动。

标签: jsf jsf-2 grid alignment composite-component


【解决方案1】:

复合组件确实被渲染为单个组件。您想改用 Facelet 标记文件。它的渲染与它的输出渲染完全一样。这是一个启动示例,假设您想要一个 3 列的表单,其中第三列中有一个消息字段。

在/WEB-INF/tags/input.xhtml 中创建标签文件(或在/META-INF 中,当您想在要包含在/WEB-INF/lib 中的JAR 文件中提供标签时)。

<ui:composition
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">

    <c:set var="id" value="#{not empty id ? id : (not empty property ? property : action)}" />
    <c:set var="required" value="#{not empty required and required}" />

    <c:choose>
        <c:when test="#{type != 'submit'}">
            <h:outputLabel for="#{id}" value="#{label}&#160;#{required ? '*&#160;' : ''}" />
        </c:when>
        <c:otherwise>
            <h:panelGroup />
        </c:otherwise>
    </c:choose>

    <c:choose>
        <c:when test="#{type == 'text'}">
            <h:inputText id="#{id}" value="#{bean[property]}" label="#{label}" required="#{required}">
                <f:ajax event="blur" render="#{id}-message" />
            </h:inputText>
            <h:message id="#{id}-message" for="#{id}" />
        </c:when>
        <c:when test="#{type == 'password'}">
            <h:inputSecret id="#{id}" value="#{bean[property]}" label="#{label}" required="#{required}">
                <f:ajax event="blur" render="#{id}-message" />
            </h:inputSecret>
            <h:message id="#{id}-message" for="#{id}" />
        </c:when>
        <c:when test="#{type == 'select'}">
            <h:selectOneMenu id="#{id}" value="#{bean[property]}" label="#{label}" required="#{required}">
                <f:selectItems value="#{options.entrySet()}" var="entry" itemValue="#{entry.key}" itemLabel="#{entry.value}" />
                <f:ajax event="change" render="#{id}-message" />
            </h:selectOneMenu>
            <h:message id="#{id}-message" for="#{id}" />
        </c:when>
        <c:when test="#{type == 'submit'}">
            <h:commandButton id="#{id}" value="#{label}" action="#{bean[action]}" />
            <h:message id="#{id}-message" for="#{id}" />
        </c:when>
        <c:otherwise>
            <h:panelGroup />
            <h:panelGroup />
        </c:otherwise>            
    </c:choose>
</ui:composition>

在/WEB-INF/example.taglib.xml 中定义它(或在/META-INF 中,当您想在要包含在/WEB-INF/lib 中的JAR 文件中提供标签时):

<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
    version="2.0">
    <namespace>http://example.com/jsf/facelets</namespace>
    <tag>
        <tag-name>input</tag-name>
        <source>tags/input.xhtml</source>
    </tag>
</facelet-taglib>

在/WEB-INF/web.xml 中声明标签库的使用(当标签由包含在/WEB-INF/lib 中的JAR 文件提供时不需要这样做!JSF 将自动加载来自/META-INF 的所有*.taglib.xml 文件)。

<context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/WEB-INF/example.taglib.xml</param-value>
</context-param>

(多个taglib文件可以用分号;分隔)

最后只需在您的主页模板中声明它。

<!DOCTYPE html>
<html lang="en"
    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:my="http://example.com/jsf/facelets"
>
    <h:head>
        <title>Facelet tag file demo</title>
    </h:head>
    <h:body>
        <h:form>
            <h:panelGrid columns="3">
                <my:input type="text" label="Username" bean="#{bean}" property="username" required="true" />
                <my:input type="password" label="Password" bean="#{bean}" property="password" required="true" />
                <my:input type="select" label="Country" bean="#{bean}" property="country" options="#{bean.countries}" />
                <my:input type="submit" label="Submit" bean="#{bean}" action="submit" />
            </h:panelGrid>
        </h:form>
    </h:body>
</html>

(#{bean.countries} 应该返回一个Map&lt;String, String&gt;,其中国家代码作为键,国家名称作为值)

截图:

希望这会有所帮助。

【讨论】:

  • 哇,这是 BalusC 自己的答案!:) 当我使用谷歌时,你已经帮助了我很多次。我有另一个问题,但你已经给了我这个案例的完整答案,所以我会打开另一个问题。
  • 我认为最终解决方案如此复杂,我不想再在 panelGrid 内部使用复合组件了。
  • @Valter:如果你只有一个表格,那么它可能看起来确实很笨拙。但是,如果您的网站中有大约 5 个或更多这样的表单应该按照相同的标准进行布局,那么它方式要好得多。请注意,我的答案没有显示复合组件,而只是一个标记文件。
  • @BalusC 删除属性'bean'并直接传递属性会不会更容易,例如property=#{bean.username}?或者您是否有特定的理由单独包含 bean 属性?
  • @Theo:这适用于输出组件,但不适用于输入组件。则无法设置该属性。
【解决方案2】:

panelGrid 中应该有一个开关来单独渲染复合组件。我有一个解决方案。您可以拥有单独的复合组件,而不是将它们组合在一起。在每个复合组件中,您可以使用 ui:fragments 来划分您想要分别属于不同列的组件。以下是我的 inputText.xhtml 的摘录:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:composite="http://java.sun.com/jsf/composite"
    xmlns:ui="http://java.sun.com/jsf/facelets">

<composite:interface>
    <composite:attribute name="id" />
    <composite:attribute name="value" />
    <composite:attribute name="label" />
    <composite:attribute name="readonly" />
    <composite:attribute name="disabled" />
    <composite:attribute name="required" />

</composite:interface>

<composite:implementation>

    <ui:fragment id="label">
        <h:outputText id="#{cc.attrs.id}Label" value="#{cc.attrs.label}"
            for="#{cc.attrs.id}" />
        <h:outputLabel value="#{bundle['label.STAR']}"
            rendered="#{cc.attrs.required}" styleClass="mandatory"
            style="float:left"></h:outputLabel>
        <h:outputLabel value="&nbsp;" rendered="#{!cc.attrs.required}"
            styleClass="mandatory"></h:outputLabel>
    </ui:fragment>
    <ui:fragment id="field">
        <h:inputText id="#{cc.attrs.id}" value="#{cc.attrs.value}"
            styleClass="#{not component.valid ? 'errorFieldHighlight medium' : 'medium'}"
            disabled="#{cc.attrs.disabled}" required="#{cc.attrs.required}"
            label="#{cc.attrs.label}" readonly="#{cc.attrs.readonly}">
        </h:inputText>
    </ui:fragment>
</composite:implementation>

</html>

现在这将不会与 panelGrid 内的表单对齐:

<h:panelGrid width="100%">
<my:inputText label="#{bundle['label.fname']}" value="#{bean.fname}" id="fname"></my:inputtext>
<my:inputText label="#{bundle['label.lname']}" value="#{bean.lname}" id="lname"></my:inputtext>
</panelGrid>

所以我扩展了 GroupRenderer 的 encodeRecursive 方法,添加后标签和前字段:

// inside my extended renderer
protected void encodeRecursive(FacesContext context, UIComponent component)
            throws IOException {

        // Render our children recursively
        if (component instanceof ComponentRef
                && component.getId().equals("field")) {
            context.getResponseWriter().startElement("td", component);
        }

        super.encodeRecursive(context, component);
        if (component instanceof ComponentRef
                && component.getId().equals("label")) {
            context.getResponseWriter().endElement("td");
        }
    }

【讨论】:

  • +1 不是因为它是最好的解决方案,而是因为它引发的想法是,如果真的没有办法,你可以在不同的场合使用它。
猜你喜欢
  • 1970-01-01
  • 2013-02-02
  • 2013-05-03
  • 2011-06-27
  • 2013-03-23
  • 2013-04-22
  • 2011-06-04
  • 1970-01-01
相关资源
最近更新 更多