yhy 发表于 2016-3-24 09:11:05

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

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

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

后台代码:
<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的结构肯定也要另外写的。

前台代码:
<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的名字必须和结果集的名称一样才能够取到,正确代码如下:

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


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

菜鸟小羽 发表于 2016-3-24 09:31:51

支持中国红客联盟(ihonker.org)

wuyan 发表于 2016-3-24 10:31:22

代码编辑的好乱啊,有些字符被转义了吧

H.U.C—Prince 发表于 2016-3-24 13:04:00

支持中国红客联盟(ihonker.org)

admin1964 发表于 2016-3-24 15:04:06

支持中国红客联盟(ihonker.org)

Sty,涛 发表于 2016-3-24 19:00:04

支持中国红客联盟(ihonker.org)

ljy07 发表于 2016-3-24 19:57:53

支持中国红客联盟(ihonker.org)

菜鸟小羽 发表于 2016-3-24 23:56:52

支持,看起来还是可以的

wanmznh 发表于 2016-3-25 10:51:06

支持中国红客联盟(ihonker.org)

wtsqq123 发表于 2016-3-25 11:30:59

谢谢楼主的分享
页: [1] 2 3 4 5 6 7 8 9 10
查看完整版本: 如何从后台传数据到前台显示