打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

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

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

后台代码:

  1. private List<DeviceTypeAttribute> typeanames;  
  2. public String list() throws Exception {  
  3.     AccessDB db = new AccessDB();//打开数据库连接  
  4.     try {  
  5.         typeanames=new ArrayList<DeviceTypeAttribute>();  
  6.         List anames=db.query("from DeviceTypeAttribute where DeviceTypeID='"+deviceDeviceTypeId+"'");//查询特定类型设备的属性名称  
  7.         if(anames!=null && anames.size()>0){  
  8.             for(int i=0;i<anames.size();i++){  
  9.                 DeviceTypeAttribute aname=(DeviceTypeAttribute)anames.get(i);  
  10.                 DeviceTypeAttribute anamevo=new DeviceTypeAttribute();  
  11.                 anamevo.setDeviceTypeAttributeName(aname.getDeviceTypeAttributeName());  
  12.                 typeanames.add(anamevo);//将结果进行封装,得到结果集  
  13.                   
  14.             }  
  15.         }  
  16.         return SUCCESS;//返回到页面  
  17.     } catch (Exception e) {  
  18.         // TODO: handle exception  
  19.         e.printStackTrace();  
  20.         return ERROR;  
  21.     } finally {  
  22.         // 释放数据库连接  
  23.         db.close();  
  24.     }  
  25. }  
  26. public List<DeviceTypeAttribute> getTypeanames() {  
  27.     return typeanames;  
  28. }  
  29. public void setTypeanames(List<DeviceTypeAttribute> typeanames) {  
  30.     this.typeanames = typeanames;  
  31. }  

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

前台代码:

  1. <span style="font-size:12px;"><s:iterator value="typeanames">  
  2.     <td>                         
  3.     <s:property value="%{DeviceTypeAttributeName}" />  
  4.     </td>  
  5. </s:iterator>  
  6. <s:if test="typeanames==null || typeanames.size() == 0">  
  7. <tfoot>  
  8.     <tr>  
  9.         <td colspan="8" class="noRecord">  
  10.             没有相关记录。  
  11.         </td>  
  12.     </tr>  
  13. </tfoot>  
  14. </s:if></span>  

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

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

  1. public List<DeviceTypeAttribute> getDeviceTypeanames() {  
  2.     return typeanames;  
  3. }  
  4. public void setDeviceTypeanames(List<DeviceTypeAttribute> typeanames) {  
  5.     this.typeanames = typeanames;  
  6. }  

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

  1. public List<DeviceTypeAttribute> getTypeanames() {  
  2.     return typeanames;  
  3. }  
  4. public void setTypeanames(List<DeviceTypeAttribute> typeanames) {  
  5.     this.typeanames = typeanames;  
  6. }  

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



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
关于play framework的几个问题
Java总结篇系列:Java多线程(二)
运行第一个java程序“hello world”
一杯浓茶.八味人生
前台与后台 37'07''
最简单Java窗口程序
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服