简体   繁体   中英

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.

Google API says that <variableName>.getvalue() will give the value of the Text variable!

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.

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.

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.

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
or wrap each actual paragraph in a

tag), or as suggested above use an html element which preserves formatting.

You can do that with a tag, or by applying a css style rule of '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'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM