如何从后台传数据到前台显示
这不是什么很难的问题,只是我接触java,struts2,hibernate不久,所以这里遇到了很大问题,经过一天半的艰辛测试,终于实现了功能,所以肯定要留个脚印了。目标:在后台从java中查询数据库,查出结果集,然后通过struts2传送的前台,并在jsp页面上显示。
后台代码:
<li class="alt"><span><span class="keyword">private</span><span> List<DeviceTypeAttribute> typeanames; </span></span></li><li class=""><span><span class="keyword">public</span><span> String list() </span><span class="keyword">throws</span><span> Exception { </span></span></li><li class="alt"><span> AccessDB db = <span class="keyword">new</span><span> AccessDB();</span><span class="comment">//打开数据库连接</span><span> </span></span></li><li class=""><span> <span class="keyword">try</span><span> { </span></span></li><li class="alt"><span> typeanames=<span class="keyword">new</span><span> ArrayList<DeviceTypeAttribute>(); </span></span></li><li class=""><span> List anames=db.query(<span class="string">"from DeviceTypeAttribute where DeviceTypeID='"</span><span>+deviceDeviceTypeId+</span><span class="string">"'"</span><span>);</span><span class="comment">//查询特定类型设备的属性名称</span><span> </span></span></li><li class="alt"><span> <span class="keyword">if</span><span>(anames!=</span><span class="keyword">null</span><span> && anames.size()></span><span class="number">0</span><span>){ </span></span></li><li class=""><span> <span class="keyword">for</span><span>(</span><span class="keyword">int</span><span> i=</span><span class="number">0</span><span>;i<anames.size();i++){ </span></span></li><li class="alt"><span> DeviceTypeAttribute aname=(DeviceTypeAttribute)anames.get(i); </span></li><li class=""><span> DeviceTypeAttribute anamevo=<span class="keyword">new</span><span> DeviceTypeAttribute(); </span></span></li><li class="alt"><span> anamevo.setDeviceTypeAttributeName(aname.getDeviceTypeAttributeName()); </span></li><li class=""><span> typeanames.add(anamevo);<span class="comment">//将结果进行封装,得到结果集</span><span> </span></span></li><li class="alt"><span> </span></li><li class=""><span> } </span></li><li class="alt"><span> } </span></li><li class=""><span> <span class="keyword">return</span><span> SUCCESS;</span><span class="comment">//返回到页面</span><span> </span></span></li><li class="alt"><span> } <span class="keyword">catch</span><span> (Exception e) { </span></span></li><li class=""><span> <span class="comment">// TODO: handle exception</span><span> </span></span></li><li class="alt"><span> e.printStackTrace(); </span></li><li class=""><span> <span class="keyword">return</span><span> ERROR; </span></span></li><li class="alt"><span> } <span class="keyword">finally</span><span> { </span></span></li><li class=""><span> <span class="comment">// 释放数据库连接</span><span> </span></span></li><li class="alt"><span> db.close(); </span></li><li class=""><span> } </span></li><li class="alt"><span>} </span></li><li class=""><span><span class="keyword">public</span><span> List<DeviceTypeAttribute> getTypeanames() { </span></span></li><li class="alt"><span> <span class="keyword">return</span><span> typeanames; </span></span></li><li class=""><span>} </span></li><li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">void</span><span> setTypeanames(List<DeviceTypeAttribute> typeanames) { </span></span></li><li class=""><span> <span class="keyword">this</span><span>.typeanames = typeanames; </span></span></li><li class="alt"><span>} </span></li>
上面的代码不全,结果集DeviceTypeAttribute的结构肯定也要另外写的。
前台代码:
<li class="alt"><span><span class="tag"><</span><span class="tag-name">span</span><span> </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> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"typeanames"</span><span class="tag">></span><span> </span></span></li><li class=""><span> <span class="tag"><</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li><li class="alt"><span> <span class="tag"><</span><span class="tag-name">s:property</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"%{DeviceTypeAttributeName}"</span><span> </span><span class="tag">/></span><span> </span></span></li><li class=""><span> <span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li><li class="alt"><span><span class="tag"></</span><span class="tag-name">s:iterator</span><span class="tag">></span><span> </span></span></li><li class=""><span><span class="tag"><</span><span class="tag-name">s:if</span><span> </span><span class="attribute">test</span><span>=</span><span class="attribute-value">"typeanames==null || typeanames.size() == 0"</span><span class="tag">></span><span> </span></span></li><li class="alt"><span><span class="tag"><</span><span class="tag-name">tfoot</span><span class="tag">></span><span> </span></span></li><li class=""><span> <span class="tag"><</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li><li class="alt"><span> <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">colspan</span><span>=</span><span class="attribute-value">"8"</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"noRecord"</span><span class="tag">></span><span> </span></span></li><li class=""><span> 没有相关记录。 </span></li><li class="alt"><span> <span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li><li class=""><span> <span class="tag"></</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li><li class="alt"><span><span class="tag"></</span><span class="tag-name">tfoot</span><span class="tag">></span><span> </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> </span></span></li>
set和get的名字必须和结果集的名称一样才能够取到,正确代码如下:
public List<DeviceTypeAttribute> getTypeanames() {
return typeanames;
}
public void setTypeanames(List<DeviceTypeAttribute> typeanames) {
this.typeanames = typeanames;
}
这个内容也算最常用的,现在终于掌握住了要点,嘿嘿,功夫不负有心人! 支持中国红客联盟(ihonker.org) 代码编辑的好乱啊,有些字符被转义了吧 支持中国红客联盟(ihonker.org) 支持中国红客联盟(ihonker.org) 支持中国红客联盟(ihonker.org) 支持中国红客联盟(ihonker.org) 支持,看起来还是可以的 支持中国红客联盟(ihonker.org) 谢谢楼主的分享