Eclipse + Resin + WebWork + Hibernate = Sah-WEET!
Here's how it's done:
- Download Eclipse. The 2.1 M2 build has been working well for me.
- Download Resin. 2.1.6 was recently released.
- Download the Resin plug-in for Eclipse. Version 0.5.2 has been working well for me.
- Install and configure all of the above. In particular, make sure that your JDBC driver is in Resin's "lib" folder if it's not already on your system classpath.
- Download Hibernate. 1.2b2 was recently released.
- Download WebWork. Version 1.2.1 has been working well for me.
- Download Log4J. Version 1.2.7 was recently released.
- Create a Resin Java project in Eclipse.
- Right-click on "WEB-INF/src" and select "Import..." from the drop-down.
- Navigate through the filesystem to WebWork's skeleton example, importing "webwork.properties", "webwork.vm", "log4j.properties", and "views.properties".
- Import WebWork's "web.xml" into "WEB-INF" in the same manner.
- From WebWork's "lib" folder import "webwork.jar" and all of the supporting-jars except "saxon.jar" into "WEB-INF/lib".
- Import WebWork's "template" folder into the root of your Resin project.
- From Hibernate's root import "cache.ccf" and "hibernate.properties" into "WEB-INF/src".
- Note that there's also another "log4j.properties" file. Open it with your favorite text editor, copy the contents, and append them to the "log4j.properties" that you already imported into "WEB-INF/src".
- Also from Hibernate's root import "hibernate.jar" into "WEB-INF/lib".
- From Hibernate's "lib" folder import all of the .jars except "j2ee.jar," "junit.jar," "xerces.jar," and "xml-apis.jar" into "WEB-INF/lib".
- Import the Log4J .jar file into "WEB-INF/lib".
- Use Hibernate's tools to create your persistent Java classes and their mappings.
- Edit "WEB-INF/web.xml". Add entries similar to the following:
12: <servlet>
13: <servlet-name>initializer</servlet-name>
14: <servlet-class>skeleton.servlet.SkeletonInitializer</servlet-class>
15: <load-on-startup>1</load-on-startup>
16: </servlet>
17:
18: <resource-ref>
19: <res-ref-name>jdbc/skeleton</res-ref-name>
20: <res-type>javax.sql.DataSource</res-type>
21: <init-param driver-name="org.postgresql.Driver"/>
22: <init-param url="jdbc:postgresql://localhost/skeleton"/>
23: </resource-ref>
- Write your Initializer servlet. It'll look a lot like this:
1:package skeleton.servlet;
2:
3:import java.io.IOException;
4:
5:import javax.servlet.GenericServlet;
6:import javax.servlet.ServletConfig;
7:import javax.servlet.ServletContext;
8:import javax.servlet.ServletException;
9:import javax.servlet.ServletRequest;
10:import javax.servlet.ServletResponse;
11:
12:import javax.naming.Context;
13:import javax.naming.InitialContext;
14:import javax.naming.NamingException;
15:
16:import java.sql.Connection;
17:import java.sql.SQLException;
18:import javax.sql.DataSource;
19:
20:import cirrus.hibernate.Datastore;
21:import cirrus.hibernate.Hibernate;
22:import cirrus.hibernate.HibernateException;
23:import cirrus.hibernate.SessionFactory;
24:import cirrus.hibernate.Session;
25:
26:public class SkeletonInitializer extends GenericServlet
27:{
28: public void init(ServletConfig config) throws ServletException
29: {
30: super.init(config);
31: Datastore ds = Hibernate.createDatastore()
32: .storeClass(skeleton.persistent.Customer.class)
33: .storeClass(skeleton.persistent.Purchase.class);
34:
35: try
36: {
37: SessionFactory factory = ds.buildSessionFactory();
38:
39: Context ctx = (Context)new InitialContext().lookup("java:comp/env");
40: DataSource db = (DataSource)ctx.lookup("jdbc/skeleton");
41: Connection conn = db.getConnection();
42: Session sess = factory.openSession(conn);
43: sess.disconnect();
44:
45: ServletContext app = getServletContext();
46: app.setAttribute("hibernate.factory", factory);
47: app.setAttribute("hibernate.session", sess);
48: }
49: catch (HibernateException he)
50: {
51: he.printStackTrace();
52: }
53: catch (NamingException ne)
54: {
55: ne.printStackTrace();
56: }
57: catch (SQLException se)
58: {
59: se.printStackTrace();
60: }
61: }
62:
63: public void service(ServletRequest req, ServletResponse res) throws IOException, ServletException
64: {
65: // This space intentionally left blank
66: }
67:}
Of course, you'll need to use the actual names of your persistent classes or, better yet, rework this to get them from web.xml.
- Write your WebWork action classes, which will look a lot like this:
1:package skeleton.action;
2:
3:import webwork.action.Action;
4:import webwork.action.ActionContext;
5:import webwork.action.ActionSupport;
6:
7:import cirrus.hibernate.Hibernate;
8:import cirrus.hibernate.HibernateException;
9:import cirrus.hibernate.Session;
10:
11:import java.sql.SQLException;
12:
13:import java.util.List;
14:
15:import skeleton.persistent.Customer;
16:
17:public class SkeletonAction extends ActionSupport
18:{
19: private String incomingA;
20: private String incomingB;
21: private Customer customer;
22:
23: public void setIncomingA(String value)
24: {
25: incomingA = value;
26: }
27:
28: public void setIncomingB(String value)
29: {
30: incomingB = value;
31: }
32:
33: public void setCustomer(Customer value)
34: {
35: customer = value;
36: }
37:
38: public String getIncomingA()
39: {
40: return incomingA;
41: }
42:
43: public String getIncomingB()
44: {
45: return incomingB;
46: }
47:
48: public Customer getCustomer()
49: {
50: return customer;
51: }
52:
53: public String execute()
54: {
55: Session sess = (Session)ActionContext.getContext().getApplication.().get("hibernate.session");
56: try
57: {
58: sess.reconnect();
59: List results = sess.find("Your query here", new Object[]{incomingA, incomingB}, new Object[]{Hibernate.STRING, Hibernate.STRING});
60: // Do something to discriminate among the results; for now snag the first one
61: customer = (Customer)results.iterator().next();
62: }
63: catch (HibernateException he)
64: {
65: he.printStackTrace();
66: return Action.ERROR;
67: }
68: catch (SQLException se)
69: {
70: se.printStackTrace();
71: return Action.ERROR;
72: }
73: finally
74: {
75: try
76: {
77: sess.disconnect();
78: }
79: catch (HibernateException he)
80: {
81: he.printStackTrace();
82: }
83: catch (SQLException se)
84: {
85: se.printStackTrace();
86: }
87: }
88: return Action.SUCCESS;
89: }
90:}
- Develop your views, probably using WebWork's <property> tag:
1:<ww:property value="customer">
2: <ww:property value="name"/><br>
3: <ww:property value="address"/><br>
4: ...
5:</ww:property>
- Reflect on the fact that steps 19-23 are the only ones that need to be done over from project to project and on the simplicity of your action code. Ship better products faster.
|
© Copyright
2002
Paul Snively.
Last update:
11/17/02; 7:09:37 AM.
This theme is based on the SoundWaves
(blue) Manila theme. |
|
|