Hello everybody,
I worked long hours this evening trying to unthread the whole thing and still not working... I don't know why. The page display the results using normal code in JSP (no jstl tags). When using jstl tags the page results in blank screen, no posts. Here is the code:
index.jsp (jstl not working)
<%@ page import="bean.PostBean"%>
<%@ page import="model.Posts" %>
<%@ page import="java.util.*" %>
<jsp:useBean id="Posts" class="model.Posts" scope="page" />
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Report Results (NIOSH)</title>
</head>
<body>
<% ArrayList<PostBean> posts = new ArrayList<PostBean>(Posts.getPosts()); %>
<% //System.out.println(posts); %>
<c:forEach var="post" items="${posts}">
<h2>${post.title}</h2>
<p>${post.text}</p>
</c:forEach>
</body>
</html>
index.jsp (works)
<%@ page import="bean.PostBean"%>
<%@ page import="model.Posts" %>
<%@ page import="java.util.*" %>
<jsp:useBean id="Posts" class="model.Posts" scope="page" />
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Report Results (NIOSH)</title>
</head>
<body>
<%
ArrayList<PostBean> posts = Posts.getPosts();
%>
<%for(int i=0; i < posts.size(); i++)
{
PostBean post = new PostBean();
post = (PostBean) posts.get(i);
%>
<h2><%= post.getTitle() %></h2>
<p><%= post.getText() %></p>
<%}%>
</body>
</html>
Posts.java
import java.sql.Statement;
import java.util.ArrayList;
import bean.PostBean;
public class Posts {
public ArrayList<PostBean> getPosts() throws Exception{
ArrayList<PostBean> list = new ArrayList<PostBean>();
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String password = "xx";
Class.forName(driver);
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dcaminoa", user, password);
Statement select = (Statement) conn.createStatement();
ResultSet result = select.executeQuery("select * from posts");
while(result.next())
{
PostBean post = new PostBean();
post.setId(result.getInt("id"));
post.setTitle(result.getString("title"));
post.setText(result.getString("text"));
list.add(post);
}
result.close();
conn.close();
return list;
}
}
PostBean.java
package bean;
public class PostBean {
private int id;
private String title;
private String text;
public PostBean() {}
public int getId() {
return id;
}
public void setId(int num) {
id = num;
}
public String getTitle() {
return title;
}
public void setTitle(String str) {
title = str;
}
public String getText() {
return text;
}
public void setText(String str) {
text = str;
}
}