[英]Is there any way to get the single value from hashmap.?
When I try to retrieve the values from
HashMap
i use getValue
method. 当我尝试从
HashMap
检索值时,我使用getValue
方法。 but the getValue
method is inside the iteration tag in Struts2 so, it is displaying even unwanted values. 但是
getValue
方法位于Struts2的迭代标记中,因此,它甚至显示不需要的值。 how can i avoid this? 我如何避免这种情况? here in my JSP page i am creating table, where i want to set the
orderdate
from database inside the <th>
tags. 在我的JSP页面上,我正在这里创建表,我想在其中设置
<th>
标记中来自数据库的orderdate
。 i mean from HashMap
i am receiving orderdate
, result, dataamount
, count. 我的意思是从
HashMap
我正在接收orderdate
,结果, dataamount
,count。 in <th>
i want orderdate
to go and sit and in <tr>
i want remaining three values to go and sit. 在
<th>
我要orderdate
坐下,在<tr>
我要剩余的三个值坐下。 How do i do that ? 我怎么做 ?
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class JanuaryAction {
private String from; // creating variables to access input values.
private String to; // creating variables to access input values.
Map abc1 = new HashMap(); // Getting the map objects to store tha values
public Map getAbc1() { /* Creating the setters and getters */
return abc1;
}
public void setAbc1(Map abc1) {
this.abc1 = abc1;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public Map<String, String> getAbc() {
return abc;
}
public void setAbc(Map<String, String> abc) {
this.abc = abc;
}
Map<String, String> abc = new HashMap<String, String>();
Map<String, List<ReportClass>> map = new HashMap<String, List<ReportClass>>(); // Using
// Map
// interface
// implementing
// hashmap
public Map<String, List<ReportClass>> getMap() {
return map;
}
public void setMap(Map<String, List<ReportClass>> map) {
this.map = map;
}
public String execute() throws Exception {
String fromdate = getFrom();
System.out.println(fromdate);
String todate = getTo();
System.out.println(todate);
Connection con = GetCon.getCon();
Statement statement = con.createStatement();
ResultSet resultset = statement
.executeQuery("SELECT MarketPlace,OrderDate, ROUND(SUM(Total),2), COUNT(*) , ROUND(ROUND(SUM(Total),2)/ COUNT(*),2) FROM vend_printed WHERE OrderDate >='"
+ fromdate + "' AND OrderDate <='" + todate + "' GROUP BY OrderDate,MarketPlace");
while (resultset.next()) {
String marketplace = resultset.getString(1);// get the first column
// in marketplace
String orderdate = resultset.getString(2);// get the second column
// in orderdate
Double datamount = resultset.getDouble(3);// get the third column in
// datamount
Integer count = resultset.getInt(4);// get the fourth column in
// count
Double result = resultset.getDouble(5); // get the fifth column in
// result
ReportClass a = new ReportClass(); // create an object to access
// pojo class.
a.setMarketplace(marketplace); // setting the values in pojo class
a.setOrderdate(orderdate);
a.setDataamount(datamount);
a.setCount(count);
a.setResult(result);
if (map.get(marketplace) != null) { // if the value in marketplace
// is null then go to else
// statement or go to if
// statement.
map.get(marketplace).add(a); // using the key add one complete
// row values from a to
// marketplace
} else {
List<ReportClass> optionsVO = new ArrayList<ReportClass>(); // create
// a
// list
// optionsVo
optionsVO.add(a); // store Reportclass values in List.
map.put(marketplace, optionsVO); // storing it in map.
/* System.out.println(map.get(marketplace).toString()); */
}
}
Set s = map.entrySet(); // A map entry (key-value pair).creating Set
// interface.
Iterator i = s.iterator();
while (i.hasNext()) {
List<ReportClass> tempList = (List<ReportClass>) ((Entry) i.next()).getValue(); // creating
// a
// temporary
// list
for (ReportClass reportClassObj : tempList) {
System.out.println(reportClassObj.getMarketplace());// display
// values.
System.out.println(reportClassObj.getDataamount());
System.out.println(reportClassObj.getOrderdate());
System.out.println(reportClassObj.getCount());
System.out.println(reportClassObj.getResult());
}
}
/* System.out.println(map.size()); */
return "success";
}
}
in my jsp i have a code. 在我的jsp中,我有一个代码。
<s:iterator var="studentEntry" status="stat" value="map.entrySet()">
Key: <s:property value="%{#studentEntry.getKey()}"/>
Value: <s:property value="%{#studentEntry.getValue()}"/><br>
</s:iterator>
Whether you group entries you have to iterate groups first, then other entries by group through get all values or choose the group which you want to iterate. 不管您是对条目进行分组,都必须先对组进行迭代,然后再逐组访问其他条目,以获取所有值或选择要进行迭代的组。
<s:iterator value="map">
<s:iterator var="studentEntry" value="value">
ReportClass: <s:property value="%{#studentEntry.toString()}"/><br>
</s:iterator>
</s:iterator>
Actually you don't need to use entrySet
just use map
variable as value
attribute of <s:iterator>
tag and value
keyword as value of second iterator. 实际上,您不需要使用
entrySet
只需将map
变量用作<s:iterator>
标记的value
属性,并将value
关键字用作第二个迭代器的值。 Then you can directly reference properties of your ReportClass
inside the iterator in <s:property>
tag. 然后,您可以在
<s:property>
标签中的迭代器中直接引用ReportClass
<s:property>
。
<s:iterator value="map">
<s:iterator value="value">
<table>
<tr><th colspan="3"><s:property value="orderdate"/></th></tr>
<tr>
<td><s:property value="dataamount"/></td>
<td><s:property value="count"/></td>
<td><s:property value="result"/></td>
</tr>
</table>
</s:iterator>
</s:iterator>
try the below code, it must work. 试试下面的代码,它必须工作。
<s:iterator value="map">
<s:iterator value="key">
<s:iterator value="value">
<th><s:property value="orderdate"/></th>
<td rowspan="4"><s:property value="marketplace"/></td>
<tr> <td><s:property value="dataamount"/></td></tr>
<tr><td><s:property value="count"/></td></tr>
<tr><td><s:property value="result"/></td></tr>
</s:iterator>
</s:iterator>
</s:iterator>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.