Thymeleaf构建一个字符串

时间:2017-02-11 21:03:53

标签: html spring thymeleaf

我想通过迭代循环并将值连接到此字符串变量来在百万富翁中创建一个字符串变量。然后我想在<span>元素中显示此字符串。我想要实现的内容可以用java编写如下:

String forDisplay = "";
foreach (MyObject o : myObjectCollection) {
   if (o.type == 1) { forDisplay += o.stringValue; }
}

然后在我想把它放在像span这样的html元素中。我知道如何使用:

<span th:each="o : ${objectCollection}" th:if="${o.type == 1}" th:text="${o.stringValue}"></span>

但是这会为满足条件的每个元素创建<span>。我只想在th标记免费部分中构建我的字符串,然后我只想在单个<span>元素中显示我的字符串。

2 个答案:

答案 0 :(得分:0)

艾哈迈德,请查看来自Thymeleaf docs的Expression Utility Objects for Strings

您有三种方法可以加入项目:

${#strings.arrayJoin(namesArray,',')} // For Arrays
${#strings.listJoin(namesList,',')} // For Lists
${#strings.setJoin(namesSet,',')} // For Sets

这些实用程序对象为聚合,日历等提供了许多很酷的方法。

ATT

答案 1 :(得分:0)

以下是使用&#34;,&#34;将数字连接到字符串的方法作为分隔符

<?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:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core" 
  xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
  xmlns:p="http://primefaces.org/ui">

<ui:composition template="templates/template.xhtml"> 
    <!--header and footer template-->
    <ui:define name="content">
        <f:loadBundle basename="internationalization.selfEval" var="msg" />
        <h:form id="form">
            <p:growl id="info" autoUpdate="true"  />

            <p:dataTable id="selfEval" var="eval" value="#{selfEvalBean.selfEvaluations}" >
                <f:facet name="header">
                    #{msg['header']}
                </f:facet>
                <p:columnGroup type="header">
                    <p:row>
                        <p:column>
                            <f:facet name="header">#{msg['selfEval']}</f:facet>
                        </p:column>
                        <p:column width="2%">
                            <f:facet id="sad" name="header">
                                <p:graphicImage library="images" name="sad.png"/> 
                            </f:facet>
                        </p:column>
                        <p:column width="2%">
                            <f:facet id="sad" name="header">
                                <p:graphicImage library="images" name="normal.png"/> 
                            </f:facet>
                        </p:column>
                        <p:column width="2%">
                            <f:facet id="sad" name="header">
                                <p:graphicImage library="images" name="happy.png"/> 
                            </f:facet>
                        </p:column>
                    </p:row>                      
                </p:columnGroup>

                <p:subTable var="t" value="#{eval.topics}">
                    <f:facet name="header">
                        <h:outputText value="#{eval.title}" />
                    </f:facet>

                    <p:column id="topic" >
                        <h:outputText value="#{t}" /> <!--t is of type List<SelfEvaluation> and not SelfEvaluationTopic-->
                        <p:commandButton style="float:right; width:22px; height: 22px; background-color: #cd001e;" title="Delete" update=":form" action="#{selfEvalBean.remove(t)}" icon="fa fa-trash-o" />
                    </p:column>

                    <p:column width="2%" >
                        <div style="text-align: center;" >
                            <p:selectBooleanCheckbox id="s" value="#{t}"  />
                        </div>
                    </p:column>

                    <p:column width="2%"  >
                        <div style="text-align: center;" >
                            <p:selectBooleanCheckbox id="n" value="#{t}"  />
                        </div>
                    </p:column>

                    <p:column width="2%" >
                        <div style="text-align: center;" >
                            <p:selectBooleanCheckbox id="h" value="#{t}" />
                        </div>
                    </p:column>
                </p:subTable>
            </p:dataTable>

                <center>
                    <p:commandButton id="addSelfEvalTopic" styleClass="button" value="#{msg['actionAdd']}" onclick="PF('evalDialog').show();" update=":form" />
                    <p:commandButton id="selection" styleClass="button" style="float:right;" value="#{msg['actionSelect']}" action="#{selfEvalBean.save}" />
                </center>


        </h:form>
        <p:dialog widgetVar="evalDialog" header="#{msg['newTopic']}" showEffect="clip" hideEffect="clip" resizable="false">
            <h:form id="dialog">
                <h:panelGrid columns="2">
                    <p:outputLabel value="Description:" />
                    <p:inputText value="#{selfEvalBean.topic.topic}" required="true" maxlength="60" />

                    <p:commandButton value="#{msg['actionSave']}" styleClass="button"  action="#{selfEvalBean.addTopic}" update=":form" oncomplete="PF('evalDialog').hide();" />
                    <p:commandButton value="#{msg['actionCancel']}" styleClass="button"  immediate="true" oncomplete="PF('evalDialog').hide();" /> 
                </h:panelGrid>
            </h:form>
        </p:dialog>
    </ui:define>
</ui:composition>