您的位置:首页 > 编程语言 > Java开发

练习使用struts中的iterator标签对集合进行迭代

2015-12-15 20:59 411 查看
最近尝试了在struts2框架下,当从数据库中查询的结果不只一个的时候,用iterator标签对查询结果的集合进行迭代,从而将查询结果返回到界面显示。部分代码显示如下:

这是action类:

public class UserAction extends ActionSupport{
public String execute() throws Exception{
UserDao ud = new UserDao();
List<User> list = ud.checkUser();
if(!list.isEmpty()){
Map session = ActionContext.getContext().getSession();
session.put("list", list);
return SUCCESS;
}else{
return ERROR;
}
}
}


这是action类对应的struts配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="userAction" class="org.action.UserAction">
<result name="success">/result.jsp</result>
<result name="error">/index.jsp</result>
</action>
</package>
<constant name="struts.i18n.encoding" value="utf-8"></constant>
</struts>这是输出结果的界面代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>结果</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
所有账号信息如下:<br />
<table>
<s:iterator value="#session['list']" id="user" status="li">
<tr>
<td><s:property value="#user.getUserId()"/></td>
<td><s:property value="#user.getUserPassword()"/></td>
</tr>
</s:iterator>
</table>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struts2.0 iterator