?
Solved

JSF DB Access

Posted on 2006-10-20
14
Medium Priority
?
802 Views
Last Modified: 2008-01-09
What is the recommended mechanism for DB access with JSF?

Can you provide a simple example that I could work from that gives me:

Any config file(s) information I need to add
Any property file information I need to add
A sample JSF file
A sample bean (I'm guessing this will be needed)
Any other code that I would need

I'm using postgres database (I"m guessing that isn't terribly relevant other than the fact that I might be using my JDBC postgres driver) and Tomcat app server

Thanks for any quick responses I need to get going quickly on this.
0
Comment
Question by:kls1
  • 5
  • 4
  • 4
13 Comments
 

Author Comment

by:kls1
ID: 17778033
I don't see any info on interaction with databases, am I overlooking it?
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17778063
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 

Author Comment

by:kls1
ID: 17778312
I had found the first one also -- it seems to be geared to MS Access and Windows applications. Mine will be Linux/Unix based.

Does the second one have the needed pieces? I don't see any sample code -- am I missing it?

Thanks...
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17778340
sample code for what ?
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17780879
Use SDO: while this isn't very popular in general, it actually works really well with JSF. It's what IBM's Rational tools use to access data. Of course, if you want to really do The Right Thing, you could provide access to the data in an SOA manner, exposing access to the data as a service.
0
 

Author Comment

by:kls1
ID: 17788835
Jaggernat -- I hope I'm not being simplistic, but I've been looking for sample code that will do DB access from JSF "the right way". I have just started JSP and JSF, so I am _very_ clueless about what I'm doing (yes, I know Java), therefore am looking for a simple example that gives me all the basic pieces -- as JSF code, bean code, and property config files. Usually I can find this stuff very easily on the net but have not been able to find something that will help me get a quick start (I have people breathing down my throat to get this done quickly and "the right way".
0
 

Author Comment

by:kls1
ID: 17788850
Bpmurray -- do you thin either one of these methods would be easy to get a quick start for a newbie in this stuff like me? (Someone who knows Java but is absolutely clueless with JSF and JSP?)

I want to do this the right way but I also need to get it done quickly. (And I want to do it cleanly, I've seen too many messes that have stuck around from people trying to write code quickly...) I guess I want my cake and eat it too.... Is it possible? Or is there a way I'm best off going with in order to satisfy my managers by getting it done fast?
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17790661
Perhaps a good way to start would be to use the beta of Rational's new version of tools: http://www-306.ibm.com/software/rational/beta/v7/ - sign up for the architect product. This will make it realy easy to create your apps.

Alternatively, since you're happy enough with DB access, encapsulate the DB access in a bean. Then you simply access it using the EL (expression language). So you get something like:
   <x:output value="#{bean.property}">

This gets translated in JSF to a call to bean.getProperty. You can tag the bean as being a "managed bean" in which case it's already know to JSF so you don't have to declare it anywhere. I'll try to throw together a basic example later and post it in the files section.
0
 
LVL 15

Accepted Solution

by:
bpmurray earned 1600 total points
ID: 17799460
Sorry - haven't been able to make time, but here's something you could use to try to understand how it all hangs together. First, here's a simple bean - you could create something equivalent that retrieves data from a database rather than this static thing:

/******* Class 1: Companies - a collection of Company object **********/
package my.bean;

import java.util.ArrayList;

public class Companies {
      // Variables used for the class
      ArrayList<Company> companiesList = new ArrayList<Company>();

      /**
       *
       */
      public Companies() {
            super();

            // Add companies
            companiesList.add(new Company("Small Wonders"));
            companiesList.add(new Company("Small Rocks"));
            companiesList.add(new Company("Smart Travellers"));
            companiesList.add(new Company("Silly"));
            companiesList.add(new Company("Sweet Stuff"));
      }

      public Company[] getCompanies() {
            Company[] co = new Company[0];
            co = (Company[]) companiesList.toArray(co);
            return co;
      }

}

/*********** Class 2 : The Company object ********/
package my.bean;

public class Company {
      String name = null;

      public Company() {
            this("");
      }

      public Company(String Name) {
            super();
            setName(Name);
      }

      /**
       * @return Returns the name.
       */
      public String getName() {
            return name;
      }

      /**
       * @param name The name to set.
       */
      public void setName(String name) {
            this.name = name;
      }
}
/****************************/


Next, for the simple page. This page has a simple table which is used to display the list of the names, one per cell in the table's single column:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%-- jsf:pagecode language="java" location="/src/pagecode/Index.java" --%><%-- /jsf:pagecode --%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="theme/stylesheet.css"      title="Style">
</head>
<f:view>
      <body>
            <h:form id="form1" styleClass="form">
                  <h:dataTable border="0" cellpadding="2" cellspacing="0"
                        columnClasses="columnClass1" headerClass="headerClass"
                        footerClass="footerClass" rowClasses="rowClass1, rowClass2"
                        id="tableEx1" styleClass="dataTableEx"
                        value="#{pc_Index.companies.companies}" var="varCompanies">
                        <h:column id="column1">
                              <h:outputText id="textName1" value="#{varCompanies.name}" styleClass="outputText"></h:outputText>
                        </h:column>
                  </h:dataTable>
            </h:form>
      </body>
</f:view>
</html>


0
 
LVL 10

Assisted Solution

by:jaggernat
jaggernat earned 400 total points
ID: 17800655
>>>>but I've been looking for sample code that will do DB access from JSF "the right way

http://www.oracle.com/technology/pub/articles/cioroianu_jsfdb.html
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 18002233
Just one more comment: I got a mail from Sun today about using JSF with the new Java Persistence APIs, which might help you. Have a look at this sample: http://communications1.sun.com/r/c/r?1.1.3J1.2Vc.12RRpW.C00W*O..H.D4F6.1aec.3KCXAZ
0
 

Author Comment

by:kls1
ID: 18021325
Thanks for the help and suggestions, I really appreciate it.
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Washington based MSP turned to OnPage to solve their needs for after-hours alerting when customers’ technologies failed. In this post  see how DNS benefited from rolling out OnPage as a solution: -Preserve their SLAs -Improve response time by…
In this article, we will see two different methods to recover deleted data. The first option will be using the transaction log to identify the operation and restore it in a specified section of the transaction log. The second option is simpler and c…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question