how to use struts2 Iterator to iterate List<String> - java

if anyone knows , how to iterate String list in jsp using in struts2.
(I want to display values of the list in the jsp)
Please help me..

Java
public class YourFormBean{
private List<String> listOfString;
//setter & getters
}
JSP
<s:iterator value="listOfString" status="statusVar">
<tr>
<td><s:property/></td>
</tr>
</s:iterator>

Iterator will iterate over a value. An iterable value can be any of: java.util.Collection, java.util.Iterator, java.util.Enumeration, java.util.Map, or an array.
<table border="0" cellspacing="1" cellpadding="0" width="100%" class="boxtbl">
<s:iterator value="listOfString" status="strVal">
<tr>
<td><s:property/></td>
</tr>
</s:iterate>
</table>
status If specified, an instanceof IteratorStatus will be pushed into stack upon each iteration

Java code:
List<String>days=Arrays.asList("SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THRSDAY","FRIDAY","SATURDAY");
Jsp
<c:forEach items="${days}" var="days">
<option value="${days}">${days}</option>
</c:forEach>

Related

Is there any way to remove an element from array in jsp?

I want to give my users an ability to remove some items from database. Like this:
I have an array in jsp file:
<c:forEach items="${page.content}" var="row" varStatus="status">
<tr>
<td><input type="checkbox"/></td>
<td>${row.name}</td>
<td>${row.profession}</td>
<td>${row.phone}</td>
<td>${row.city.name}</td>
<td><img src="img/del.png"/></td>
Is there any way to remove an item from array "row" (for example, when I click on the picture del.png)? After that, I will send this array to the controller, to let it know which items I want to remove.
So write a if condition inside foreach tag like this.My idea is to exclude or skip the creation of html if if condition is not satisfied with the value you will specify.
<c:forEach items="${page.content}" var="row" varStatus="status">
<c:if test="${row.value != '<What you want to exclude>'}">
<tr>
<td><input type="checkbox"/></td>
<td>${row.name}</td>
<td>${row.phone}</td>
<td>${row.city.name}</td>
</tr>
</c:if>
</c:forEach>

Join two Map on Jsp page - Struts2 application

I am working on Struts2 Application;
my displaying objects are Map objects:
Map<String, HashMap<String, List<PlanDAO>>> channelRoomTypePlanmap =
new HashMap<String,HashMap<String,List<PlanDAO>>>();
Map<String, List<RateInventoryDAO>> rateInventoryMap =
new HashMap<String, List<RateInventoryDAO>>();
Here, I have two objects, where rateInventoryMap key is dependent on channelRoomTypePlanmap value combination.
Like when I print first value of channelRoomTypePlanmap I want to create key using combination of its value, and find it in rateInventoryMap , if its true then it'll print its value.
<div>
<table id="mainTable" >
<tr>
<td>
<s:iterator value="channelRoomTypePlanmap">
<table border="1">
<tr>
<td><s:property value="key" />
</td>
<td>
<table>
<s:iterator value="value">
<tr>
<td><s:property value="key" />
</td>
<td>
<table border="1">
<s:iterator value="value">
<tr>
<td><s:property value="planName" />
</td>
<s:iterator value="rateInventoryMap">
<s:iterator value="value">
<td><s:property value="currentRate" />
</td>
</s:iterator>
</s:iterator>
</tr>
</s:iterator>
</table>
</td>
</tr>
</s:iterator>
</table>
</td>
</tr>
</table>
</s:iterator>
</td>
</tr>
</table>
</div>
I tried this, which is showing in wrong format, means showing all data from second Map in single row for all. I've also heard that scriptlet code should be avoided.
How can I join these two Maps on JSP ?
Current View using above code_
Your model structure
new HashMap<String,HashMap<String,List<PlanDAO>>>();
should be improved; it is too noisy, you need five nested iterators in your page to do what you need, never know in which iterator you are without counting them, and, more important, even if this structure can be used to display data, it can't be used to send it back (you never know).
I strongly suggest you to read the following answer:
Struts2 Object Modelling
That said, when dealing with Collections in a JSP you must read about two great features of OGNL:
OGNL List Selection
OGNL List Projection

How to retrieve the data of <s:radio> within <s:iterator> tag into Struts2's action

I need to do something in webpage like this :
a bunch of rows that display questions and choices , the choices are made up of radio buttions.
I don't konw how to get these radio button's data into my java code.
This is my jsp code:
<s:iterator value="questionsList" status="status" var="questionsList">
<tr>
<td><s:property value="title"/></td>
</tr>
<tr>
<td>
<s:radio list="{'A','B','C','D'}" name="[%{#status.index}]answer" theme="simple"/>
</td>
</tr>
</s:iterator>
I've tried
private List answer = new ArrayList();
answer = (List)request.getAttribute("[%{#status.index}]answer");
but it doesn't work.
If you have other means to do this please tell me, thank you!
You can provide indexed property names for the lists you want to get in Java
<s:iterator value="questionsList" status="status">
<tr>
<td><s:property value="title"/></td>
</tr>
<tr>
<td>
<s:radio list="{'A','B','C','D'}" name="questionsList[%{#status.index}].answer" theme="simple"/>
</td>
</tr>
</s:iterator>
Voila! Your question list will be populated with indexes provided by status.index variable.

