JSTL: How to print a newline - jstl

How can I print a newline ("\n" or "\r\n" or "\n\r"). Which is the right one to be understood by a browser?) using JSTL or EL? I want to really print a newline (not a <BR>), since I need to place it in a javascript section in a HTML file.

Try the xml entities for this:
for a newline and 
 for carriage return.

Simple, solution is just not to use JSTL/EL
<% out.print("\n"); %>

Even simpler:
<%= '\n' %>

You are asking the wrong question in your main post, and you later added it in a comment (perhaps you should edit your post to reflect the information in the comment?):
I need to put a \n after a // <![CDATA[ to end the comemnt before actaul JS code starts.
The easiest way to fix your issue is to comment out the CDATA using a block comment like this:
/* <![CDATA[ */
This will allow you to continue your code on the same line and it will not be part of the comment.
Example
/* <![CDATA[ */ var foo = "var";alert( foo ); /* ]]> */

Try the below concept and it works.
<c:set var="String1" value="line1 line2 line3 line4" />
<c:set var="String2" value="${fn:split(String1, ' ')}" />
<c:set var= "new" value="<br />" />
<c:out value="${String2[0]}${new}" escapeXml="false" />
<c:out value="${String2[1]}${new}" escapeXml="false" />
<c:out value="${String2[2]}${new}" escapeXml="false" />
<c:out value="${String2[3]}${new}" escapeXml="false" />

Related

How to skip paragraphs with comments in XPath expression?

I'm trying to scrape websites like this with the following Xpath expression:
.//div[#class="tresc"]/p[not(starts-with(text(), "<!--"))]
The thing is that the first paragraph is a comment section, so I'd like to skip it:
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:HyphenationZone>21</w:HyphenationZone>
<w:PunctuationKerning />
<w:ValidateAgainstSchemas />
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid
<w:IgnoreMixedContent>false</w:IgnoreMixedContent
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:Compatibility>
<w:BreakWrappedTables />
<w:SnapToGridInCell />
<w:WrapTextWithPunct />
<w:UseAsianBreakRules />
<w:DontGrowAutofit />
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]-->
Unfortunately, my expression does not skip the paragraph with comments. Anyone know what I'm doing wrong?
Comments are not part of text(), they constitute a node of their own: comment(). To exclude p's that contain comments, use
p[not(comment())]

JSTL <c:if> tag to compare two variables

Why the block never reaches "conditionTrue"? I have printed both the variables
authorEmail and scase.authorEmail and they are same string. If I use literal like 'abc#yahoo.com' to test the condition , it works but not if both the c:if params are variables.
<c:forEach var="scase" items="${section.subSectionList.get(0).SCaseList}">
<c:set var="authorEmail" >
<sec:authentication property="principal.email" />
</c:set>
<c:if test = "${authorEmail == scase.authorEmail}" >
<option>conditionTrue</option>
</c:if>
</c:forEach>
UPDATE - I got it to work by making these changes.
I commented out the entire c:set tag. Instead I added these line -
<sec:authentication var="principal" property="principal" />
My test condition was then :
<c:if test = "${principal.email == scase.authorEmail}" >
<option>conditionTrue</option>
</c:if>
And this worked. However, the reasoning behind it still baffles me. Any insight, much appreciated.

Line break or new paragraph inside foreach jsp

I got a small problem i want to add for example i have some values in my foreach which i recive from my database how can i add a br or a p because now its just in one value
<c:forEach var="vac" items="${loc}">
<div class="featurette">
<p class="vactextl">
${vac.description}
</p>
</c:forEach>
I get now a text from database but i want to add a new line or break.
what i want the text is coming from a database its very long discription text i want to add breaks or paragraph in it but now i cant when i print it out there is no paragraph or anything just text is the are way to do this?
In my database is stored like this http://puu.sh/q73rp/b26d8f7014.png
But when i show it on html i get this http://puu.sh/q73uY/4026d4dc62.png
Is there a way to replace \n \r with br this would be fix the problem
Its obviously wrong way to do it like below but for now you can do it
<c:forEach var="vac" items="${loc}">
<div class="featurette">
<p class="vactextl">
${vac.description}
<%out.println("<br>")%>
</p>
</d>
</c:forEach>
Note: out is jsp's implicit object
You're going to have to split your string if you want to output it with line breaks from the database. You can then try putting the split strings in an arraylist then output them in your forEach loop.
<c:forEach var="vac" items="${loc}">
<div class="featurette">
<c:out value="${vac.description}" /><br/>
</c:forEach>

JSTL c:if Comparison Failing

I am using the following block of code, which is meant to conditionally display a Spring MVC form tag checkbox:
"<c:out value="${contractForm.option}" />"
<c:if test="${ (contractForm.option == ' 4') || (contractForm.option == ' 5')} ">
<form:checkbox path="hold" /><form:label path="hold">Hold</form:label>
</c:if>
The "option" field is a 2-character fixed-width field. The <c:out> will print " 5", but the <c:if> fails. Can anyone help?
Jason
For some reason, <c:choose> with <c:when> blocks worked. Thanks to JB Nizet for tryng to assist.

jstl access a second array value with foreach index

i have two arrays (strings delimited with commas) and i made a foreach loop on one of this vars
i need to be able to access to the other string with the foreach index like
<c:set var="name" value="Zara,nuha,roshy" />
<c:set var="name2" value="Zara2,nuha2,roshy2" />
<c:forEach items="${name}" delims="," var="name" varstatus="i">
<c:out value="${name}"/><br>
</c:forEach>
i need to access name2 values, in the name foreach, is it possible without doing another foeach?
The varstatus variable you are using contains a value "index" that you can use.
But, you can't operate on a string like that (not that I know of at least).
First, you need to convert name2 to a proper array or list. Then you can access it inside the for loop:
${name2list[i.index]}
Now, how to convert it to an array? How about the split function?
<%#taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="name2list" value="${fn:split(name2, ',')}"/>
This can be done, contrary to some previous comments.
See the following example based on the question:
<c:set var="names" value="Zara,nuha,roshy" />
<c:set var="names2" value="Zara2,nuha2,roshy2" />
<c:forEach items="${names.split(',')}" varStatus="i" var="name" >
${name} : ${names2.split(',')[i.index]}<br/>
</c:forEach>
Essentially we're using a string split function with expression language to get a string array from list comma separated values. Within the loop we the get the second value from the names2 array by using the varStatus index. I believe this accomplishes the task.

Resources