This is a very stupid question, probably with a very simple answer. I have been googling around for the past week attempting to understand this, however, and have come up empty.
I have a simple servlet that allows a user to access an article posted by another user. The servlet uses the request url to determine what article to get, and follows the pattern:
http://www.example.com/Servlet/Article#
By using this number in the url, it fetches the article, puts it into a corresponding article object, which is a request-scoped managed bean, and attempts to pass the request to the a JSF, article.xhtml, which is responsible for rendering the article page.
Despite my best efforts, I appear unable to pass the Article bean created in my servlet to the JSF page. The method by which I pass the article is as follows:
//import and package declaration
public class ArticleServlet extends HttpServlet {
//various methods
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//Retrieve article #
Article article = //create/fetch article using #
request.setAttribute("article", article);
request.getRequestDispatcher("/dev/article.xhtml").forward(request, response);
}
}
The Article class itself is setup similar to the following:
@ManagedBean
@RequestScoped
public class Article {
private String title;
//other instance variables
public Article()
{
title = null;
//init other variables to default values
}
public Article(Integer articleNum)
{
//Retrieve article and init using the number, including the title variable
}
//Getter and Setter methods
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
//More getters and setters
}
Finally, I have the JSF page itself, which is modified from a mockup the web designer has made for me. I've attempted two ways to access the bean from within the page:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
//header stuff, stylesheet links, etc.
</h:head>
<h:body>
//template text, scripts, etc.
<h2>#{article.title}</h2>
</h:body>
The other method I have attempted is replacing the outputText tag with #{article.title}
. The first method, as I have it posted, just fills in the space in the h2 tag with nothing, and the second method literally prints the characters #{article.title}
to the page.
Am I attempting to use JSF for something it isn't meant for? Am I not passing the bean through to the page properly? All I know is that this should be a fairly simple task, yet I haven't been able to figure it out regardless of the amount of searching I've done.