问题描述
我在数据库中存储了一个字符串。当我保存并检索字符串时,我得到的结果如下所示:
这就是我从println命令,当我调用save和index方法。
但是当我在屏幕上显示它时。它显示为:
已经尝试显示如下:
$ {adviceInstance.advice?.encodeAsHTML()}
但仍然是同样的事情。 p>
我是否需要将\\\
替换为?或类似的东西?有没有更简单的方法来正确显示它?
常见问题有多种解决方案
可能是你用
< br>替换\\\
所以无论是在你的控制器/服务,或者如果你喜欢在gsp中:
$ {adviceInstance.advice?.replace('\\ \\ n','< br>')}
2>只有textarea
< g:textArea name =somethingreadonly =true>
$ {adviceInstance.advice}
< / g:textArea>
使用< pre>
标记
< pre>
$ {adviceInstance.advice}
< / pre>
4>使用css white-space :
< div class =space>
< / div>
// css code:
.space {
white-space:pre
}
如果你有一个严格的配置来存储这些字段,那么当你通过表单提交它时,我还没有其他的元素深入研究它的实际情况,它可能实际上是回归车或\ r,无论如何在下面的评论中解释。关于设置每次接收元素时修剪元素的setter的好规则。即:
类建议{
字符串建议
静态约束= {
建议:false,minSize:1,maxSize:255)
}
/ *
*在这种情况下,使用a maxSize值,确保
*设置您自己的setter修剪任何隐藏的\ $
*,它们可能会被最终用户作为表单请求
*的一部分回传。相信我,我了解到困难的方式。
* /
void setAdvice(String adv){
advice = adv.trim()
}
}
I've stored a string in the database. When I save and retrieve the string and the result I'm getting is as following:
That is what I get from a println command when I call the save and index methods.
But when I show it on screen. It's being shown like:
Already tried to show it like the following:
${adviceInstance.advice?.encodeAsHTML()}
But still the same thing.
Do I need to replace \n to
or something like that? Is there any easier way to show it properly?
Common problems have a variety of solutions
1> could be you that you replace \n with <br>
so either in your controller/service or if you like in gsp:
${adviceInstance.advice?.replace('\n','<br>')}
2> display the content in a read-only textarea
<g:textArea name="something" readonly="true">
${adviceInstance.advice}
</g:textArea>
3> Use the <pre>
tag
<pre>
${adviceInstance.advice}
</pre>
4> Use css white-space http://www.w3schools.com/cssref/pr_text_white-space.asp:
<div class="space">
</div>
//css code:
.space {
white-space:pre
}
Also make a note if you have a strict configuration for the storage of such fields that when you submit it via a form, there are additional elements I didn't delve into what it actually was, it may have actually be the return carriages or \r, anyhow explained in comments below. About the good rule to set a setter that trims the element each time it is received. i.e.:
Class Advice {
String advice
static constraints = {
advice(nullable:false, minSize:1, maxSize:255)
}
/*
* In this scenario with a a maxSize value, ensure you
* set your own setter to trim any hidden \r
* that may be posted back as part of the form request
* by end user. Trust me I got to know the hard way.
*/
void setAdvice(String adv) {
advice=adv.trim()
}
}
这篇关于如何在gsp grails文件上显示字符串新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!