How to iterate an object in JSP to get the percentage?

I am working on servlet and jsp project. I am passing an object from servlet to JSP. And currently I am iterating that object and showing them in a table -
Below is my code in jsp -
<TABLE id="tableSMS" BORDER="1" CELLPADDING="3" CELLSPACING="1" style="text-align: center;">
<TR style="color:#ffffff;background-color:#787878;">
<TH>Hash Name</TH>
<TH>Database Name</TH>
<TH>Version</TH>
</TR>
<c:forEach var="i" begin="0" end="${reportCount.getHash().size() - 1}">
<TR>
<TD>
${reportCount.getHash().get(i)}
</TD>
<TD>
${reportCount.getDatabaseName().get(i)}
</TD>
<TD>
${reportCount.getVersion().get(i)}
</TD>
</TR>
</c:forEach>
And above code is working fine and I am able to show the data properly. Now what I need to do is -
${reportCount.getDatabaseName().get(i)} will return database name as oracle or mysql only. Now I need to calculate what is the percentage of oracle database. I will get the total number of records from ${reportCount.getHash().size(). So if total number of records is 10 and oracle database is present 5 times, then the percentage should be 50%.
Now I am not sure how would I calculate the above percentage using that object? And after calculating that percentage, I need to show the result in a new table which is shown below -
<table>
<tr>
<th style="background-color: #E8E8E6;"><b>Oracle Database</b></th>
</tr>
<tr>
<!-- I would like to show the percentage in this row -->
<td>%</td>
</tr>
</table>
I am thinking I should iterate the reportCount object again in the above new table and extract the percentage here but not sure how would I do that? Can anyone provide an example?
UPDATE:-
Here is my bean code -
public class Response {
private List<String> hash = new LinkedList<String>();
private List<String> databaseName = new LinkedList<String>();
private List<String> version = new LinkedList<String>();
// getters and setters here
}
There are three things as following:
Add the functions taglib:
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
Add reportsCounts variable to requestScope instead of session if not required on any other page & use as following:
<c:set var="oracleDBCount" value="0" scope="page" />
<c:forEach var="reportCount"
items="${requestScope.reportCounts.databaseName}" varStatus="loop">
<TR>
<TD>${requestScope.reportCounts.hash[loop.index]}</TD>
<TD>${reportCount}
<c:if test="${reportCount == 'ORACLE'}">
<c:set var="oracleDBCount" value="${oracleDBCount + 1}" scope="page"/>
</c:if>
</TD>
<TD>${requestScope.reportCounts.version[loop.index]}</TD>
</TR>
</c:forEach>
Now display percentage as:
${(oracleDBCount / fn:length(requestScope.reportCounts))*100}%
Based on my experience, doing number-crunching on a JSP page can make it unreadable very quickly. Also you may encounter additional requirements in the future such as:
Text matching (Are you guaranteed that the value is always "oracle"? Or can it become "ORACLE" or "Oracle"?
What if there are zero reports? Then you will need an if-condition to prevent division by zero.
If your client says "We have more report servers like MS, Postgres...can you show the percentage of those?"
Is it possible to do the computation inside the servlet while you are making the reportCount object?
Then you can pass the value inside a session attribute
request.getSession(true).setAttribute("oracleReports", "50%")
and then in the JSP output something like
<c:out value="${sessionScope.oracleReports}"/>
Use JavaScript for doing the operation, Here is the code
Read the comments to understand the code.
Add an id to the so that i can read the inner content using
javascript. Appended id with the index so that each raw gets different
id's
<c:forEach var="i" begin="0" end="${reportCount.getHash().size() - 1}">
<TD id="databaseName<c:out value='${i}' />">
${reportCount.getDatabaseName().get(i)}
</TD>
Add Id to the so that i can set values to the raw using
JavaScript
<tr>
<td id="oraclePercentage">%</td>
<td id="mySQLPercentage">%</td>
</tr>
call the javascript function to set the values to the raw, as we don't
have any button to trigger we open a raw and add the JavaScript call
from the JSP so that the script is triggered every time the page loads
the raw.
<TR><TD><Script>setPercentage('${reportCount.getHash().size() - 1}');</script><TD><TR>
what to do is defined here
<Script>
function setPercentage(var index){
for(var i=0; i<index;i++){
var documentType=document.getElementById("databaseName"+i).innerHTML;
var oracleCount=0;
var mySQLCount=0;
if(vardocumentType=='Oracle')
oracleCount+=(Number)1;
else if(vardocumentType=='MySQL')
mySQLCount+=(Number)1;
document.getElementById("oraclePercentage").innerHTML=(oracleCount/index)*100;
document.getElementById("mySQLPercentage").innerHTML=(mySQLCount/index)*100;
}
</script>

How i can find a elements with tag table in a html page

I have the html page, and me need find some elements with tag a 'table', but such tables maybe more than one, and they don't have id or class name.
Example:
<table>
<tbody>
<tr>
.....
<table>
<tbody>
<tr>
.....
<table>
<tbody>
<tr> <-------BINGO!
.....
How i can it's do with help HtmlUnit?
Thanks...
Use HtmlPage.getElementsByTagName("table") and iterate over the returned list of HtmlTable objects until you find the table you want to find.

Categories