SSH系统中,数据库mysql,jsp表示使用struts标签。两个表,一个活动类型表,一个活动文章表。现在要列出所有的活动文章,列出所有的活动文章比较容易,但要把每条列出来的文章属于哪个活动栏目的名称给列出来。具体不知道如何是好。活动文章表。表明sale,建表语句如下create table sale
(
id int auto_increment not null,
title varchar(100) not null,
hdid int,
primary Key (id)
);
活动类型表,表明hd,建表如下
create table hd
(
id int auto_increment not null,
title varchar(100) not null,
primary Key (id)
);com.wj.vo包内两个java文件。 分别是Hd.java与Sale.javaHd.java内容如下package com.wj.vo;
import java.util.Date;
import java.util.Set;
public class Hd {
private int id;
private String title;
private Set sales;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
} public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
} public Set getSales() {
return sales;
}
public void setSales(Set sales) {
this.sales = sales;
}
}Sale.java内容如下
package com.wj.vo;
import java.util.Date;
public class Sale {
private int id;
private String title;
private int hdid;
private Hd hd; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
} public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getHdid() {
return hdid;
}
public void setHdid(int hdid) {
this.hdid = hdid;
}
public Hd getHd() {
return hd;
}
public void setHd(Hd hd) {
this.hd = hd;
}
}com.wj.dao.impl包内IHdDAOImpl内容如下
public Hd queryByHd() throws Exception { Hd hd = null;
String hql = "FROM Hd AS a";
Query q = super.getSession().createQuery(hql);
List all = q.list();
if(all.size()>0){
hd = (Hd) all.get(0);
System.out.println(hd.getId());
}
return hd;
}com.wj.struts.action包内的SaleAction.java文件部分内容如下 public ActionForward list(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
try { request.setAttribute("hd", this.ihddao.queryByHd());
} catch (Exception e) {
e.printStackTrace();
}
return mapping.findForward("list");
} salelist.jsp 列出所有活动列表程序如下 <logic:iterate id="sale" name="hd" scope="request" property="sales">
<b>${hd.title }</b>---- ${sale.title }<br>
<br>
</logic:iterate>所有关键代码到此结束。只能正常显示一个栏目内的所有活动信息。怎样做,才能显示所有栏目的所有活动信息呢?也就是显示出所有的活动信息,列表中同时体现活动信息所在活动栏目的栏目名称!!!
(
id int auto_increment not null,
title varchar(100) not null,
hdid int,
primary Key (id)
);
活动类型表,表明hd,建表如下
create table hd
(
id int auto_increment not null,
title varchar(100) not null,
primary Key (id)
);com.wj.vo包内两个java文件。 分别是Hd.java与Sale.javaHd.java内容如下package com.wj.vo;
import java.util.Date;
import java.util.Set;
public class Hd {
private int id;
private String title;
private Set sales;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
} public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
} public Set getSales() {
return sales;
}
public void setSales(Set sales) {
this.sales = sales;
}
}Sale.java内容如下
package com.wj.vo;
import java.util.Date;
public class Sale {
private int id;
private String title;
private int hdid;
private Hd hd; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
} public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getHdid() {
return hdid;
}
public void setHdid(int hdid) {
this.hdid = hdid;
}
public Hd getHd() {
return hd;
}
public void setHd(Hd hd) {
this.hd = hd;
}
}com.wj.dao.impl包内IHdDAOImpl内容如下
public Hd queryByHd() throws Exception { Hd hd = null;
String hql = "FROM Hd AS a";
Query q = super.getSession().createQuery(hql);
List all = q.list();
if(all.size()>0){
hd = (Hd) all.get(0);
System.out.println(hd.getId());
}
return hd;
}com.wj.struts.action包内的SaleAction.java文件部分内容如下 public ActionForward list(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
try { request.setAttribute("hd", this.ihddao.queryByHd());
} catch (Exception e) {
e.printStackTrace();
}
return mapping.findForward("list");
} salelist.jsp 列出所有活动列表程序如下 <logic:iterate id="sale" name="hd" scope="request" property="sales">
<b>${hd.title }</b>---- ${sale.title }<br>
<br>
</logic:iterate>所有关键代码到此结束。只能正常显示一个栏目内的所有活动信息。怎样做,才能显示所有栏目的所有活动信息呢?也就是显示出所有的活动信息,列表中同时体现活动信息所在活动栏目的栏目名称!!!
解决方案 »
- AXIS2 发送对象问题
- 关于dwr的调用类
- java API 解压缩 程序问题
- If this is a valid DTD ? Thanks in advance.
- 帮帮忙,生成dom树发生Unhandled event loop exception解决不了
- 寻找空间,服务器,求助
- tomcat下部署多个应用导致cpu使用到达100%的情况如何解决
- 怎样在xsl文件中使用+、-、*、/、%等运算????请举些例子。
- 重金寻求答案
- 求助,mysql Database access problem. Killing off all remaining connections
- 整合struts1系统与strut2系统
- oracle中long数据插入过长。
hd = (Hd) all.get(0);
System.out.println(hd.getId());
}
首先,你的这段代码只会执行一次,因为你用的是if,用for循环即可
for(Iteratoritr = all.iterator(); itr.hasNext();){
Hd hd=itr.next();
System.out.println(hd.getId());
}
while(it.hasNext()){
hd = (Hd)it.next();
System.out.println(hd.getId());
}
但返回的hd,在salelist.jsp 页面中,还是只能显示一个栏目下的内容。
估计是否是hd的值被重复覆盖了。
或者通过查hd表,然后同过便利hd.sales拿到所有该活动栏目显得所有sale。当然以上要在hibernate的关系中配置正确
Hd.hbm.xml内容如下<class name="com.wj.vo.Hd" table="hd">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native"></generator>
</id>
<set name="sales" inverse="true" cascade="all"
table="sale" order-by="id">
<key>
<column name="hdid" />
</key>
<one-to-many class="com.wj.vo.Sale" />
</set>
<property name="title" type="java.lang.String">
<column name="title" length="100" not-null="true" />
</property> </class>Sale.hbm.xml内容如下 <class name="com.wj.vo.Sale" table="sale">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native"></generator>
</id>
<property name="title" type="java.lang.String">
<column name="title" length="100" not-null="true" />
</property> <many-to-one name="hd" class="com.wj.vo.Hd">
<column name="hdid" />
</many-to-one> </class>你看看,所有代码都在这儿了!至于你说的首先显示出hd的title,然后根据一对多关系取出他包含的sale的集合,打印出标题就可以了,这之后的步骤怎么做,请名示一下
all = this.iarticledao.queryAll(currentPage, lineSize, 1, 0); 并且在其中的代码内都使用了关闭。
public int getAllCount(int int1, int int2) throws Exception {
int count = 0;
String hql = null; hql = "SELECT COUNT(a.id) FROM Article AS a WHERE a.status=? and a.deleted=?"; Query q = super.getSession().createQuery(hql);
q.setInteger(0, int1);
q.setInteger(1, int2);
count = (Integer) q.list().get(0);
super.getSession().close();
return count;
} public List queryAll(int currentPage, int lineSize, int int1, int int2) throws Exception {
String hql = null;
hql = "FROM Article AS a WHERE a.status=? and a.deleted=? order by id desc";
Query q = super.getSession().createQuery(hql);
q.setInteger(0, int1);
q.setInteger(1, int2); q.setFirstResult((currentPage - 1) * lineSize);
q.setMaxResults(lineSize); List all = q.list();
super.getSession().close();
return all;
}但是我频繁点击还是出现如下错误2009-08-01 17:22:23,875 WARN [org.hibernate.jdbc.ConnectionManager] - finalizing with closed connection
2009-08-01 17:22:29,468 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 0, SQLState: null
2009-08-01 17:22:29,468 ERROR [org.hibernate.util.JDBCExceptionReporter] - Cannot get a connection, pool exhausted
2009-08-01 17:22:29,468 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 0, SQLState: null
2009-08-01 17:22:29,468 ERROR [org.hibernate.util.JDBCExceptionReporter] - Cannot get a connection, pool exhausted
org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:301)
主要是从sale内一次性读取数据,不应该跟先读栏目有关的!所有代码都在上面了,高人看着帮我更改一下吧