Jstl String to Number parse issue - jstl

I am parsing string to number for comparison with zero(0). But getting weird result.
<fmt:parseNumber type ="number" var="parsedNumber" value="${noInStringFormate}"/>
<c:if test="${parsedNumber gt 0}">
Perform some action
</c:if>
Ex. for String "10.20" its parsed to Number 10.20.
But for String "0.23" its parsed to Number 0(zero) instead of 0.23
Even I tried by setting integerOnly="false" though its false by default. But its didn't work.
any idea on this?
Thanks for help.

Related

input type number restriction to an INTEGER in jsp

I have a fill.jsp file, in which I have the below code:
<div class="user-values"> <!-- for user's monthly gross pay -->
<label for="grossPay">Monthly gross pay: </label>
$<input id="grossPay" type="number" name="grossPay" placeholder="0.0 or greater" step="0.01" value="${grossPay}">
<span class="req">*</span>
</div>
<div class="user-values"> <!-- for error message -->
<p class="msg">${message2}</p>
</div>
<div class="user-values"> <!-- for user's dependents -->
<label for="noOfDependents">Number of dependents: </label>
<input id="noOfDependents" type="number" name="dependents" placeholder="0 or greater" step="1" value="${dependents}">
<span class="req">*</span>
</div>
<div class="user-values"> <!-- for error message -->
<p class="msg">${message3}</p>
</div>
The "Monthly gross pay" accepts double values. The "Number of Dependents" should accept only integers.
When I run this code: if I fill the number 2 or 2. for "Number of Dependents" and hit calculate, the calculation is processed successfully by my servlet.
However, if I fill the number of double value 2.1 or 2.2 or 3.1, and so on, I get the below error in my form:
MY FIRST PROBLEM:
When I enter the number of double value 2.0 or 3.0 or 4.0, etc., I get a NumberFormatException
Type: class java.lang.NumberFormatException
Message: For input string: "2.0"
Why am I not getting the error similar to the image above? Or, why is my processing not successful with 2.0? Please suggest a solution so that I can avoid the exception for values such as 1.0 or 2.0 or 3.0, and so on?
MY SECOND PROBLEM:
The value part in both input types in my fill.jsp file is giving me the below warning in NetBeans IDE:
"Bad value " " for attribute "value" on element "input": Expected a minus sign or a digit but saw " " instead."
How can I try to avoid this warning?
Please note: my program runs properly even with the warning.
I figured out the answer:
In my servlet I had a part of code: The "dependents" input box on the client-side takes in a value of 2.0, which is received as a String at the server-side.
String dependentNum = request.getParameter("dependents");
user.setDependents(Integer.parseInt(dependentNum));
For some reason, the second line of code was throwing a NumberFormatException for me. So, I changed the second line in my servlet code to:
user.setDependents((int) Double.parseDouble(dependentNum));
I don't know why the Integer object was having a problem with accepting the String value of 2.0 and converting it to 2. The workaround was to just convert the String value 2.0 to a double and cast it to an int, and I avoid getting the NumberFormatException completely.
However, I am still unable to understand, why can't the same validation error message show up as in the image for double values like 2.0, 3.0, when it can show for double values like 2.1, 3.2, etc., as below:
Input type number allows 2.0 because it theoretically is an integer, unlike 2.1, which is a double.
2.0 -> 2
However, on the server side, it does raise errors because Integers can't have tailing zeroes, or periods.
In this case, you should probably strip the tailing zeroes.
You can use a substring to cut it, and then convert it back to an Integer.
For example:
double number = 2.0;
int newnumber = Integer.parseInt(String.valueOf(number).substring(0, 1));
So the servlet should look like this:
String dependents = req.getParameter("dependents");
double dependentsd = Double.parseDouble(dependents);
int numofDependents = Integer.parseInt(String.valueOf(number).substring(0, 1));
You can verify with this test case:
String dependents = "2.0";
double dependentsd = Double.parseDouble(dependents);
int numofDependents = Integer.parseInt(String.valueOf(dependentsd).substring(0, 1));
System.out.println(numofDependents);
Or you can just cast the double to an int, like the following:
(int) Double.parseDouble(dependentNum)

