JSTL的forEach标签和Struts的logic:iterate标签
时间:2011-01-22 BlogJava BBT_soft
用于解决Action中request.setAttribute("list",list)
由于我用的是struts框架,就拿整个项目介绍:
1.首先把jstl的两个常用包jstl.jar、standard.jar加载到环境中
2.Action代码:(整个过程不需要了解,这儿方法就是返回一个封装Students对象的list,然后request.setAttribute("list",list)起来)
public ActionForward selectStudent(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) {
StudentForm studentForm = (StudentForm) form;
DBConnection dbconn = new DBConnection();
Connection conn = dbconn.getConnection();
StudentServiceFactory serviceFactory = new StudentServiceFactory();
List list = serviceFactory.getStudentService().selectStudent(conn);
request.setAttribute("list",list);
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return mapping.findForward("show");
}
3.show.jsp页面:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>//这三句很重要
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%
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>My JSP ''show.jsp'' starting page</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>
<tr>
<td>ID</td>
<td>姓名</td>
</tr>
<c:forEach items="${list}" var="student">// items为list的一个迭代器,list为Action中传递过来的list,student为Student类对象
<tr>
<td>${student.id }</td>//输出student的id属性
<td>${student.name }</td>//输出student的name属性
</tr>
</c:forEach>
<logic:iterate id="li&qu
|