jsp - Calculate total sum of all numbers in c:forEach loop -
i have java bean this:
class person { int age; string name; }
i'd iterate on collection of these beans in jsp, showing each person in html table row, , in last row of table i'd show total of ages.
the code generate table rows this:
<c:foreach var="person" items="${personlist}"> <tr><td>${person.name}<td><td>${person.age}</td></tr> </c:foreach>
however, i'm struggling find way calculate age total shown in final row without resorting scriptlet code, suggestions?
note: tried combining answers make comprehensive list. mentioned names appropriate give credit due.
there many ways solve problem, pros/cons associated each:
pure jsp solution
as scarcher2 mentioned above, easy , simple solution problem implement directly in jsp so:
<c:set var="agetotal" value="${0}" /> <c:foreach var="person" items="${personlist}"> <c:set var="agetotal" value="${agetotal + person.age}" /> <tr><td>${person.name}<td><td>${person.age}</td></tr> </c:foreach> ${agetotal}
the problem solution jsp becomes confusing point might have introduced scriplets. if anticipate looking @ page able follow rudimentary logic present fine choice.
pure el solution
if you're on el 3.0 (java ee 7 / servlet 3.1), use new support streams , lambdas:
<c:foreach var="person" items="${personlist}"> <tr><td>${person.name}<td><td>${person.age}</td></tr> </c:foreach> ${personlist.stream().map(person -> person.age).sum()}
jsp el functions
another way output total without introducing scriplet code jsp use el function. el functions allow call public static method in public class. example, if iterate on collection , sum values define public static method called sum(list people) in public class, perhaps called personutils. in tld file place following declaration:
<function> <name>sum</name> <function-class>com.example.personutils</function-class> <function-signature>int sum(java.util.list people)</function-signature> </function>
within jsp write:
<%@ taglib prefix="f" uri="/your-tld-uri"%> ... <c:out value="${f:sum(personlist)}"/>
jsp el functions have few benefits. allow use existing java methods without need code specific ui (custom tag libraries). compact , not confuse non-programming oriented person.
custom tag
yet option roll own custom tag. option involve setup give think esentially looking for, absolutly no scriptlets. nice tutorial using simple custom tags can found @ http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/jsptags5.html#74701
the steps involved include subclassing tagsupport:
public personsumtag extends tagsupport { private list personlist; public list getpersonlist(){ return personlist; } public void setpersonlist(list personlist){ this.personlist = personlist; } public int dostarttag() throws jspexception { try { int sum = 0; for(iterator = personlist.iterator(); it.hasnext()){ person p = (person)it.next(); sum+=p.getage(); } pagecontext.getout().print(""+sum); } catch (exception ex) { throw new jsptagexception("simpletag: " + ex.getmessage()); } return skip_body; } public int doendtag() { return eval_page; } }
define tag in tld file:
<tag> <name>personsum</name> <tag-class>example.personsumtag</tag-class> <body-content>empty</body-content> ... <attribute> <name>personlist</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.util.list</type> </attribute> ... </tag>
declare taglib on top of jsp:
<%@ taglib uri="/you-taglib-uri" prefix="p" %>
and use tag:
<c:foreach var="person" items="${personlist}"> <tr><td>${person.name}<td><td>${person.age}</td></tr> </c:foreach> <p:personsum personlist="${personlist}"/>
display tag
as zmf mentioned earlier, use display tag, although need include appropriate libraries:
Comments
Post a Comment