Jstl equals doesn't work

The jstl equals method doesn't work for me for some reason. The code is
<span>${fromerror}</span>
<span>${fromerror eq 'mandatoryCriteria.criteria.from'}</span>
yet the result is like this
mandatoryCriteria.criteria.from
false
I'm using jstl 1.2
More specifically I'd need it in if statement but the result is the same
<c:if test="${not empty fromerror and fromerror eq 'mandatoryCriteria.criteria.from'}">
I solved it. The problem was that fromerror variable actually contained something like this
<span id="from.errors">mandatoryCriteria.criteria.from</span>
Which browser evaluated as "mandatoryCriteria.criteria.from". And that's why eq didn't work

Nokogiri: How to select the value of an attribute that contains periods in its id?

I've been working with Nokogiri for a couple of days and I absolutely adore it. Everything was working brilliantly until I got a requirement to scrape a website that uses the data-reactid javascript attribute tag. The problem is that Nokogiri seems to be getting confused with the attribute id format this website is using (several periods, some dollar signs and some other invalid xml/css characters):
An example of what I need to scrape would be:
<td data-reactid=".3.3.1:$contract_23.$=1$dataRow:0.1">94.280</td>
I need the value (94.280) inside of the attribute with an id of ".3.3.1:$contract_23.$=1$dataRow:0.1"
which usually in nokogiri we would select by doing something like:
doc.css("type[attributename=attributeid]")
in my example it would be:
doc.css("td[data-reactid=.3.3.1:$contract_23.$=1$dataRow:0.1]")
but no matter what I do to escape the invalid characters, it keeps telling me there is an invalid character after my equals sign:
Error message for code above:
nokogiri-1.4.3.1/lib/nokogiri/css/parser.rb:78:in `on_error': unexpected '.3' after 'equal'
I've tried:
a) Getting my string defined as a variable and forced into a string
b) Escaping it with backslashes (.3.[...])
c) Prefixing it with a hash (#.3.3[...])
d) Escaping it using cgi escapedString
e) Placing it inside '%{ }' eg '%{.3.3[...]}'
No matter what I do, I keep getting the same message (except for option e which gives me an altogether different error message:
: no .<digit> floating literal anymore; put 0 before dot
Can you guys help me get the right value with such an oddly-named attribute?
You didn't show how you are parsing your document, but if I parse it as HTML and then use single quotes around the attribute value in the css selector, I can get the tag:
require 'nokogiri'
html = <<END_OF_HTML
<td data-reactid="hello">10</td>
<td data-reactid=".3.3.1:$contract_23.$=1$dataRow:0.1">94.280</td>
<td data-reactid="goodbye">20</td>
END_OF_HTML
html_doc = Nokogiri::HTML(html)
html_doc.css("td[data-reactid='.3.3.1:$contract_23.$=1$dataRow:0.1']").each do |tag|
puts tag.text
end
--output:--
94.280
Check out the Mothereffing Unquoted Attribute Value Validator via this SO post:
CSS attribute selectors: The rules on quotes (", ' or none?)

If statement inside checked attribute of input element in freemarker?

Is it possible to write this in freemarker.
<input type="checkbox" value="Available ?" checked="<#if ${status}=='Available'>true<#else>false</#if>"/>
For now it throws exception
I want html checkbox to be checked if status property equals to "Available".
How to do this in freemarker?
<#if ${status}=='Available'> has a syntactical error (that the error message that you haven't included points to, I'm certain): you can't use ${...} inside FreeMarker tags (well, except inside string literals, but whatever). It should be just <#if status == 'Available'>. But, the simples solution for what you want is:
checked="${(status == 'Available')?c}"
or if you have an older FreeMarker then:
checked="${(status == 'Available')?string('true', 'false')}"

c:if test condition actually regular expression?

I try to use condition
<c:when test="${game.duration!='0:00'}">
...show rows
...
The duration here is String, and condition should be true when the String does not match 0:00. This isn't working however! When I put
<c:when test="${game.duration=='0:00'}">
... show rows
Rows are hidden, even if the duration value would be etc. 5:45. Is condition actually regular expression? How to fix this?

Resources