Velocity and $foreach.count - java

I am using velocity 1.7 and within a foreach loop I want to print the count. In the template I have the following string in a #foreach/#end section:
Count: $foreach.count
and was expecting to see in the rendered result something like
Count: 1
...
Count: 2
...
but all I see is:
Count: $foreach.count
...
Count: $foreach.count
...
Any ideas what am I doing wrong?

Neither $foreach.count nor $counter worked for me.
This answer suggests using $velocityCount, and it worked for me.

Your code is partial, we don't see the foreach directive.
Else, I know that the foreach loop has a built-in variable called $counter, though in the guide they do refer to $foreach.count

I tried with $counter & $foreach.count but neither of these worked for me.
However, the $velocityCount tag worked and below is the example.
Input code:
#foreach($entry in $entries)
<p>In for Loop count is : $velocityCount</p>
#end
Output:
In for Loop count is : 1
In for Loop count is : 2
In for Loop count is : 3

I do not know why the foreach loop built-in variable called $count is not working as guide refer. But $velocityCount is worked for me.
There is property called directive.foreach.counter.name is velocityCount in velocity.properties file, so default $count variable may not be working.

k.honsalis answer is deprecated.
At this point you can only use $velocityCount, even though the documentation will refer to deprecated methods.
#foreach($item in $items)
counter 0: $foreach.index
counter 1: $foreach.count
counter 2: $counter
counter 3: $velocityCount
#end
Output:
$foreach.index
$foreach.count
$counter
1

The default variable is velocityCount, but you can change the variable name and initial value (only in prior 2.0 versions) if you want.
VelocityEngine engine = new VelocityEngine();
engine.setProperty("directive.foreach.counter.name", "velocityCount");
engine.setProperty("directive.foreach.counter.initial.value", 1);
http://people.apache.org/~henning/velocity/htmlsingle/VelocityUsersGuide.html

$velocityCount is works for me and i'm using velocity 1.5 $foreach.count & $counter

I am currently formatting my email_html.vm like so.
Note, I am using
#set( $count = 1 )
and
#set( $count = $count + 1 )
<html>
<body>
<table style="border: 1px solid black; border-collapse: collapse">
#set( $count = 1 )
#foreach( $film in $filmList )
<tr>
<td colspan=2 style="background: bisque; text-align: center"><b>Movie $count</b></td>
</tr>
<tr>
<th style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left">Title</th>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left"> $film.getTitle() </td>
</tr>
<tr>
<th style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left">Synopsis</th>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left"> $film.getSynopsis() </td>
</tr>
<tr>
<th style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left">Trailer</th>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left"> $film.getTrailerLink() </td>
</tr>
<tr>
<th style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left">More Information</th>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left">
https://www.landmarktheatres.com/$film.getMoreInfoLink() </td>
</tr>
#set( $count = $count + 1 )
#end
</table>
</body>
</html>
Output

$foreach.count (starts with 1) and $foreach.index (starts with 0) worked for me with Velocity 2.3.
More available loop variables are mentioned in the docs.

Related

text-align, justify-content not working in Safari

In Chrome everything works good, but is aligned to the right in Safari, I don't know why. It's a button with text. I tried text-align center;, justify-content: center; or text-align: -webkit-center; but nothing happened.
.box-product .add-items .bttn-add {
width: 115px;
height: 30px;
display: block;
background-color: var(--naranjoAgro);
color: #fff;
text-transform: uppercase;
font-size: 1em;
font-weight: 700;
line-height: 30px;
border-radius: 15px;
margin: 0 0 0 1em;
transition: .3s;
-webkit-transition: .3s;
-moz-transition: .3s;
justify-content:center;
}
<div class="buttonbox">
<c:choose>
<c:when test="${(autentificar == 'true')&&(statusUsuario == 2)}">
<a class="bttn-add btncarr spinner-button" onclick="mensajeBloqueoCompras();" type="submit">agregar</a>
</c:when>
<c:when test="${autentificar == 'true'}">
<a class="bttn-add btncarr spinner-button" onclick="agregarProducto(this, '${oferta.codigo_material}', 'cantidadProdu${oferta.codigo_material}', '${contexto}', '${cantidadMinPed}', '${cantidadMaxUni}', 'HOME - PRODUCTOS EN OFERTA')" data-button-action="add-to-cart"
type="submit">agregar</a>
</c:when>
</c:choose>
</div>
For justify-content:center to work it has to be in a display:flex or display:grid block.
You can change your display: block to display:flex.
It also seems to me that your css classes do not correspond to those in your html.

