Solved

JSF DB Access

Posted on 2006-10-20
14
785 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
14 Comments
 
LVL 10

Expert Comment

by:jaggernat
Comment Utility
0
 

Author Comment

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

Expert Comment

by:jaggernat
Comment Utility
0
 

Author Comment

by:kls1
Comment Utility
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
Comment Utility
sample code for what ?
0
 
LVL 15

Expert Comment

by:bpmurray
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:kls1
Comment Utility
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
Comment Utility
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
Comment Utility
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 400 total points
Comment Utility
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 100 total points
Comment Utility
>>>>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
Comment Utility
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
Comment Utility
Thanks for the help and suggestions, I really appreciate it.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now