Last element of an array in Thymeleaf - spring

How can I know if the current iteration is the last one?
<th:block th:each="err: ${#fields.errors('confirmedPassword')}">
[[${err}]],
</th:block>
As you can see, a comma will be placed after each error message. However, I would like to place a full stop after the last iteration instead.

This worked for me:
<th:block th:each="errMsg, errStatus: ${#fields.errors('confirmedPassword')}">
<th:block th:text="!${errStatus.last} ? ${errMsg} + ', ' : ${errMsg} + '.'"></th:block>
</th:block>

You can use the iteration status variable for this. It has a property last that is set to true on the last iteration. Something like this should work:
<th:block th:each="err, status: ${#fields.errors('confirmedPassword')}">
[[${err}]][# th:unless="${status.last}"],[/]
</th:block>

Related

how to avoid print null when value is not in th:text in Thymeleaf page

when value is not in th:text it is printing null on the page. how to handle this condition on thymeleaf page
This is Thymleaf code:
<tr th:each="respondentdata,status : ${complaintPdfBean.respondent}">
<td th:text="'Respondents No : ' + ${respondentdata.id}"></td>
<td>
<p th:text="${respondentdata.respondentDesignationname+','+respondentdata.respondentOffice+','+respondentdata.respondentothersOrg+','+respondentdata.respondentsubOrgName+','+respondentdata.respondentdepartmentName+','+respondentdata.citytownvillage}"></p>
</td>
</tr>
and this is the screenshot where value is not then it is showing null
https://i.stack.imgur.com/hqfjK.png
There are several methods to achieve this. I would like to provide a couple of the simplest ones...
You may use Thymeleaf Expression Utility #objects to check for null your object. Your code may look like the following:
<p th:text="${#objects.nullSafe(respondentdata.respondentDesignationname,'')+','+#objects.nullSafe(respondentdata.respondentOffice,'')+','+ .... so on .....}"></p>
You may use Thymeleaf Conditional Evaluation th:if attribute with Expression Utility #strings object by dividing your paragraph p into small chanks of span. Your code may look like the following:
<p>
<span th:unless="${#strings.isEmpty(respondentdata.respondentDesignationname)}" th:text="th:text="${respondentdata.respondentDesignationname + ','}"></span>
<span th:unless="${#strings.isEmpty(respondentdata.respondentOffice)}" th:text="th:text="${respondentdata.respondentOffice + ','}"></span>
<span>.... so on ....</span>
</p>

XPATH Grab following sibling and stop at the next sibling in the tree

<div class="season-rate season-summer">
<p class="heading">Summer</p>
<p class="subHeading">from</p>
<p class="price">€180,000<span>p/week + expenses</span><span class="approx">Approx
$211,500</span></p>
</div>
I am trying to grab the price here (€180,000) based on that the heading class is "Summer":
//p[contains(.,'Summer')]/following-sibling::p[2]
This returns:
€180,000p/week + expensesApprox
$211,500
But I only want:
€180,000
So I want to stop the XPATH before this next span class:
<span class="approx">Approx
$211,500</span>
I am trying variations of this without any luck!
//p[contains(.,'Summer')]/following-sibling::p[2] [not(preceding-sibling::span[contains(.,'p/week')])]
You can try this expression to get price only
//p[.="Summer"]/following-sibling::p[#class="price"]/text()
I think this should do it:
//div[p["Summer"]]/p[#class="price"]/text()[not(self="span")]
or even simpler:
//div[p["Summer"]]/p[#class="price"]/text()[not(span)]

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>

Smarty: Restart {cycle} using nested {sections}

I am trying to generate background colors for multiple tables that are contained withing a {section} ... {/section} block, but if I don't give names to the {cycle} functions, the cycle is continued, not restarted the next time it is encountered. The same problem occurs when I use named cycles and the same named cycle repeats in a section.
Example:
{section name=i loop=$tables}
<table>
{section name=j loop=$tables[i].data}
<tr class="{cycle name=bgcolor values='odd_row,even_row'}">
..
..
..
</tr>
{/section}
</table>
{/section}
How can I get the {cycle} to reset its self on each iteration of {section name=j}? Is it possible to compose names in Smarty? (use something like {cycle name=bgcolor.$i})
Note: I have the same issue on another page which is included multiple times in a single .tpl and every time it shows a table, the background color cycle continues from where it left instead of resetting its self. I understand this is normal behavior (since there is no way for Smarty to know where my cycle begins), but I would like to change it.
Used reset=true like this:
{section name=i loop=$tables}
<table>
{cycle name=bgcolor print=false reset=true values='odd_row,even_row'}
{section name=j loop=$tables[i].data}
<tr class="{cycle name=bgcolor values='odd_row,even_row'}">
..
..
..
</tr>
{/section}
</table>
{/section}

how to access this element

I am using Watir to write some tests for a web application. I need to get the text 'Bishop' from the HTML below but can't figure out how to do it.
<div id="dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5-b45385e5f45b_view" style="display: block;">
<div class="workprolabel wpFieldLabel">
<span title="Please select a courtesy title from the list.">Title</span> <span class="validationIndicator wpValidationText"></span>
</div>
<span class="wpFieldViewContent" id="dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5-b45385e5f45b_view_value"><p class="wpFieldValue ">Bishop</p></span>
</div>
Firebug tells me the xpath is:
html/body/form/div[5]/div[6]/div[2]/div[2]/div/div/span/span/div[2]/div[4]/div[1]/span[1]/div[2]/span/p/text()
but I cant format the element_by_xpath to pick it up.
You should be able to access the paragraph right away if it's unique:
my_p = browser.p(:class, "wpFieldValue ")
my_text = my_p.text
See HTML Elements Supported by Watir
Try
//span[#id='dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5b45385e5f45b_view_value']//text()
EDIT:
Maybe this will work
path = "//span[#id='dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5b45385e5f45b_view_value']/p";
ie.element_by_xpath(path).text
And check if the span's id is constant
Maybe you have an extra space in the end of the name?
<p class="wpFieldValue ">
Try one of these (worked for me, please notice trailing space after wpFieldValue in the first example):
browser.p(:class => "wpFieldValue ").text
#=> "Bishop"
browser.span(:id => "dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5-b45385e5f45b_view_value").text
#=> "Bishop"
It seems in run time THE DIV style changing NONE to BLOCK.
So in this case we need to collect the text (Entire source or DIV Source) and will collect the value from the text
For Example :
text=ie.text
particular_div=text.scan(%r{div id="dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5-b45385e5f45b_view" style="display: block;(.*)</span></div>}im).flatten.to_s
particular_div.scan(%r{ <p class="wpFieldValue ">(.*)</p> }im).flatten.to_s
The above code is the sample one will solve your problem.

Resources