这是我的程序,用于从servlet获取包含bean-object的ArrayList,并使用jstl标记在jsp中显示其内容。运行程序后,没有显示结果。请查看我的代码并帮助我找到错误。我是jstl的新手。
package com.servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.servlet.mybean;
@WebServlet("/Bean")
public class Bean extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Bean() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
mybean mybean_obj = new mybean();
ArrayList<mybean> obj=new ArrayList<mybean>();
mybean_obj.id=100;
mybean_obj.name="vishal";
mybean_obj.roll=1225;
mybean mybean_obj1 = new mybean();
mybean_obj1.id=101;
mybean_obj1.name="anand";
mybean_obj1.roll=1226;
mybean mybean_obj2 = new mybean();
mybean_obj2.id=102;
mybean_obj2.name="google";
mybean_obj2.roll=1557;
mybean mybean_obj3 = new mybean();
mybean_obj3.id=103;
mybean_obj3.name="yahoo";
mybean_obj3.roll=1558;
obj.add(mybean_obj);
obj.add(mybean_obj1);
obj.add(mybean_obj2);
obj.add(mybean_obj3);
HttpSession session = request.getSession();
session.setAttribute("mybean", obj);
request.setAttribute("mybean", obj);
RequestDispatcher fwd = request.getRequestDispatcher("encodeUrl.jsp");
fwd.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Encode URL </title>
</head>
<body>
<c:forEach var="start" items="${sessionScope.mybean}">
${sessionScope.start.id}<br/>${sessionScope.start.roll }<br/>${sessiontScope.start.name }<hr/>
</c:forEach>
</body>
</html>
我从主页调用servlet,从servlet调用请求转发到代码在上面的最终jsp页面。
答案 0 :(得分:2)
重写你的代码:
<c:forEach var="start" items="${sessionScope.mybean}">
${start.id}<br/>${start.roll }<br/>${start.name }<hr/>
</c:forEach>
var =“start”创建一个名为 start 的局部变量,表示当前项目。您可以在forEach语句中使用它。 (不需要使用sessionScope作为前缀,因为没有基于sessionScope的bean称为start)
您的代码的其他一些提示:
MyBean bean = new MyBean(); // classes start with an uppercase and use CamelCasing.
List<mybean> beans=new ArrayList<MyBean>(); // program against the list interface.
bean.setId(100); // use accessor methods instead of accessing variables directly.