Add slash in DATE YYYMMDD and Colon in TIME HHmm in jstl - jstl

I'm getting two variables From DB Date and time (${COLL.date},${COLL.time}).
The two variables values are like this 20160719 and 1234
I want to format this two variables like this 2016/07/19 and 12:34
In my JSP page, I haded this library
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
And I set tag
<fmt:parseDate pattern="yyyy/MM/dd" value="${COLL.date}" var="parsedDate" />
<fmt:formatDate value="${parsedDate}" pattern="yyyy/MM/dd" var="dateformat"/>
<p>${dateformat}</p>
This is my variable ${COLL.date} (20160719), Which I'm getting from my DB.
When I do like above, I'm getting an ERROR

OK, so you've followed the instructions from the answer on Convert and format a Date in JSP. Your parseDate format is wrong though. And, you could throw in the time as well in one go, so:
<fmt:parseDate pattern="yyyyMMdd HHmm" value="${COLL.date} ${COLL.time}" var="parsedDate" />
<fmt:formatDate value="${parsedDate}" pattern="yyyy/MM/dd HH:mm" />
If you need to output the date and time separately, use:
<fmt:parseDate pattern="yyyyMMdd HHmm" value="${COLL.date} ${COLL.time}" var="parsedDate" />
<fmt:formatDate value="${parsedDate}" pattern="yyyy/MM/dd" />
<fmt:formatDate value="${parsedDate}" pattern="HH:mm" />

Related

XMLUNIT 2 using comparison with ignore element order with diffbuilder and namespaces fails

I am trying to use DiffBuilder to ignore XML elements order when comparing two .xml files but it fails. I have tried every possible combination and read many articles before posting this question.
For example:
<Data:Keys>
<Data:Value Key="1" Name="Example1" />
<Data:Value Key="2" Name="Example2" />
<Data:Value Key="3" Name="Example3" />
</Data:Keys>
<Data:Keys>
<Data:Value Key="2" Name="Example2" />
<Data:Value Key="1" Name="Example1" />
<Data:Value Key="3" Name="Example3" />
</Data:Keys>
I want these two treated as same XML. Notice that elements are empty, they have only attributes.
What I did so far:
def diff = DiffBuilder.compare(Input.fromString(xmlIN))
.withTest(Input.fromString(xmlOUT))
.ignoreComments()
.ignoreWhitespace()
.checkForSimilar()
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder()
.whenElementIsNamed("Data:Keys").thenUse(ElementSelectors.byXPath("./Data:Value",
ElementSelectors.byNameAndText))
.elseUse(ElementSelectors.byName)
.build()))
But it fails every time. I don't know if the issue is the namespace, or that the elements are empty.
Any help will be appricated. Thank you in advance.
if you aim to match tags Data:Value by their attributes together, you should start with this:
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder()
.whenElementIsNamed("Data:Value")
and since that tag doesn't have any text, the byNameAndText won't work. You can only work on names and attributes. My advice is to do it like this:
.thenUse(ElementSelectors.byNameAndAttributes("Key"))
or
.thenUse(ElementSelectors.byNameAndAllAttributes())
//equivalent
.thenUse(ElementSelectors.byNameAndAttributes("Key", "Name"))
As of issues with namespaces, checkForSimilar() should output SIMILAR, this means they are not DIFFERENT, so this is what you need. If you didn't use checkForSimilar() the differences in namespaces would be outputed as DIFFERENT.

Spring Freemarker Form Bind : Problem with Exponent Value

I have Spring bind form with freemarker. But large number show Exponent value. How to show value without Exponent.
For small numeber...
<#spring.formInput 'CaseMaster.year' 'placeholder="e.g. 2013" ' 'number'/>
For large number...
<#spring.formInput 'CaseMaster.suitValue' 'placeholder="e.g. Suit Value" ' />
HTML View:
I wnat to show value like 80000000 or 80000000.00 or like 8,00,00,000.00 in the second field.
I've dug down into the macro and it looks like you have no control over the formatting to the populated value. Perhaps there's a more elegant solution, but at this point, you might try creating your own macro that gives you formatting control. (This is untested.)
<#macro numberInput path attributes="">
<#spring.bind path/>
<input type="text"
id="${status.expression?replace('[','')?replace(']','')}"
name="${status.expression}" value="status.value?c" ${attributes}<#closeTag/>
</#macro>
You would call it like this:
<#numberInput 'CaseMaster.suitValue' 'placeholder="e.g. Suit Value" ' />

How to compare two string in foreach loop of jsp/jstl?

I have the need to compare to strings in foreach loop.
My development environment:
1) MAC 10.11,
2) STS Version: 3.7.3.RELEASE
4) Spring Web MVC,
5) Pivotal tc Server Developer Edition v3.1
<c:set var="day" scope="session" value="${day}_${curId}"/>
<c:set var="new_day" scope="session" value="${2_456123}"/>
<c:if test="${day eq new_day}">
<p> Both are same.
</c:if>
But the system get hags at the line
<c:if test="${day eq new_day}">
Can somebody give any pointer ?
Thanks & Regards,
Arun Dhwaj
Here is demonstration code.
<%# taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<c:set var="day" scope="session" value="2"/>
<c:set var="curId" scope="session" value="456123"/>
<c:set var="day" scope="session" value="${day}_${curId}"/>
<c:set var="new_day" scope="session" value="2_456123"/>
<c:if test="${day eq new_day}">
Both are same.
</c:if>
that prints: Both are the same.
If I change line number 5 to
<c:set var="new_day" scope="session" value="${2_456123}"/>
which is what you posted, then I get an error message
contains invalid expression(s): javax.el.ELException: Failed to parse the expression [${2_456123}]
Most likely the problem is caused by the following assignment
<c:set var="day" scope="session" value="${day}_${curId}"/>
With the above line you are assigning to the var day a value that comes from another variable with the same name but not necessarily with the same scope. In other words you might have already a variable called day that lives in another scope.
The default scope is the page scope, then you have in order the request, session and application therefore if a variable with the name day is retrieved in the page or request scope that variable will be considered when you perform the test and the variable you have defined in the session scope will be ignored.
You have two options
Change the name of the variable defined in the session scope and use that name when performing the test
<c:set var="niceday" scope="session" value="${day}_${curId}"/>
<c:if test="${niceday eq ...}">
Specify the scope of the variable used in the c:if tag
<c:if test="${sessionScope.day eq ...}">
Hope that helped.

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.

JSTL calculation error

Is this the correct code to print out the calculated value? There seems to be no error but it just directly prints out all my values with the addition, times sign etc.
This is the code:
Monthly Instalment = <c:out value="(${LoanAmount} + (${LoanAmount} * ${IR} * ${param.loanPeriod}))/ (${param.loanPeriod} * 12)" />
You need to wrap the entire calculation in ${...} rather than just the individual variables:
Monthly Instalment = <c:out value="${(LoanAmount + (LoanAmount * IR * param.loanPeriod))/ (param.loanPeriod * 12)}" />
This causes the entire expression to be evaluated, whereas in your case each occurrence of ${...} being evaluated individually and the result inserted into a string.
One cause of that problem is not importing the JSTL library. Try putting
<%# taglib uri="/WEB-INF/c.tld" prefix="c" %>
...at the top of the JSP file.

Resources