WEB开发网
开发学院软件开发Java JSTL的forEach标签和Struts的logic:iterate标签(... 阅读

JSTL的forEach标签和Struts的logic:iterate标签(解决Action中request.setAttribute("list",list))

 2009-09-24 00:00:00 来源:WEB开发网   
核心提示:由于我用的是struts框架,就拿整个项目介绍:1.首先把jstl的两个常用包jstl.jar、standard.jar加载到环境中2.Action代码:(整个过程不需要了解,JSTL的forEach标签和Struts的logic:iterate标签(解决Action中request.setAttribute(&quo

由于我用的是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" %>

Tags:JSTL forEach 标签

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接