tags:

views:

41

answers:

4

I have a HTML text area in a JSP page. When I continuously press a button and the cursor reaches the line end it automatically goes to the second line.

When I get this text and try to print it inside a <p> tag the line breaks disappears and the whole text appears in one line, not in separate lines like I entered any ideas how to correctly print it with the new lines?

A: 

*) You can limit width of A element in user side to innitiate break;
*) Or you can insert breaks at serverside

Māris Kiseļovs
A: 

You could use this method server-side and/or implement it as a JSP tag:

public static String nl2br(String s) {
  return s.replaceAll("\n","<br/>");
}

This method replaces all newlines with their html equivalent.

halfdan
+1  A: 

It sounds like you're expecting the textarea's automatic word wrapping to be translated into line breaks.

What you're seeing in the text area is word wrapping, not a line break. Unless you press enter to put in a manual line break, there won't be any line breaks in the final text.

Secondly, if you are putting physical line breaks into your text, you still won't see them if you display it in an HTML page because HTML suppresses white space and line breaks. If you want to see your line breaks, you need to compensate for HTML. There are two ways to do this:

  • Use your programming language (JSP, from your tags) to replace the line feeds with <br> tags that HTML will understand as line breaks.
  • Display your text on the page in a block which is set to display white space. This could be the old way, using a <pre> tag, or the newer method of setting the white-space:pre-wrap; CSS style. (note that if you need to support IE6, the CSS style won't work for you, so you'll need to use <pre>)
Spudley
A: 

If you want wrapping in the textarea to turn into linebreaks (\n) set wrap="hard", ie:

<textarea wrap="hard"></textarea>

Source: https://developer.mozilla.org/en/HTML/Element/textarea

Knio
i need something like this indeed but in spring form:textarea there's no wrap attribute
sword101