[英]How to maintain formatting and indentation when using com.google.appengine.api.datastore.Text
So i am using com.google.appengine.api.datastore.Text
to store whole Articles(String more than 500 char) in my GAE database. 所以我正在使用
com.google.appengine.api.datastore.Text
将整个Articles(字符串超过500个字符)存储在我的GAE数据库中。
Google API says that <variableName>.getvalue()
will give the value of the Text variable! Google API表示
<variableName>.getvalue()
将给出Text变量的值!
But if i insert a String which is already indented and formatted.. i lose the formatting and the white spaces, when i use .getValue()
function and print the Text on my webpage. 但是,如果我插入已经缩进和格式化的字符串,则当我使用
.getValue()
函数并在我的网页上打印文本时,我会失去格式和空格。
This is my code of the function the returns a HTML String to the client which then appends this HTML String to a div and displays it. 这是我的函数代码,它向客户端返回一个HTML字符串,然后将该HTML字符串附加到div并显示它。
public void getArticle(int articleId)
{
PersistenceManager pm = PMF.get().getPersistenceManager();
Articles a = pm.getObjectById(Articles.class,(articleId));
String html = "";
html += "<p>" + (a.getArticle()).getValue() + "</p>";
return html;
}
You need to wrap the HTML in <pre>
tags to preserve the formatting. 您需要将HTML包装在
<pre>
标记中以保留格式。
ie 即
html += "<p><pre>" + (a.getArticle()).getValue() + "</pre></p>";
Your issue is that browsers removes extraneous whitespace and newlines. 您的问题是浏览器删除了多余的空格和换行符。 This has nothing to do with the datastore, this would happen if you just put your content straight onto a webpage.
这与数据存储无关,如果将内容直接放在网页上,则会发生这种情况。
You either need to convert your text to html for presentation (ie replace newlines with 您要么需要将文本转换为html才能进行演示(例如,将换行符替换为
or wrap each actual paragraph in a 或将每个实际段落包装在
tag), or as suggested above use an html element which preserves formatting. 标记),或者如上建议使用保留格式的html元素。
You can do that with a tag, or by applying a css style rule of 'white-space: pre;' 您可以使用标签或通过应用CSS样式规则“ white-space:pre;”来做到这一点。 or 'white-space: pre-line;'
或“空白:行前;”
You can check out the white-space reference here . 您可以在此处查看空白参考。 On this page the behaviour you're seeing is described under the value 'normal'.
在此页面上,您看到的行为在值“正常”下进行了描述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.