当前位置:编程学习 > JAVA >>

HQL一对多关联查询怎么在JSP页面显示?

产品表:
public class Chanpin {

private int id;
private String chanpinmingcheng;
private String chanpinxinghao;
private String shengchanchangjia;
private String chanpinleibie;
.......
private Set<Ziliao> ziliao = new HashSet<Ziliao>();
}
资料表:
public class Ziliao {

private int id;
private byte[] shuju;
private String ziliaoleixing;
private String wenjianming;
private String wenjianleixing;
private Date shangchuanshijian;
private String shangchuanren;
private String lujing;
private String xinwenjianming;
private Chanpin chanpin;
}
HQL查询语句:
public List findCpList() {
// TODO Auto-generated method stub
String sql = "select c.chanpinmingcheng,c.chanpingongneng,c.id,z.lujing from Chanpin c left join c.ziliao z where c.id is not null";
return chanpinDao.findList(sql);
}
strut2 action代码:
public String chaxun() throws Exception {
session.put("cpList", chanpinService.findCpList());
return "chaxun";
}
JSP代码:
(我写的运行不正确)
<s:iterator value="#session.cpList" var="cpList" id="cpList">
<tr>
<td align="left">
<div>
<img src=""
style="width: 160px;height: 120px; float: left;margin: 10px;border: 1px solid #eeeeee;"
alt="暂无图片">
<p class="wenzi-biaoti-red">
<a
href="chanpin/chanpin_xianshi.action?chanpin.id=<s:property
value="#cpList.id" />"
style="color: #f2392b;"><s:property
value="#cpList.chanpinmingcheng" /> </a>
</p>
<p>
<s:property value="#cpList.chanpingongneng" escapeHtml="false" />
</p>
</div> <br /></td>
</tr>
</s:iterator> --------------------编程问答-------------------- 配置上:web.xml开易做图

<!--Hibernate Open Session in View Filter-->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>


后台,struts的,调用你的query将结果塞到list里面,如果用下面的读取方法,那就将这个list塞到request下面,没必要放session里面啦,至于怎么获取request你应该知道~


request.setAttribute("cpList", chanpinService.findCpList());



jsp页面,我这边没用struts的ognl,用jstl加el:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<c:if test="${not empty cpList}">
  <c:forEach var="obj" items="${cpList}">
<p>${obj}</p><br/>
  </c:forEach>
</c:if>


大概这样~
补充:Java ,  Web 开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,