Dynamic JSF Application with MySql on JBoss 4.2.2 GA

March 21st, 2010 by Usha Informatique Team Leave a reply »

I have created a small JSF application (Employer Management) with MySQL as database on JBoss 4.2.2 GA in Netbeans 6.8.

These are the steps that need to be followed

1) Copy the mysql driver class (mysql-connector-java-5.1.6-bin.jar) which comes with Netbeans and located under {INSTALL DIR}/NetBeans 6.8/ide12/modules/ext to {INSTALL DIR}/server/default/lib. You don’t need to have the driver in your classpath.

2) Go to  {INSTALL DIR}/docs/examples, copy the mysql-ds.xml and copy it to {INSTALL DIR}/server/default/deploy. Change the database settings as per your configuration

3) Go to the services and (re)start the Application Server

4) Come back to netbeans, create a new project under category “Java Web”

5) Create a Managed Bean called “Employer” with member variables (id, firstname, lastname,phone and email) and corresponding getter and setter methods

6) Create a Backing Bean called EmployerFormBean with two variables

a) Employer

b) EmployerList

7) Create the getter setter for the above two variables and initialize the employerList by calling the getEmployerList function in EmployerDAO which is discussed later

8) Add method called addEmployer with its code as follows

EmployerDAO empDAO = new EmployerDAO();
empDAO.insertData(this.getEmployer());
return “go_insert_data”;

9) The configuration for Managed Bean, Backing Bean and “go_insert_data” should be made in faces-config.xml

<managed-bean>
<managed-bean-name>employerBean</managed-bean-name>
<managed-bean-class>com.testjsf.EmployerFormBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>employer</property-name>
<property-class>com.domain.Employer</property-class>
<value>#{employer}</value>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>employer</managed-bean-name>
<managed-bean-class>com.domain.Employer</managed-bean-class>
<managed-bean-scope>none</managed-bean-scope>
</managed-bean>

<from-view-id>/employer.jsp</from-view-id>
<navigation-case>
<from-outcome>go_insert_data</from-outcome>
<to-view-id>/employerlist.jsp</to-view-id>
</navigation-case>

10) As you see, the database operations are performed inside DAO. The method insertData would be as follows

InitialContext ctx = new InitialContext();
//DataSource ds = (DataSource) ctx.lookup(”jdbc/jsfperson”);
DataSource ds = (DataSource) ctx.lookup(”java:/MySqlDS”);
conn = ds.getConnection();
String query = “insert into employer(firstname,lastname,email,phone) values (?,?,?,?)”;
//String query = “insert into employer(firstname,lastname,email,phone) values (’Mayank’,'Singhai’,'123456′,’abc@yahoo.com’)”;
stmt = conn.prepareStatement(query);
stmt.setString(1, emp.getFirstname());
stmt.setString(2, emp.getLastname());
stmt.setString(3, emp.getEmail());
stmt.setString(4, emp.getPhone());
stmt.executeUpdate();

11) While doing the JNDI lookup in JBoss call it as “java:/{dsname}” as defined in mysql-ds.xml

12) After insert, populate the employerList again in the backing bean before redirecting

12) Similarly create the getEmployerList function to get the list of employers

13) Now create two jsp

a) employer.jsp where form to insert data for the employer. The fields here are bind to the employer object in employerbean. The structure is as

<h:form id=”EmployerForm”>
<h:outputLabel value=”#{msg.firstname}”/> <h:inputText value=”#{employerBean.employer.firstname}”/>
<h:outputLabel value=”#{msg.lastname}”/> <h:inputText value=”#{employerBean.employer.lastname}”/>
<h:outputLabel value=”#{msg.phone}”/> <h:inputText value=”#{employerBean.employer.phone}”/>
<h:outputLabel value=”#{msg.email}”/> <h:inputText value=”#{employerBean.employer.email}”/>
<h:commandButton action=”#{employerBean.addEmployer}” value=”Submit”/>
<h:commandLink action=”show_emp_data” value=”Show all employers”/>
</h:form>

b) employerlist.jsp To display the list of employers. In this use the dataTable concept of JSF which is as follows

<h:form>
<h:dataTable value=”#{employerBean.empList}” var=”item”>
<h:column>
<f:facet name=”header”>
<h:outputText value=”Employee No.”/>
</f:facet>
<h:outputText value=”#{item.id}”/>
</h:column>
<h:column>
<f:facet name=”header” >
<h:outputText value=”First Name”/>
</f:facet>
<h:outputText value=”#{item.firstname}”/>
</h:column>
<h:column>
<f:facet name=”header” >
<h:outputText value=”Last Name”/>
</f:facet>
<h:outputText value=”#{item.lastname}”/>
</h:column>
<h:column>
<f:facet name=”header” >
<h:outputText value=”Email”/>
</f:facet>
<h:outputText value=”#{item.email}”/>
</h:column>
<h:column>
<f:facet name=”header” >
<h:outputText value=”Phone”/>
</f:facet>
<h:outputText value=”#{item.phone}”/>
</h:column>

</h:dataTable>
</h:form>

16) Compile the code and run employer.jsp. On successful insertion, employerlist with newly inserted record would be displayed.

