Thursday, December 17, 2009

Spring, JSF & Hibernate Integration - Part II

This tutorial mentions Step-by-Step instructions for developing Spring, JSF & Hibernate integrated application. It totally has three parts:

I. Creating simple Spring application
II. Creating application involving Spring and JSF
III. Creating application involving Spring, JSF and Hibernate

The current tutorial deals with the second part of creating application having Spring and JSF.

The final netbeans project after completing Part II of the current tutorial has been uploaded here. Because of size limitations the third party jars inside the lib folder has been deleted in the uploaded zip file.

Requirements :: Netbeans 6.7.1 with Java 6 installed

Steps
=====
Ensure that you have gone through the first part of tutorial here.

You can continue the below steps starting with the netbeans project available here which is obtained after performing steps in Part I of the tutorial

14. Create a LinkBean.java class which is used to maintain components state in JSF UI components. It accesses LinksService implementation class for business functions.

package com.linkedlinks.bean;

import com.linkedlinks.domain.Link;
import com.linkedlinks.service.LinksService;
import java.util.List;

/**
*
* @author harish
*/
public class LinkBean {
private LinksService linksService;
private String title;
private String httpLink;
private String description;

public Long getTotalLinksCount(){
return linksService.getTotalLinksCount();
}

public String createLink(){
linksService.createLink(title, httpLink,description);
return "createLinkSuccess";
}

public List<Link> getLinksList() {
List<Link> list = linksService.getLinks();
return list;
}

public void saveLink(Link link){
linksService.updateLink(link);
}

public void setLinksService(LinksService linksService) {
this.linksService = linksService;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getHttpLink() {
return httpLink;
}

public void setHttpLink(String httpLink) {
this.httpLink = httpLink;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
}

15. Delete welcomeJSF.jsp file inside Web Pages which was auto-generated.

16. Configure faces-config.xml such that it tells JSF to pick up managed bean definitions from spring beans definition file. For that add the below entry to faces-config.xml

<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application>

17. Define bean definition for LinkBean.java class in applicationContext.xml so that the JSF can access it. Add below entry to applicationContext.xml

<bean id="myBean" class="com.linkedlinks.bean.LinkBean">
<property name="linksService">
<ref bean="myService" />
</property>
</bean>

18. Add below entry of RequestContextListener to web.xml so that spring can understand the meaning of request scope in the bean definition.

<listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

19. Set the scope as request for myBean definition in applicationContext.xml

<bean id="myBean" class="com.linkedlinks.bean.LinkBean" scope="request">
<property name="linksService">
<ref bean="myService" />
</property>
</bean>

20. Create a jsf page - linkslist.jsp inside page folder under Web Pages to list all the links using dataTable jsf UI component.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>JSP Page</title>
</head>
<body>
<h:form id="f1">
<h:commandLink value="Create Link" action="createlink" />
<h:dataTable width="90%" id="dt1" value="#{myBean.linksList}" var="link"
border="1">
<h:column>
<f:facet name="header"><h:outputText value="Title"/></f:facet>
<h:outputLink value="#{link.httpLink}">
<h:outputText value="#{link.title}"/>
</h:outputLink>
</h:column>
<h:column>
<f:facet name="header"><h:outputText value="Description"/></f:facet>
<h:outputText value="#{link.description}"/>
</h:column>
</h:dataTable>
</h:form>
</body>
</html>
</f:view>

21. Run linkslist.jsp file from netbeans. It will display a table displaying the details of the five links. This data is retreived from SimulateDao class

22. Create another jsf page - createlink.jsp having a form to create a new link. In the action attribute of commandButton, invoke the createLink method of myBean.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Create Link Page</title>
</head>
<body>
<h:form>
<h:commandLink value="Links list" action="linkslist" />
<h:panelGrid id="panel" columns="2" border="1">
<f:facet name="header">
<h:outputText value="Create New Link"/>
</f:facet>
<h:outputLabel for="title" value="Title" />
<h:inputText id="title" value="#{myBean.title}" />
<h:outputLabel for="httpLink" value="Http Link" />
<h:inputText id="httpLink" value="#{myBean.httpLink}" />
<h:outputLabel for="description" value="Description" />
<h:inputText id="description" value="#{myBean.description}" />
<f:facet name="footer">
<h:panelGroup style="display:block; text-align:center">
<h:commandButton id="submit" value="Create"
action="#{myBean.createLink}" />
</h:panelGroup>
</f:facet>
</h:panelGrid>
</h:form>
</body>
</html>
</f:view>

23. Add below navigation rule in faces-config.xml. It says that if we get createLinkSuccess outcome from createlink.jsp file, then navigate to linkslist.jsp page.

<navigation-rule>
<from-view-id>/page/createlink.jsp</from-view-id>
<navigation-case>
<from-outcome>createLinkSuccess</from-outcome>
<to-view-id>/page/linkslist.jsp</to-view-id>
</navigation-case>
</navigation-rule>

24. Now when we run createlink.jsp, enter values and press Create button, it redirects to linkslist.jsp page listing the newly added link in the table.

25. Similarly add links in both createlink.jsp & linkslist.jsp to navigate between these two pages.
Add below entry above panelGrid definition in createlink.jsp

<h:commandLink value="Links list" action="linkslist" />

Add below entry above dataTable definition in linkslist.jsp

<h:commandLink value="Create Link" action="createlink" />

Change the navigation rules in faces-config.xml as below to enable page navigation between create and list link pages.

<navigation-rule>
<from-view-id>/page/createlink.jsp</from-view-id>
<navigation-case>
<from-outcome>createLinkSuccess</from-outcome>
<to-view-id>/page/linkslist.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>linkslist</from-outcome>
<to-view-id>/page/linkslist.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/page/linkslist.jsp</from-view-id>
<navigation-case>
<from-outcome>createlink</from-outcome>
<to-view-id>/page/createlink.jsp</to-view-id>
</navigation-case>
</navigation-rule>

Swith over to PageFlow view of faces-config.xml which shows the connections between create and list pages.

26. Re-run createlink.jsp file and the links are displayed at top in both create and list link pages for navigation.

This completes integration of JSF with Spring. To contiue further for integrating Hibernate with Spring & JSF, go through part III of the tutorial here.

No comments:

Post a Comment