请选择 进入手机版 | 继续访问电脑版
查看: 8585|回复: 2488

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

[复制链接]
  • TA的每日心情

    2019-9-4 09:40
  • 签到天数: 107 天

    [LV.6]常住居民II

    发表于 2016-3-24 09:11:05 | 显示全部楼层 |阅读模式
    这不是什么很难的问题,只是我接触java,struts2,hibernate不久,所以这里遇到了很大问题,经过一天半的艰辛测试,终于实现了功能,所以肯定要留个脚印了。

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

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




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

    前台代码:
    [HTML] 纯文本查看 复制代码
    <li class="alt"><span><span class="tag"><</span><span class="tag-name">span</span><span>&nbsp;</span><span class="attribute">style</span><span>=</span><span class="attribute-value">"font-size:12px;"</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">s:iterator</span><span>&nbsp;</span><span class="attribute">value</span><span>=</span><span class="attribute-value">"typeanames"</span><span class="tag">></span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag"><</span><span class="tag-name">td</span><span class="tag">></span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag"><</span><span class="tag-name">s:property</span><span>&nbsp;</span><span class="attribute">value</span><span>=</span><span class="attribute-value">"%{DeviceTypeAttributeName}"</span><span>&nbsp;</span><span class="tag">/></span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="tag"></</span><span class="tag-name">s:iterator</span><span class="tag">></span><span>&nbsp;&nbsp;</span></span></li><li class=""><span><span class="tag"><</span><span class="tag-name">s:if</span><span>&nbsp;</span><span class="attribute">test</span><span>=</span><span class="attribute-value">"typeanames==null&nbsp;||&nbsp;typeanames.size()&nbsp;==&nbsp;0"</span><span class="tag">></span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="tag"><</span><span class="tag-name">tfoot</span><span class="tag">></span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag"><</span><span class="tag-name">tr</span><span class="tag">></span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag"><</span><span class="tag-name">td</span><span>&nbsp;</span><span class="attribute">colspan</span><span>=</span><span class="attribute-value">"8"</span><span>&nbsp;</span><span class="attribute">class</span><span>=</span><span class="attribute-value">"noRecord"</span><span class="tag">></span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;没有相关记录。&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag"></</span><span class="tag-name">tr</span><span class="tag">></span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="tag"></</span><span class="tag-name">tfoot</span><span class="tag">></span><span>&nbsp;&nbsp;</span></span></li><li class=""><span><span class="tag"></</span><span class="tag-name">s:if</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">span</span><span class="tag">></span><span>&nbsp;&nbsp;</span></span></li>


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

    [Java] 纯文本查看 复制代码
        public List<DeviceTypeAttribute> getTypeanames() {  
            return typeanames;  
        }  
        public void setTypeanames(List<DeviceTypeAttribute> typeanames) {  
            this.typeanames = typeanames;  
        }  



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

    使用道具 举报

  • TA的每日心情

    2015-10-24 10:52
  • 签到天数: 7 天

    [LV.3]偶尔看看II

    发表于 2016-3-24 09:31:51 | 显示全部楼层
    支持中国红客联盟(ihonker.org)
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    无聊
    2017-9-11 15:40
  • 签到天数: 425 天

    [LV.9]以坛为家II

    发表于 2016-3-24 10:31:22 | 显示全部楼层
    代码编辑的好乱啊,有些字符被转义了吧
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    郁闷
    2016-4-13 21:38
  • 签到天数: 1 天

    [LV.1]初来乍到

    发表于 2016-3-24 13:04:00 | 显示全部楼层
    支持中国红客联盟(ihonker.org)
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    发表于 2016-3-24 15:04:06 | 显示全部楼层
    支持中国红客联盟(ihonker.org)
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    发表于 2016-3-24 19:00:04 | 显示全部楼层
    支持中国红客联盟(ihonker.org)
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2015-6-21 22:12
  • 签到天数: 1 天

    [LV.1]初来乍到

    发表于 2016-3-24 19:57:53 | 显示全部楼层
    支持中国红客联盟(ihonker.org)
    回复 支持 反对

    使用道具 举报

  • TA的每日心情

    2015-10-24 10:52
  • 签到天数: 7 天

    [LV.3]偶尔看看II

    发表于 2016-3-24 23:56:52 | 显示全部楼层
    支持,看起来还是可以的
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2020-1-10 03:16
  • 签到天数: 1 天

    [LV.1]初来乍到

    发表于 2016-3-25 10:51:06 | 显示全部楼层
    支持中国红客联盟(ihonker.org)
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    发表于 2016-3-25 11:30:59 | 显示全部楼层
    谢谢楼主的分享
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    快速回复 返回顶部 返回列表