问题
In my Java EE project I have the following Servlet:
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.naming.InitialContext;
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.sql.DataSource;
/**
* Servlet implementation class TitlePopulatorServlet
*/
@WebServlet("/TitlePopulatorServlet")
public class TitlePopulatorServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TitlePopulatorServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Inside doGet()!");
ArrayList<BeanTitle> bt=new ArrayList<BeanTitle>();
java.io.PrintWriter out = response.getWriter();
response.setContentType("text/html");
Connection conn=null;
try {
/* get the DataSource from using the JNDI name */
Class.forName("com.mysql.jdbc.Driver");
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/Test");
//Create connection and then continue as usual other JDBC calls
conn=ds.getConnection();
System.out.println("\nConnection Successful in TitlePopulatorServlet !");
Statement s= conn.createStatement();
ResultSet rs=s.executeQuery("SELECT * FROM story");
while (rs.next() )
{
BeanTitle b = new BeanTitle();
b.setBtitle(rs.getString(1));
bt.add(b);
}
} catch (Exception e){
out.println("Failed!"+ e);
}
request.setAttribute("bt", bt);
request.getRequestDispatcher("/StoryTitlePage.jsp").forward(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
The BeanTitle is as follows:
public class BeanTitle
{
private String btitle;
public String getBtitle() {
return btitle;
}
public void setBtitle(String btitle) {
this.btitle = btitle;
}
}
And the JSP page ("StoryTitlePage.jsp") is as follows:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id="storylist" class="serv.TitlePopulatorServlet" scope="request" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Story Title Page</title>
</head>
<body>
<form action="/ReportData/DisplayReport" method="post" >
Please select an element:
<select id="selectedRecord" name="selectedRecord">
<c:forEach var="item" items=${storylist.bt} >
<option>${item.tarr}</option>
</c:forEach>
</select>
<input type="submit" value="Submit">
</form>
</body>
</html>
But when I launch the TitlePopulatorServlet on the server it doesn't work. Where am I going wrong in the iteration/EL?
回答1:
The servlet store the list in the request, under the attribute named "bt". So it's already there, in the request. There is no need to do
<jsp:useBean id="storylist" class="serv.TitlePopulatorServlet" scope="request" />
That will only create a new instance of your servlet, which doesn't make any sense. and bt is a request attribute. It's not a property of the servlet. So using ${storylist.bt}
doesn't make any sense either.
You want to iterate over the elements of bt
, so all you need is
<c:forEach var="item" items="${bt}">
Finally, each item of bt
is of type BeanTitle
. And you're doing
${item.tarr}
This is equivalent to calling
beanTitle.getTarr()
And there is no tarr property in BeanTitle. The only property is btitle
(since the only getter of the class is named getBTitle()
).
So what you must use is
${item.btitle}
Your naming is awful, and that only makes you more confused. Why don't you use real words to name your classes and properties? Like for example
public class Book {
private String title;
public String getTitle()
...
}
来源:https://stackoverflow.com/questions/27474836/populating-a-drop-down-list-in-jsp-from-a-array-list-of-bean-type