基于struts2平台的例子
struts.xml配置
......
<action name="struts2" class="com.struts2.test.Vo" method="execute">
<result name="success">struts2.jsp</result>
<result name="failure">err.jsp</result>
</action>
......
ContentBo .java
package com.struts2.test;
import java.sql.SQLException;
import com.qs.data.*;//引入控件
public class ContentBo extends DbBase{
public ContentBo(){}
/**
* query
*/
public DbTable get(String sqls){
if("".equals(sqls)){
return null;
}
return new DbBase().executeQuery(sqls); //查询数据库
}
/**
* noquery
*/
public boolean set(String sqls){
try {
if( new DbBase().executeNonQuery(sqls)>=0 ){return true;}//执行非查询操作,如更新、删除等
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
ContentVo .java
package com.struts2.test;
import com.opensymphony.xwork2.ActionSupport;
import com.qs.data.*;//引入控件
public class ContentVo extends ActionSupport{
private static final long serialVersionUID = 1L;
ContentBo bo=new ContentBo();//定义数据层
DbRowCollection dtRow=new DbRowCollection();//定义行集合
DbColumnCollection dtCol=new DbColumnCollection();//定义列集合
public ContentVo(){}
public String execute(){
try{
DbTable dt=bo.get("select * from question");//获取数据层返回的虚拟表
setDtRow( dt.getRowCollection() );//从虚拟表加载行集合
setDtCol( dt.getColumnCollection() );//从虚拟表加载列集合
return "success";
}catch(Exception e){
System.out.println(e);
return "failure";
}
}
public DbRowCollection getDtRow() {
return dtRow;
}
public void setDtRow(DbRowCollection dtRow) {
this.dtRow = dtRow;
}
public DbColumnCollection getDtCol() {
return dtCol;
}
public void setDtCol(DbColumnCollection dtCol) {
this.dtCol = dtCol;
}
struts2.jsp
<!---行迭代--->
<table border="1">
<tr bgcolor="#eeeeee">
<td><s:property value="dtRow.getColName(0)" /></td><!-- 按索引给出列名 -->
<td><s:property value="dtRow.getColName(1)" /></td>
<td><s:property value="dtRow.getColName(2)" /></td>
<td><s:property value="dtRow.getColName(3)" /></td>
<td><s:property value="dtRow.getColName(4)" /></td>
<td>新增加的列对象</td>
</tr>
<s:iterator value="dtRow" status="sss">
<tr>
<td> <s:property value="get(0)" /> </td><!-- 按索引得到列值 -->
<td> <s:property value="get(1)" /> </td>
<td> <s:property value="get(2)" /> </td>
<td> <s:property value="get(3)" /> </td>
<td> <s:property value="get(4)" /> </td>
<td> <s:property value="新增加的列对象" /> </td><!-- 按列名得到列值 -->
</tr>
</s:iterator>
</table>
<!---列迭代--->
<s:property value=" dtCol.get(7).getColumnName() "/><BR><!-- 按索引给出列名 -->
<s:property value="dtCol.size()"/><BR><!-- 得到当前列集合具备多少列对象 -->
<s:iterator value=" dtCol.get(dtCol.get(7).getColumnName()) " var="DbColumn"><!-- 根据列名得到列对象(也可以根据列索引得到列对象),并取得列对象的值 -->
<s:property value="DbColumn" />
<BR>
</s:iterator>