0

(For just multi-line strings in Java, see: Java multiline string. This question below may bleed out into different answers considering the web app and HTML output context.)

Is there a way to enter multiple lines of text (stored a String) as Java code without formatting it?

I'd like to avoid:

  1. "..." + format on each line (necessary to wrap a text in Java)
  2. escaping quotation marks contained within the text.
  3. avoid \n for breaks on each line

It's a MVC webapp and it's HTML code I am outputting, fitted with some variables here and there. The string in question is so long as it's an entire row of a table, where each row contains values such as id, key, value, etc. The .jsp file just has a single variable that contains the eventually output of all these rows, generated by a Java method. I've done the same with drop down boxes in forms, generating maybe options, <option>...</option>, but that code was small and tight when generated with Java. These rows I am stuck on are are large and complicated by comparison. I want the output HTML to be readable.

Likely not, but had to ask. Never know what you might miss.

Community
  • 1
  • 1
Xonatron
  • 15,622
  • 30
  • 70
  • 84
  • This question has already been answered: http://stackoverflow.com/questions/878573/java-multiline-string – Sahand Feb 28 '12 at 19:41
  • @Achamenes, it's a little bit different, as this is a webapp outputting HTML, and within that context the answer might be different. I'll clarify this a bit more in the question. – Xonatron Feb 28 '12 at 19:57

2 Answers2

5

I guess you ask this question because you are going to keep a really big string(s) in your Java code. And this is bad.

Code and resources are different things, and mixing them is not recommended. Java provides a number of ways to manage resources, you can use Properties to store plain text/HTML resources, and ResourceBundle to store localized resources.

PS You mentioned variables - you're not concatenating them I hope? Putting something like foo=bar{0} in resource bundle and then doing String.format(bundle.getString("foo"), fooNum); should be a preferred way.

edit

Concerning the Java variable containing the whole HTML table - that's why JSP Custom Tags were invented, - to encapsulate your weird HTML-generation logic. Implementing HTML table generation logic with JSTL in Tag Files is the way to go - you won't need to compile anything.

Oleg Mikheev
  • 17,186
  • 14
  • 73
  • 95
  • You are exactly right. I am doing something that is wrong, hoping to to it right when I later learn how to do it right. I appreciate this input. Basically it's a webapp and I need to return (table) rows of values, and the table row structure HTML-wise is complicated and needs to be outputted for each row of values. Much easier and cleaner would be a list of options in a drop down menu. I am trying to figure out how to properly handle the abundance of HTML being outputted by my code for each row. – Xonatron Feb 28 '12 at 19:55
  • why are you not using JSP? or JSF? you can output tables of any complexity with that, and dealing with HTML on business tier should be avoided. Also JSP is much easier to program for a beginner - it gets compiled on its own :) – Oleg Mikheev Feb 28 '12 at 19:58
  • I am using JSP. The `.jsp` code links to a single variable which is populated (if that's the right word) with the HTML string info from a Java function. Should I be using `.jsp` solutions to do this? I originally had Java code in the `.jsp` to loop through the rows and it was ugly. Much cleaner if there's just a single variable in place of the code. Please read the original question again, I added more info to it. – Xonatron Feb 28 '12 at 20:02
  • perhaps `jstl` is a solution to use, instead of the more ugly `Java`, inside of my `.jsp` files. My goal was to remove all code out of my `.jsp` files, however. – Xonatron Feb 28 '12 at 20:06
  • Excellent. I will look into this. I'll accept your answer for now and look into this very soon! – Xonatron Feb 28 '12 at 20:16
0

If by multiline, you refer to the existance of paragraphs, then "\n" is probably enough. What I usually do if I want a more advanced multiline String is create a class with a variable inside that stores each line in a String[] or in an ArrayList<String>:

DarkVeneno
  • 101
  • 8