STL的forEach标签和Struts的logic:iterate标签(解决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" name="list" type="vo.Student">//id为自定义的名字,name为Action中传过来的list,type为实体类,包括完整路径,这里是vo.Student
<tr>
<td><bean:write name="li" property="id"/></td>//name为逻辑名,和logic:iterate id="li"中的id对应,property为实体类中真正的属性
<td><bean:write name="li" property="name"/></td>
<td><a href="student.do?method=deleteStudent&id=<bean:write name="li" property="id"/>">删除</a></td>
</tr>
</logic:iterate>


</table>
<a href="student.jsp">返回</a>
</body>
</html>

在JSP页面引入Struts标签库的时候有所不同:

struts1.3的为:

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>或者<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值