Stringbuilder append newline in spring mvc

2019-08-08 17:13发布

问题:

I am still learning Spring Roo so please bear with me. Here is what I have (generated by roo)

public String convert(Cluster cluster) {
        return new StringBuilder().append(cluster.getName()).toString();
    }

so what that produces is this.

<div id="_s_com_clusters_id" class="box">Cluster1,Cluster2,Cluster3,Cluster4,Cluster5,Cluster6</div>

Which displayed

Cluster1,Cluster2,Cluster3,Cluster4,Cluster5,Cluster6

to the webpage.

What I want to do is insert a new line instead of the "," i tried append("\n") and that added a space between each but that was it.

Edit: I have also tried append("
") as well and the generated html is

<div id="_s_com_clusters_id" class="box">Cluster1&lt;br/&gt;,Cluster2&lt;br/&gt;,Cluster3&lt;br/&gt;,Cluster4&‌​lt;br/&gt;,Cluster5&lt;br/&gt;,Cluster6&lt;br/&gt;</div>

Which results in

Cluster1<br/>,Cluster2<br/>,Cluster3<br/>,Cluster4&‌​lt;br/>,Cluster5<br/>,Cluster6<br/>

being displayed on the webpage.

回答1:

It's possible that your spring configuration escapes HTML code. Check if there is something like that :

<context-param>
    <param-name>defaultHtmlEscape</param-name>
    <param-value>true</param-value>
</context-param>

in your web.xml file



回答2:

Try this, it should be fine:

append("<br/>");