|
Integrating Struts and Hibernate |
|
Following code define the key to store the
session factory instance in the Servlet
context.
public static final String
SESSION_FACTORY_KEY =
SessionFactory.class.getName();
The init() is called on the startup of the
Struts Application. On startup the session
factory is initialized and cached in the Servlet
context.
configFileURL = HibernatePlugIn.class.
getResource(_configFilePath);
context = servlet.getServletContext();
configuration = (new Configuration()).
configure(configFileURL);
_factory =
configuration.buildSessionFactory();
//Set the factory into session
context.setAttribute(SESSION_FACTORY_KEY,
_factory);
Changes to be done in struts-config.xml file
Configuring Hibernate with Struts is very
simple work it requires you to have
hibernate.cfg.xml in your WEB-INF/classes
directory, and to add the following line to the
struts-config.xml file.
<plug-in
className=”roseindia.net.plugin.HibernatePlugIn”>
</plug-in>
Testing the Plugin
Build your application and deploy on the
tomcat server. Start tomcat server and
observe the console output. It should display
following lines:
log4j:WARN Please initialize the log4j system
properly.
*************************************
**** Initilizing HibernatePlugIn **********
*************************************
Nov 27, 2007 10:09:53 AM
org.apache.struts.tiles.TilesPlugin initD
|
|
This means you have successfully configured
your Struts Hibernate Plugin with struts
application.
5. Developing Struts Web Module
In this section, we will be creating search
interface for enabling the user to search
tutorials. This example is a client to test our
Struts Hibernate Plugin.
The web component of the application
consists of the following files:
1. Search Tutorial Form
(SearchTutorial.jsp):
This file is used to display the search form to
the user. Here is the code of search form:
<%@ taglib uri=”/tags/struts-bean”
prefix=”bean” %>
<%@ taglib uri=”/tags/struts-html”
prefix=”html” %>
<html:html locale=”true”>
<head>
<title>
<bean:message key=”welcome.title”/></title>
<html:base/>
</head>
<body bgcolor=”white”>
<html:form action=”/searchTutorial”>
<html:errors/>
<table>
<tr>
<td align=”right”>
Search Tutorial
</td>
<td align=”left”>
<html:text property=”keyword” size=”30"
maxlength=”30"/>
</td>
</tr>
<tr>
<td align=”right”>
<html:submit>Search</html:submit>
</td>
</tr> |
|
Dec
2007 | Java Jazz Up | 58 |
|
|
View All Topics |
All Pages of this Issue |
Pages:
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
49,
50,
51,
52,
53 ,
54,
55,
56,
57,
58,
59,
60,
61,
62,
63 ,
64,
65 ,
66 ,
67 ,
68 ,
69 ,
70 ,
71 ,
72 ,
73 ,
74 ,
75 ,
76 ,
77 ,
78 ,
79 ,
80 ,
81 ,
82 , Download PDF |
|
|
|
|
|
|
|
|
|