Stringbuilder append newline in spring mvc

霸气de小男生 提交于 2019-12-11 04:53:07

问题


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/>");


来源:https://stackoverflow.com/questions/10220340/stringbuilder-append-newline-in-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!