如何从后台传数据到前台显示

       这不是什么很难的问题,只是我接触java,struts2,hibernate不久,所以这里遇到了很大问题,经过一天半的艰辛测试,终于实现了功能,所以肯定要留个脚印了。

目标:在后台从java中查询数据库,查出结果集,然后通过struts2传送的前台,并在jsp页面上显示。

后台代码:

	private List<DeviceTypeAttribute> typeanames;
	public String list() throws Exception {
		AccessDB db = new AccessDB();//打开数据库连接
		try {
			typeanames=new ArrayList<DeviceTypeAttribute>();
			List anames=db.query("from DeviceTypeAttribute where DeviceTypeID='"+deviceDeviceTypeId+"'");//查询特定类型设备的属性名称
			if(anames!=null && anames.size()>0){
				for(int i=0;i<anames.size();i++){
					DeviceTypeAttribute aname=(DeviceTypeAttribute)anames.get(i);
					DeviceTypeAttribute anamevo=new DeviceTypeAttribute();
					anamevo.setDeviceTypeAttributeName(aname.getDeviceTypeAttributeName());
					typeanames.add(anamevo);//将结果进行封装,得到结果集
					
				}
			}
			return SUCCESS;//返回到页面
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ERROR;
		} finally {
			// 释放数据库连接
			db.close();
		}
	}
	public List<DeviceTypeAttribute> getTypeanames() {
		return typeanames;
	}
	public void setTypeanames(List<DeviceTypeAttribute> typeanames) {
		this.typeanames = typeanames;
	}

上面的代码不全,结果集DeviceTypeAttribute的结构肯定也要另外写的。

前台代码:

<s:iterator value="typeanames">
	<td>						 
	<s:property value="%{DeviceTypeAttributeName}" />
	</td>
</s:iterator>
<s:if test="typeanames==null || typeanames.size() == 0">
<tfoot>
	<tr>
		<td colspan="8" class="noRecord">
			没有相关记录。
		</td>
	</tr>
</tfoot>
</s:if>

简单写一下,遍历结果集即可。

错误原因:前前后后错误不下40次,感觉快要崩溃了,总是后台能够查到数据,但是前台取不到。最后终于找到错误原因,关键在下面这段代码,我以前写的是:

	public List<DeviceTypeAttribute> getDeviceTypeanames() {
		return typeanames;
	}
	public void setDeviceTypeanames(List<DeviceTypeAttribute> typeanames) {
		this.typeanames = typeanames;
	}

这样就取不到,set和get的名字必须和结果集的名称一样才能够取到,正确代码如下:

	public List<DeviceTypeAttribute> getTypeanames() {
		return typeanames;
	}
	public void setTypeanames(List<DeviceTypeAttribute> typeanames) {
		this.typeanames = typeanames;
	}

这个内容也算最常用的,现在终于掌握住了要点,嘿嘿,功夫不负有心人!



  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值