Regex creation to be used on replaceAll

I am trying to use String.replaceAll(regex, String) to replace some chars from a String.
My string is some html and I've been trying to replace 'width: xxxxx.xxxpx;' width 'width: auto;'.
I came up with 2 reg expressions which according to an online reg tester works fine as:
width: [0-9]{3,}px; -- match at least 3 digits followed by px;
width: [0-9]{3,}.[0-9]{1,}px; -- match at least 3 digits followed by any char (in my case a .) followed by at least 1 digit and finally px;
This has been used in this way in my code:
text = text.replaceAll("width: [0-9]{3,}px;", "width: auto;");
text = text.replaceAll("width: [0-9]{3,}.[0-9]{1,}px;", "width: auto;");
as an example, I used this text:
<p style="margin-left: 10px;"><br></p><p style="margin-left: 10px;">text <span style="text-decoration-line: underline;">not</span> not text.</p><div class="ui-resizable-handle ui-resizable-n" style="width: 1404.8px; z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-w" style="height: 100.8px; z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-e" style="height: 100.8px; z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-s" style="width: 1404.8px; z-index: 90;"></div><table class="ui-resizable" style="width: 1405.6px; border: 1px solid black; margin-left: auto; margin-right: auto;"><tbody><tr><td class="ui-resizable" style="padding: 10px; border: 1px solid black;">in my opinion both this text is wrong;<br><br><br><div class="ui-resizable-handle ui-resizable-e" style="height: 99.2px; z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-s" style="width: 1404px; z-index: 90;"></div></td></tr></tbody></table><p style="margin-left: 10px;">other text: </p><table class="ui-resizable" style="width: 1422.4px; border: 1px solid black; margin-left: auto; margin-right: auto;"><tbody><tr><td class="ui-resizable" style="padding: 10px; border: 1px solid black;"><p><br></p><p><br></p></td></tr></tbody></table>
But this is not working. What am I doing wrong, please?
Are you sure about your code, wouldn't it be something like?
text = text.replaceAll("width: [0-9]{3,}px;", "width:auto");
text = text.replaceAll("width: [0-9]{3,}.[0-9]{1,}px;", "width:auto");
Also I would change the second Regex by adding a backslash before the dot
"width: [0-9]{3,}\.[0-9]{1,}px;"
And what do you get as result string with your current code?

Parse info from slightly different html bodys

