迭代取值

基于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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值