Action中ArrayList显示到JSP页面的具体实例
程序员文章站
2023-11-27 16:31:10
一、useraction中获取到的arraylist对象填充到userform中,jsp页面获取userform的初始值。useraction的部分代码:复制代码 代码如下...
一、useraction中获取到的arraylist对象填充到userform中,jsp页面获取userform的初始值。
useraction的部分代码:
复制代码 代码如下:
private actionforward executemanageaction(actionmapping mapping, actionform form,
httpservletrequest request, httpservletresponse response) {
userform userform = (userform)form;
arraylist userlist = new arraylist();
sessionfactory sf= new configuration().configure().buildsessionfactory();
session session=sf.opensession();
transaction tx=session.begintransaction();
string sqlquery="from user";
query lquery=session.createquery(sqlquery);
userlist=(arraylist)lquery.list();
tx.commit();
session.close();
userform.setuserlist(userlist);
return mapping.findforward("main_user");
}
usrform的部分代码:
复制代码 代码如下:
private arraylist userlist;
public arraylist getuserlist(){
return userlist;
}
public void setuserlist(arraylist userlist){
this.userlist=userlist;
}
jsp页面代码:
复制代码 代码如下:
<table id="id1" style="border-right: darkgreen 1px solid;border-top:darkgreen 1px solid;border-left: darkgreen 1px solid;width:100%;
border-bottom;darkgreen 1px solid;border-collapse:collapse" bordercolor="darkgreen" cellspacing="0" border="1">
<logic:notempty name="userform" property="userlist">
<tr nowrap="nowrap">
<td style="width:80px;height:16px" nowrap><b>用户名</b></td>
<td style="width:80px;height:16px" nowrap><b>角色</b></td>
<td style="width:84px;height:16px" ><b>姓名</b></td>
<td style="width:88px;height:16px" ><b>电话</b></td>
<td style="width:73px;height:16px" ><b>电子邮件</b></td>
<td style="width:273px;height:16px" ><b>动作</b></td>
</tr>
<logic:iterate indexid="index" id="user" name="userform" property="userlist">
<tr>
<td nowrap style="width:80px" ><bean:write name="user" property="username"/></td>
<td nowrap style="width:80px" ><bean:write name="user" property="role"/></td>
<td nowrap style="width:80px" ><bean:write name="user" property="name"/></td>
<td nowrap style="width:80px" ><bean:write name="user" property="tel"/></td>
<td nowrap style="width:80px" ><bean:write name="user" property="email"/></td>
<td nowrap sryle="width:273px" >
<a href="javascript:submitsid(document.fview,'<bean:write name="user" property="username"/>')">查看</a>
<font >||</font>
<a href="javascript:submitsid(document.fview,'<bean:write name="user" property="username"/>')">更新</a>
<font >||</font>
<a href="javascript:if (confirm('删除此用户么?')){ submitsid(document.fview,'<bean:write name="user" property="username"/>')}">删除</a>
</td></tr>
</logic:iterate>
</logic:notempty>
</table>
二、useraction中获取到数据arraylist对象后,把arraylist对象存在request中,jsp页面在获取到arraylist对象。
useraction部分代码:
复制代码 代码如下:
private actionforward executemanageaction(actionmapping mapping, actionform form,
httpservletrequest request, httpservletresponse response) {
userform userform = (userform)form;
arraylist userlist = new arraylist();
sessionfactory sf= new configuration().configure().buildsessionfactory();
session session=sf.opensession();
transaction tx=session.begintransaction();
string sqlquery="from user";
query lquery=session.createquery(sqlquery);
userlist=(arraylist)lquery.list();
tx.commit();
session.close();
request.setattribute("userlist", userlist);
return mapping.findforward("main_user");
}
jsp部分代码:
复制代码 代码如下:
<table id="id1" bordercolor="darkgreen" cellspacing="0" border="1">
<tr >
<td ><b>用户名</b></td>
<td ><b>角色</b></td>
<td ><b>姓名</b></td>
<td ><b>电话</b></td>
<td ><b>电子邮件</b></td>
<td ><b>动作</b></td>
</tr>
<logic:present name="userlist">
<logic:iterate indexid="index" id="user" name="userlist" >
<tr>
<td ><bean:write name="user" property="username"/></td>
<td ><bean:write name="user" property="role"/></td>
<td ><bean:write name="user" property="name"/></td>
<td ><bean:write name="user" property="tel"/></td>
<td ><bean:write name="user" property="email"/></td>
<td >
<a href="javascript:submitsid(document.fview,'<bean:write name="user" property="username"/>')">查看</a>
<font >||</font>
<a href="javascript:submitsid(document.fview,'<bean:write name="user" property="username"/>')">更新</a>
<font >||</font>
<a href="javascript:if (confirm('删除此用户么?')){ submitsid(document.fview,'<bean:write name="user" property="username"/>')}">删除</a>
</td></tr>
</logic:iterate>
</logic:present>
</table>
上一篇: ps初学者第九课 白话通道