怎么用取HashMap类型的值?

flyIce945 2007-09-11 12:11:23
目前正在学着使用标签,
以下这段已经可以了,取的是ArrayList~
但是HaspMap取不出来了就,报错...

<%-- ########### 循环显示 ########### --%>
<c:forEach var="books" items="${requestScope.LIST}">
<tr align="center">
<td class=tablebody2 valign=middle align=center width="">${books.id}
</td>
<td class=tablebody1 valign=middle width="" align="left">    <a href="toProductDetail.do?id=${books.id}">${books.name}</a>
</td>
<td class=tablebody2 valign=middle align=center width="">
${books.basePrice}
</td>
<td class=tablebody1 valign=middle align=center width="">
<a href="addshopcart.do?id=${books.id}">
<img border="0" src="images/car_new.gif" width="97" height="18"> </a>
</td>
</tr>

</c:forEach>

怎么学习标签啊?到http://jakarta.apache.org/当什么呐?
...全文
1439 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyIce945 2007-09-14
  • 打赏
  • 举报
回复
请教下
怎么学习标签,有什么好的下载不?
==================
问完这个就结帖,授人以鱼不如授人以渔...
zjf405 2007-09-14
  • 打赏
  • 举报
回复
jstl标签、el语句、struts标签一般书上都有说
如果用struts做为自己的mvc框架的话,那建议你学学struts标签
可以看看<<struts程序员查询辞典>>
awusoft 2007-09-11
  • 打赏
  • 举报
回复
"${shopcart.product.id}"===========>"${shopcart.id}"
awusoft 2007-09-11
  • 打赏
  • 举报
回复
<form method="post" action="update.do?id=${shopcar.id}" name="f1">
flyIce945 2007-09-11
  • 打赏
  • 举报
回复
这边是我写的取HashMap的c:forEach...
<%-- ########### 循环显示 ########### --%>
<c:forEach var="shopcart" items="${sessionScope.LIST}">
<tr>
<form method="post" action="update.do?id=${shopcart.product.id}" name="f1">
<input type="hidden" name="productid" value="2">
<input type="hidden" name="number" value="1">

<td class=tablebody2 valign=middle align=center width="">${shopcart.product.id}</td>
<input type="hidden" name="id" value="${shopcart.product.id}"/>
<td class=tablebody1 valign=middle width="">  
<a href="productDetail.jsp?productid=2">${shopcart.product.name}</a>
</td>
<td class=tablebody2 valign=middle align=center width="">${shopcart.product.basePrice}
</td>
<td class=tablebody1 valign=middle align=center width="">
<input type="text" name="num" value="${shopcart.num}"
size="4" onblur="javascript:if(this.value<1){alert('对不起,产品数量不能小于 1');
this.focus();}else{number.value=this.value;}" />
</td>
<td class=tablebody2 valign=middle align=left width="">  ${shopcart.product.basePrice*shopcart.num}
</td>
<td class=tablebody1 valign=middle align=center width="">
<input type="button" value="取消" onclick="javascript:window.location='delete.do?id=${shopcart.product.id}';">
<input type="submit" value="保存修改">
</td>
</form>
</tr>
</c:forEach>
######################
报错信息如下:
exception

javax.servlet.ServletException: Unable to find a value for "product" in object of class "java.util.HashMap$Entry" using operator "."
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.shopcart_jsp._jspService(org.apache.jsp.shopcart_jsp:257)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1063)
org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:386)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:229)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

flyIce945 2007-09-11
  • 打赏
  • 举报
回复
再问个小问题:怎么学习标签啊?

有什么资料不?下载啥来看看?
flyIce945 2007-09-11
  • 打赏
  • 举报
回复
楼上的说的对,我再试下,应该是要加.value...

<c:forEach items="${map}" var="item">
${item.key.name}-
${item.value}<br/>
</c:forEach>
liusx0113 2007-09-11
  • 打赏
  • 举报
回复
你的HashMap是这样的结构,Map<Integer, Shopcart> shopcarts=new HashMap<Integer, Shopcart>();
所以在<c:forEach var="shopcart" items="${sessionScope.LIST}">中,你想取出Map中的key,就用${shopcart.key},Map中的value用${shopcart.value}。当然,若value中是个POJO的话,可以用${shopcart.value.id},${shopcart.value.name}等来取出POJO相应的属性。不过你的好像应该用
${shopcart.value.product.name}.因为你的product是shopcart的一个属性。只是大概看了一下你的代码,不知道说的清楚不清楚,希望LZ能看明白。
awusoft 2007-09-11
  • 打赏
  • 举报
回复
晕,猜不到你的意思
flyIce945 2007-09-11
  • 打赏
  • 举报
回复
以下是我的ShopcartAction:
public class ShopcartAction extends MappingDispatchAction {
private int num=1;
private ShopcartBiz biz = (ShopcartBiz) BeanFactory.getBean("ShopcartBiz");
private ProductBiz productBiz= (ProductBiz)BeanFactory.getBean("ProductBiz");
private Product product;

public ActionForward toList(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {

Map shopcarts = biz.findAllShopcarts();
float sum=biz.getSumPrice();

HttpSession session=request.getSession();
session.setAttribute("LIST", shopcarts);
session.setAttribute("SUM", sum);
return mapping.findForward("to_list");
}
.................................
ShopcartBiz 是接口,实现类如下:
public class ShopcartBizImpl implements ShopcartBiz {
private Map<Integer, Shopcart> shopcarts=new HashMap<Integer, Shopcart>();
private float sumPrice=0;

public Map findAllShopcarts() {
return shopcarts;
}
...
后台打印显示程序应该没问题,主要是显示页面没拿到东西,估计是我的EL表达式写得不对...
flyIce945 2007-09-11
  • 打赏
  • 举报
回复
model是这样设计的:

public class Shopcart {
private Product product;
private int num;
...}

public class Product extends AbstractProduct implements java.io.Serializable {
// Constructors
/** default constructor */
public Product() {
}
/** minimal constructor */
public Product(String name, Float basePrice, String author, String publish) {
super(name, basePrice, author, publish);
}

/** full constructor */
public Product(Category category, String name, String description,
Float basePrice, String author, String publish, Integer pages,
String image, Set orderlines) {
super(category, name, description, basePrice, author, publish, pages,
image, orderlines);
}
}

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