- This topic has 3 replies, 2 voices, and was last updated 20 years, 1 month ago by Riyad Kalla.
-
AuthorPosts
-
vtcoolMemberHi,
I’m kinda new to the whole realm of accessing data from a database via a web application. So, I was wondering when to use certain objects like EJB versus beans versus the sql tag in JSP. In my web application, I have set up a datasource already but I am ready to read data from the database to display in a suitable format. To test the datasource, I was using jsp sql tags to read the data but I think there is a better way to reading data and displaying the data. I’m using JSF for the website. Any help in this area would be appreciated. Thanks 😀
Riyad KallaMemberMoving to OT > Soft Dev
As far as your question is concerned, its typical to read data out in the following fashion (we will us a product list page as an example):
1) User clicks a link to navigate to a new page
2) Managed bean reads out the products from the DB and stored it in a local variable accessible by a getter (getProducts())
3) JSP page loads, and uses a JSTL itterator to itterate over the getProducts java.util.List of Products, displaying the information on the pageYou CAN of course embedd all this in the JSP page using the SQL tags, but in my experience its considered bad design and a PIA to maintain.
vtcoolMemberThanks for the information regarding obtaining information from the database when clicking on a link. However, when starting from the initial main page that has to read data from the database like news and such, how would one obtain information from a managed-bean. With using JSF, I created a bean that would return information from the bean so that a h:outputText could use it, but for things like a list of data for news content, what would be a more beneficial way of handling things? I’m kinda new to the whole managed-bean structure so I do not know if there is a way to populate the structure before loading the initial page so that it can render it appropriately with JSTL.
Riyad KallaMemberI am far from a JSF expert so I can only direct you to jsfcentral.com (also I bought JSF In Action eBook from manning and am really enjoying it, its quite good).
But from a Struts perspective, the way you would do this is instead of making your front news page index.jsp or something like that, you would actually stick a forward in your index.jsp and immediately send people to a new action like /news.do, so it would execute your action immediately, populate the request, and send to the news.jsp page where the news would be displayed.
In JSF you could easily accomplish the same thing by having your index.jsp page immediately forward to a /news.faces or equivalent page that was backed by your NewsBean.java managed bean which had a mehod like “getCurrentNews()” and returned the last 5 news entries to be displayed, then in your JSP page you could do a forEach over the news items and walla…
But, I’m not sure yet if managed beans act like code behinds in ASP.NET, where you load the page, and when your page calls into the bean backing the page, the values are populated at that time… if that IS the case, which it very well might be, then you could simply implement your getCurrentNews() method in your bean like so:
public List getCurrentNews() { if(currentNews == null) currentNews = NewsDAO.getCurrentNews(); return currentNews; }
Note here I am merely checking the member variable “currrentNews” but instead I could be checking a session scoped, or application scoped variable just as easily, so the first time the method is called I see that the news hasn’t been loaded, so I load it from the DB by way of a DAO (encapsulating all my DB logic, even if its simply hibernate), pop it into the local variable OR a session OR any scope I want, and then return it…. so now each time this page is reloaded (depending on where I put it and what scope my bean lives in) the news will return immediately and be rendered…. now that I think about it, I believe this is how JSF works, much like ASP.NET, not like Struts where you have this Step 1, Step 2 progression…
-
AuthorPosts