I am building an email scraper in JAVA which need to scrape information from specific mails. Those mails have been send through a couple of years. I am facing the problem that every year there is a little change in the html code my code works fine for a specific year but won't for the following or previous year. I am looking for a way to write smart code. NEEDED is the value I need, and VARIABLE can be different. SAME TITLE is always the same.
<span class="confirmationtitle">SAME TITLE 1</span></td></tr>
<tr>
<td><span class="confirmationleft">VARIABLE</span></td>
<td><span class="confirmationright">NEEDED1 </span></td>
</tr>
<tr>
<td><span class="confirmationleft">VARIABLE</span></td>
<td><span class="confirmationright">NEEDED2</span></td>
</tr>
<tr>
<td><span class="confirmationleft">VARIABLE</span></td>
<td><span class="confirmationright">NEEDED3</span></td>
</tr>
<tr>
<td><span class="confirmationleft">VARIABLE</span></td>
<td><span class="confirmationright">NEEDED4</span></td>
</tr>
<tr>
<td><span class="confirmationleft">VARIABLE</span></td>
<td><span class="confirmationright">NEEDED5</span></td>
</tr>
<tr>
<td><span class="confirmationleft">VARIABLE</span></td>
<td><span class="confirmationright">NEEDED6</span></td>
</tr>
Above the code from year x, below from year y. There are multiple table rows like this with different info.
<tr>
<div style="font-weight: bold; display: block; margin-top: 20px;">SAME TITLE 1</div>
</td>
</tr>
<tr>
<td><span style="width: 145px; display: inline-block; zoom:1; /* IE 7 Hack starts here*/ *display:inline; line-height: 18px; vertical-align: top;">VARIABLE</span></td>
<td><span style="display: inline-block; zoom:1; /* IE 7 Hack starts here*/ *display:inline; line-height: 18px; width: 488px; vertical-align: top;">NEEDED1 </span></td>
</tr>
<tr>
<td><span style="width: 145px; display: inline-block; zoom:1; /* IE 7 Hack starts here*/ *display:inline; line-height: 18px; vertical-align: top;">VARIABLE</span></td>
<td><span style="display: inline-block; zoom:1; /* IE 7 Hack starts here*/ *display:inline; line-height: 18px; width: 488px; vertical-align: top;">NEEDED2</span></td>
</tr>
<tr>
<td><span style="width: 145px; display: inline-block; zoom:1; /* IE 7 Hack starts here*/ *display:inline; line-height: 18px; vertical-align: top;">VARIABLE</span></td>
<td><span style="display: inline-block; zoom:1; /* IE 7 Hack starts here*/ *display:inline; line-height: 18px; width: 488px; vertical-align: top;">NEEDED3</span></td>
</tr>
<tr>
<td><span style="width: 145px; display: inline-block; zoom:1; /* IE 7 Hack starts here*/ *display:inline; line-height: 18px; vertical-align: top;">VARIABLE</span></td>
<td><span style="display: inline-block; zoom:1; /* IE 7 Hack starts here*/ *display:inline; line-height: 18px; width: 488px; vertical-align: top;">NEEDED4</span></td>
</tr>
<tr>
<td><span style="width: 145px; display: inline-block; zoom:1; /* IE 7 Hack starts here*/ *display:inline; line-height: 18px; vertical-align: top;">VARIABLE</span></td>
<td><span style="display: inline-block; zoom:1; /* IE 7 Hack starts here*/ *display:inline; line-height: 18px; width: 488px; vertical-align: top;">NEEDED5</span></td>
</tr>
<tr>
<td><span style="width: 145px; display: inline-block; zoom:1; /* IE 7 Hack starts here*/ *display:inline; line-height: 18px; vertical-align: top;">VARIABLE</span></td>
<td><span style="display: inline-block; zoom:1; /* IE 7 Hack starts here*/ *display:inline; line-height: 18px; width: 488px; vertical-align: top;">NEEDED6</span></td>
</tr>
<tr>
My code for this specific row in the table:
String[] SpecificInfo = new String[6];
String TravellerInfoGender = SpecificInfo[0] = headerInfo.split("</span></td>")[1].split("</span>")[0].split(">")[2];
String TravellerInfoFirstname = SpecificInfo[1] = headerInfo.split("</span></td>")[3].split("</span>")[0].split(">")[2];
String TravellerInfoMiddleName = SpecificInfo[2] = headerInfo.split("</span></td>")[5].split("</span>")[0].split(">")[2];
String TravellerInfoSurName = SpecificInfo[3] = headerInfo.split("</span></td>")[7].split("</span>")[0].split(">")[2];
String TravellerInfoDateOfBirth = SpecificInfo[4] = headerInfo.split("</span></td>")[9].split("</span>")[0].split(">")[2];
String TravellerInfoNationality = SpecificInfo[5] = headerInfo.split("</span></td>")[11].split("</span>")[0].split(">")[2];
for(int i = 0; i < TravellerInfo.length ; i++)
writeToFile(TravellerInfo[i]);
return TravellerInfo;
Where headerInfo contains the html snippet as in the first two code examples.
I hope there is a way I do not have to hard code every little change.
Thanks!

