简体   繁体   English

使用com.google.appengine.api.datastore.Text时如何维护格式和缩进

[英]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.

相关问题 如何在数据库中存储“ com.google.appengine.api.datastore.Text” - How to store “com.google.appengine.api.datastore.Text” in database 如何使用com.google.appengine.api.datastore.Text - how to use com.google.appengine.api.datastore.Text 如何将Google App Engine数据存储区中的数据写入com.google.appengine.api.datastore.Text - How do I write data in my Google App Engine Datastore to com.google.appengine.api.datastore.Text 引起:java.lang.ClassCastException:com.google.appengine.api.datastore.Text 无法转换为 java.lang.String - Caused by: java.lang.ClassCastException: com.google.appengine.api.datastore.Text cannot be cast to java.lang.String AppEngine ClassNotFoundException:com.google.appengine.api.datastore.DatastoreServiceFactory - AppEngine ClassNotFoundException: com.google.appengine.api.datastore.DatastoreServiceFactory 如何在Google App Engine _ah / api / explorer中创建com.google.appengine.api.datastore.Key对象? - How to create the com.google.appengine.api.datastore.Key object in the Google App Engine _ah/api/explorer? 将属性设置为com.google.appengine.api.datastore.Entity - Setting a property to com.google.appengine.api.datastore.Entity com.google.appengine.api.datastore.DatastoreNeedIndexException:找不到匹配的索引 - com.google.appengine.api.datastore.DatastoreNeedIndexException: no matching index found com.google.appengine.api.datastore.DatastoreFailureException:意外失败 - com.google.appengine.api.datastore.DatastoreFailureException: Unexpected failure 示例应用中的com.google.appengine.api.datastore.DatastoreNeedIndexException错误 - com.google.appengine.api.datastore.DatastoreNeedIndexException error in Sample App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM