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.
Related
While attempting to render in HTML a collection of article barcodes and proceeding incrementally to view the data (relative to other objects on tha page), the controller
require 'barby/outputter/html_outputter'
require 'barby/barcode/ean_13'
and the view invokes
<%= this_barcode = article.barcodes.first.barcode %>
<%= this_barcode.class %>
<%# barcode = Barby::EAN13.new(this_barcode) %>
<%# outputter = Barby::HtmlOutputter.new(barcode) %>
<%# barcode.to_html.html_safe %>
[returns as expected]
8001300303466 String
however, when wanting to get the barcode and uncommenting that element it fails to process because data not valid for the line barcode = Barby::EAN13.new(this_barcode) (also occurs with barcode = Barby::EAN13.new(this_barcode.to_i))
note possibly on garden path here, as there is confusion with what the wiki suggests with
barcode = Barby::DataMatrix.new(number) as that would generate the error
uninitialized constant Barby::DataMatrix
How does one ensure the data is correct for proper rendering?
Here's the issue:
8001300303466 has 13 characters. It is the correct barcode.
I was assuming one could submit a correct barcode. However line 54 of the gem's ean_13.rb file
allows a 12 character data element FORMAT = /^\d{12}$/ and proceeds to calculate the check digit.
Thus,
<%= barcode = Barby::EAN13.new(this_barcode[0...-1]) %>
will end up processing the data correctly.
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" />
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.
I have a jstl value,
<c:forEach items="${detail}" var="u">
<c:set value="${u.content}" var="c"/></c:forEach>
I want to set the variable in jquery equal to the var = "c".
How could I do that?
<script>
<c:forEach items="${detail}" var="u">
<c:set value="${u.content}" var="c"/>
var whatever = ${c};
</c:forEach>
<script>
You might need to JS escape it, quote it, etc. depending on what's actually in the variable, which you don't mention. It's also not clear what "a variable in jQuery" is.
The bottom line is that if the JavaScript lives in a JSP, just use the value: the JS isn't evaluated until the response has been emitted, so mix-and-match, but be aware that it's pretty easy to create invalid JavaScript unless you take care to properly escape whatever value types you're emitting.
As an example, consider a Java String that contains a single quote: if you single-quote the Java value in JS you'd have invalid JS because you didn't JS-escape the string value.
the method given in the below code might be correct but the variable whatever would have only one value that also only during the last count of the for each loop would exist
<script>
<c:forEach items="${detail}" var="u">
<c:set value="${u.content}" var="c"/>
var whatever = ${c};
</c:forEach>
<script>
for instance if the loop for the variable detail is of list type that consists of string type objects in it like "A" , "B" ,"C" then in the case the value of the var whatever will be "C", this is because jstl is compile time language.
So the above code won't work in for each loop
the following code might help you out or atleast provide you a idea for same.
<script>
var whatever = new Array;
<c:forEach items="${detail}" var="u">
<c:set value="${u.content}" var="c"/>
for (var i=0;i<whatever.length;i++)
{
whatever[i]=${c};
}
</c:forEach>
<script>
I am trying to create a form that allows the editing of multiple rows of data. I have no problem looping through and getting input boxes to render...I just cannot get the name attributes to output correctly.
I know that in order to submit a collection you need to post back an indexed name where the index is sequential starting at 0.
<input name="Books[0].Title" />
<input name="Books[1].Title" />
and so on...
Now, I can get the EditorFor function to output my proper name with given the following loop code
#For n = 0 To (Model.Books.Count - 1)
#Html.EditorFor(Function(m) Model.Books.Item(n).Title)
Next
giving me
<input name="Books[0].Title" />
<input name="Books[1].Title" />
and so on...
My problem is VS shows the following warning
Using the iteration variable in a lambda expression may have unexpected results. Instead, create a local variable within the loop and assign it the value of the iteration variable.
Yet when I change the loop to
#For n = 0 To (Model.Books.Count - 1)
Dim item = Mode.Books.Item(n)
#Html.EditorFor(Function(m) item.Title)
Next
I get
<input name="$VB$Local_item.Title" />
<input name="$VB$Local_item.Title" />
and so on...
Any thoughts? Should I just ignore the warning?
Thanks.
Jason
MVC works by actually breaking apart the lambda expression, and seeing what it's made of. It doesn't just execute the lambda and get the result. So you need to actually use the model parameter in the lambda for it to work. This should do it for you:
#For n = 0 To (Model.Books.Count - 1)
Dim index = n
#Html.EditorFor(Function(m) m(index).Title)
Next