Html tag was broken when i use a Apache common email

I sent Email by Apache Commons Email lib.
but "url in img" and "id in div" were removed in my email received.
Why remove Html tag information ?
How can i maintain html tag ?
Java code :
HtmlEmail htmlEmail = new HtmlEmail();
htmlEmail.setCharset(StandardCharsets.UTF_8.displayName());
htmlEmail.setHostName(smtpSetting.getHostname());
htmlEmail.setSmtpPort(smtpSetting.getPort());
htmlEmail.setSSLCheckServerIdentity(true);
htmlEmail.setSSLOnConnect(true);
String smtpUser = smtpSetting.getUserId();
String smtpPassword = encryption.decryptString(smtpSetting.getPassword());
htmlEmail.setAuthenticator(new DefaultAuthenticator(smtpUser, smtpPassword));
htmlEmail.setFrom(smtpSetting.getUserId(), smtpSetting.getUserId());
htmlEmail.addTo(to);
htmlEmail.setSubject(subject);
htmlEmail.setHtmlMsg(content);
htmlEmail.send();
Html:
<div style="min-width:614px; min-height:381px">
<div style="position:absolute; top:0; bottom:0; left:0; right:0; margin:auto; width : 614px; height: 381px; -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; background:#ffffff; -webkit-box-shadow: 0 1px 2px 0 #bababa; -moz-box-shadow: 0 1px 2px 0 #bababa; box-shadow: 0 1px 2px 0 #bababa; overflow:hidden;">
<div style="background:#2c3a3d; width:614px; height:62px;">
<img
url="localhost:9000/assets/images/logo_type1.png" style="display:inline-block; margin-top:15px; margin-left:23px; width:130px; height:32px; no-repeat center;"/>
</div>
Received Html:
<div style="min-width:614px; min-height:381px">
<div id="email-content-popup-box" style="position:absolute; top:0; bottom:0; left:0; right:0; margin:auto; width : 614px; height: 381px;>
<img/>
</div>

How to align the div to the center vertically in jsp

I'm developing a website for the first time. I have used two div tags. One is outer container whose width and height are set to 100%. Other one is inside container. I want to set this to the center vertically. similar to this website: http://www.bigcinemas.com/IN/home.aspx.
But its not working. I tired something like this:
index.jsp
<div class="container">
<div class="banner">
<a class="logo" href="index.jsp">
<img src="images/logo.png" alt="Rainbow Entertainment" width="250px" height="50px"/></a>
<div id="login">
<table style="background-color: purple">
<tr><td>Username : <input type="text"></td>
<td>Password : <input type="password"></td>
<td>Sign in<input type="submit"></td></tr>
</table>
</div>
</div>
</div>
</body>
</html>
menu.css
.container{
width: 100%;
height: 100%;
}
#banner{
width: 60%;
padding-left: 30%;
padding-right: 30%;
position: absolute;
}
.logo{
margin-top: 5px;
float: left;
width: 20%;
position: relative;
}
#login{
width: 30%;
float: right;
padding-left: 10%;
}
#menu{
height: 20%;
width: 70%;
}
.menu_items{
width: 80%;
color: white;
}
Also I want to the difference between id and class.
I was looking for a similar solution earlier today - check the answer on this question. How to vertically center a div for all browsers? . It sounds like the code provided there will suit your needs.
<div class="outer">
<div class="middle">
<div class="inner">
<h1>The Content</h1>
<p>Once upon a midnight dreary...</p>
</div>
</div>
</div>
CSS
.outer {
display: table;
position: absolute;
height: 100%;
width: 100%;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.inner {
margin-left: auto;
margin-right: auto;
width: /*whatever width you want*/;
}

Categories

Resources