We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,039 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

jstl not working

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;
	}

}
2
Contributors
2
Replies
12 Hours
Discussion Span
2 Years Ago
Last Updated
3
Views
Question
Answered
martin5211
Posting Whiz in Training
271 posts since Aug 2007
Reputation Points: 52
Solved Threads: 23
Skill Endorsements: 0

JSTL does not know of the Java variables you set, therefore in the following code the variable 'posts' cannot be used as is:

<% 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>

In this case c:forEach tries to find a variable called 'posts' in the session. So, if you had used

session.setAttribute("posts",posts")

right after the variable definition, it would work.

However, none of the above is necessary.
JSTL already has access to Post.getPosts, because of the "jsp:useBean" you have declared earlier.

So, this should do the trick:
<c:forEach var="post" items="${Posts.posts}">

Note that in JSTL the dot notation accounts for the getter of the specified property, so ${Posts.posts} is equal to "Posts.getPosts()".

nohup
Newbie Poster
9 posts since Aug 2010
Reputation Points: 16
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 2 Years Ago by nohup

Thank you. It's ok now using the dot notation.

martin5211
Posting Whiz in Training
271 posts since Aug 2007
Reputation Points: 52
Solved Threads: 23
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1084 seconds using 2.72MB