Showing multiple lines of text through EL - html

I am having problem displaying a text with multiple lines. For example, the user can type their text in a textarea in a registration form and the text can be of more than one line i.e. he can hit the Enter (return) key to insert line breaks.
On one page, if I want to show the text that he typed and I use a textarea to display (with EL), it displays the way user had entered initially.
But on another page I need this text be shown in paragraph format (using <p> tag). On this page, when I display the value that the user entered while registering, it does not have the line breaks i.e. it displays in a single line rather than with multiple lines as was entered by the user.
I already tried displaying the text through EL within a <p> tag and using a <c:out> tag of the JSTL within a <p> tag.
Some code that I tried:
Trial-1:
<p>${product.description}</p> //Doesn't show line breaks
Trial-2:
<p><c:out value="${product.description}" /></p> //Doesn't show line breaks too
How can I fix this?

Did you view the source sent to the browser ? Please try
<p><pre>${product.description}</pre></p>

Right now I can think of something as to replace the \r\n sequence in the product.description string with <br /> with help of scriptlets or fn JSTL (function) tag
Idea Courtesy: SO Answer.

Related

html text inside quotes

I am sending a text to a web service. The web service reads the text and makes a html report. The text is multiline. The web service connects all the lines and makes a single line and then wraps it in a quoted string. I want the text lines come separately in the html report. I have no access to change or view the web service's code.
I tried to add <br/> at the end of each message line before sending, but it didn't work.The browser handles <br/> like a normal text and it comes exactly in the report: line1<br/>line2.
I look for a trick to get rid of the quotes and allow the browser to interpret the html tags like <br/>.
Instead of sending
text = '************ <br/> SomeText <br/>**********';
Use
html='************ <br/> SomeText <br/> **********';
I don't know whether it satisfies your requirement

format text in textarea

I need to display the text retrieved from the database in the <textarea> tag. But the thing is that i get this text from db with html tags, like:
The line number 1<br>
The line number 2<br>
The line number three<br><br>
But when i do:
<textarea>mytext</textarea>
Inside the textarea box i get exatly the same text will all the tags, like
The line number 1<br>
The line number 2<br>
The line number three<br><br>
htmlspecialchars didn't help. It just made it to display entities instead of formatted text, without any tags as if it was show on the web page
What I need is just to show formatted text without possibility to edit it.
How can I accomplish this?
You can use following:
I hope it helps.
I'm not sure why you have HTML tags like <br> in your database. Did you use nl2br before saving or something? You shouldn't do that. Replace them by fullworthy newlines and it'll work in the textarea.
If you intend to present them in a non-textarea afterwards, then you're allowed to use nl2br or like (only at the point of presentation!), or just use CSS white-space: pre instead.

How do I inject <br> into text entered through a textarea in rails?

I have a textarea form which takes a large block of text. In this text area, I do a carriage return to end the paragraph and another carriage return to separate the paragraphs.
That text is in #contact_postalcard.message.
However, I need to output into an HTML file. The HTML file has been loaded as a long string which contains 'ReplaceThisWithPostalcardMessage' in it. I want to gsub the text from #contact_postalcard.message for ReplaceThisWithPostalcardMessage.
The problem is that the HTML file does not have any tags for each carriage return. As a result, I get one long run-on paragraph.
How can I format the substituted value in the HTML file properly?
addr_template = addr_template.gsub(/ReplaceThisWithPostalcardMessage/, #contact_postalcard.message)
Use the textarea as normal then display using :
simple_format(data_from_textarea)
How about this?
addr_template = addr_template.gsub(/ReplaceThisWithPostalcardMessage/,
#contact_postalcard.message.gsub("\n", "<br />"))

Storing text in database with tags

Can anyone please help me with this? I want to know how to store text in database with the tags. For example, if the text is bold, italics, underlined, justification, paragraphs, space etc., then even that must be retrieved and displayed in the textarea of the jsp page. Please help me out!
use function getTag("id_of_tag") to get tag.
attached its value and save it to database.Later retrieve it and show in view.
Create a field in database with datatype Text
Use some javascript text-area such as tinymce so that user can enter the text bold, italic etc.
Save the field with proper sql injectioning after form submission.

<input> multi-line capable via CSS

Is there a way to get an <input />-field in HTML to wrap lines if the text is longer than the field using CSS? I don't want to use <textarea /> as I want to avoid users entering hard line-breaks by pressing enter.
No, sorry. <input type=text> is single line by definition. See the W3C document Forms in HTML Documents:
text
Creates a single-line text input control.
Using Dojo's Dijit TextArea form control, based off TextArea, you can have an input field which begins as a single line and expands as the user adds to it.
See its documentation.
You can't do what you want with CSS alone, but you could use JavaScript to prevent the user from entering line breaks in a <textarea> field.
Look at this,
http://www.echoecho.com/htmlforms08.htm
The wrap options are the most tricky part of text areas.
If you turn wrap off the text is handled as one long sequence of text without linebreaks.
If you set it to virtual the text appears on your page as if it recognized linebreaks - but when the form is submitted the linebreaks are turned off.
If you set it to physical the text is submitted exactly as it appears on the screen - linebreaks included.
Your best bet is use a textarea (with autogrow capabilities if you like), and then strip out the new lines when the form is submitted. Using php it would be something like this:
$text = str_replace(array("\n","\r"),'',$_POST['text_field']);
This would have the desired effect of blocking newline characters. As others have pointed out it's not really possible to get multi-line input in an input field.