Advertisement

142 comments

  1. Thanks for taking this opportunity to discuss  this, I feel strongly  about it and I take pleasure in learning about this topic.  If possible, as you gain information, please add to  this blog with new information.  I have found it extremely useful.

  2. dog parvo says:

    Well, I have been reading your blog posts daily and the reason I come on your blog frequently is its compelling content… Regards…

  3. xbox 360 says:

    Thank you for the information, going to bookmark the site!

  4. I accept, I have not been on this webpage in a extended time… in spite of that it was another prize to see It is such an portentous topic and ignored around so numberless, uninterrupted professionals. I gratefulness you to refrain from making people more hip of possible issues.

  5. This article was recommended to me by a friend. I have to say, reading this was time well spent. I will tell more friends about it as well.

  6. Nice article. Thank you for this info

  7. Well, I have been reading your blog posts daily and the reason I come on your blog frequently is its compelling content… Regards…

  8. Cc cleaner says:

    thanks for the points , i’d love to follow your weblog as usually as i can.use a wonderful day~

  9. Jeans says:

    Hi there. I really appreciate the nice points you made. I dont think Ive actually thought about it in that way. I can really appreciate how you approached the subject matter and what you said really gave me a new perspective. Thanks for taking the time to write this all out. Great precise info.

  10. Jeans says:

    Thank you for such a good blog. Where else could anyone get that kind of information written in such an incite full way? I have a presentation that I am just now working on, and I have been looking for such info.

  11. thankyou lots, I am obliged to announce that your blog is excellent!

  12. nice share, good article, very usefull for me…thanks

  13. Ted Falto says:

    Good post. What other WP resources do you suggest?

  14. onlinecasino says:

    You got a indeed practical blog I compel ought to been here reading as a replacement for about an hour. I am a newbie and your ascendancy is profoundly much an inspiration in behalf of me.

  15. Its really nice post thanks.

  16. motercalo says:

    Have you got more news like that ?

  17. motercalo says:

    This blog is very interesting

  18. Rebecca says:

    I just wanted to say your blog is one of the nicely laid out, most inspirational I have come across in quite a while. Thx! :)

  19. Nice thanks (and for the other one too), are we going to be expecting more tips from you?

  20. Eminent pieces as usual…

  21. That was a

    great post. It is inspiring for all. Thanks for sharing that piece.

  22. Pierre Scrim says:

    Super-Duper site! I am loving it!! Will come back again – taking you feeds also, Appreciation.

  23. Thank you for keeping us updated. I really appreciate it and find all the information very useful.

  24. this post is very usefull thx!

  25. I cannot thank you enough for the blog article.Thanks Again. Will read on…

  26. Nice Site layout for your blog. I am looking forward to reading more from you.

  27. Thanks for your very informative post, I have bookmarked your site, thanks much!

  28. Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful.

  29. Howdy there,I discover that your weblog is extremely educational and helpful and we wonder if there can be a possibility of obtaining More web content like this on your web log. If you willing to help us out, we will be willing to compensate you¡­ Best wishes, William Kuokkanen

  30. I just want to say quick hello and want to say thanks for this good article. I digg and searching through the web for some kind of very usefularticle. like that, or at least a website. That coveredwhat i looking for
    Best Regards.

  31. Allen says:

    I Justwould like to say howdy and to let you know that I really like readingyour blog, it is very well written and articulate. Keep up the good work! Check out my new blog called stock market tips

  32. This page is very helpful for me to choose the viable product.

  33. cheap mbt says:

    Enjoyed reading

    the report – most informative thanks

  34. go holiday says:

    My sister surfed whole internet to readpost about to this field… Best regards

  35. Thanks for this information. I think someone need some help like this.

  36. Lila Salus says:

    Ah, I discover the actual fact has limitless debatable points. I do not wish to argue with you here, but I’ve my own opinions as well. Anyway, you did a great job in writing the publish, and wish to reward you for the laborious work. Sustain with the great job!

  37. mbt tataga says:

    Nice thanks (and for the other one too), are we going to be expecting more tips from you?

  38. mbt tataga says:

    For a long time, I am looking an post like such a topic. Now I have found it. Thank you for your sharing, man!

  39. May you be fascinated on a link change? Please give us a message.

  40. it was very interesting to read.

  41. Otha Poovey says:

    Your article’s topic has been one of my keenest interests for years. It’s good to find someone else who shares my deepest passion.

  42. Thank you so much for this valuable information. Very useful indeed. . . . .

  43. Good article Thank you so much

  44. This is an interesting article. Thanks for sharing.

  45. This is an interesting article. Thanks for sharing.

  46. diving says:

    You certainly have some agreeable opinions and views. Your blog provides a fresh look at the subject.

  47. That is very sublime stuff. Never new that beliefs could be this varied. Thanks for all the enthusiasm to extend such helpful information at this site.

  48. Its very informative and interesting article.all the points are very useful. Simple but very effective writing. Thanks for sharing such a nice post.

  49. I have some trouble to subscribe the rss feed, anyway I’ve bookmarked this site, is very useful and full of informations.

Trackbacks /
Pingbacks

Leave a Reply