<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
<%@page import="com.star.mode.Person"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
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 'index.jsp' starting page</title>
</head> <body>
<%
List<Person> list1 = new ArrayList<Person>();
Person p1 = new Person();
p1.setAge(10);
p1.setName("张三"); Person p2 = new Person();
p2.setAge(20);
p2.setName("李四"); Person p3 = new Person();
p3.setAge(30);
p3.setName("王五"); list1.add(p1);
list1.add(p2);
list1.add(p3); request.setAttribute("all", list1);
%>
<br>
<h1>
${all[0]}
</h1>
<c:if test="${all != null}">
<c:forEach var="person" items="${all}">
${person.age}<br/>
${person.name}<br/>
</c:forEach>
</c:if>
</body>
</html>
<%@page import="java.util.*"%>
<%@page import="com.star.mode.Person"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
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 'index.jsp' starting page</title>
</head> <body>
<%
List<Person> list1 = new ArrayList<Person>();
Person p1 = new Person();
p1.setAge(10);
p1.setName("张三"); Person p2 = new Person();
p2.setAge(20);
p2.setName("李四"); Person p3 = new Person();
p3.setAge(30);
p3.setName("王五"); list1.add(p1);
list1.add(p2);
list1.add(p3); request.setAttribute("all", list1);
%>
<br>
<h1>
${all[0]}
</h1>
<c:if test="${all != null}">
<c:forEach var="person" items="${all}">
${person.age}<br/>
${person.name}<br/>
</c:forEach>
</c:if>
</body>
</html>
如果你使用action或servlet 可以在其中调用A类
A a=new A();
List list=a.getList();
request.setAttribute("list",list);jsp中
<c:foreach var="a 变量名(可以看成是list中保存的对象名)" items="${list}">
<tr>
<td>${(a.属性)或(a)}</td>
</tr>
</c:foreach>
我就是不想用那种方法...
也不可以用ajax读取...
还有没有其他方法
<%
List <Person> list1 = new ArrayList <Person>();
Person p1 = new Person();
p1.setAge(10);
p1.setName("张三"); Person p2 = new Person();
p2.setAge(20);
p2.setName("李四"); Person p3 = new Person();
p3.setAge(30);
p3.setName("王五"); list1.add(p1);
list1.add(p2);
list1.add(p3); request.setAttribute("all", list1);
%>
这部分代码移到servlet中,如果用struts就移到action中,只要将list保存到一定的范围,比如request,再forward回jsp页面,在jsp页面就可以这样取了:
${all[0]}
<c:if test="${all != null}">
<c:forEach var="person" items="${all}">
${person.age} <br/>
${person.name} <br/>
</c:forEach>
</c:if>