(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:
"..." +format on each line (necessary to wrap a text in Java)- escaping quotation marks contained within the text.
- avoid
\nfor 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.
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 doingString.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.