bLOGical
Carpe Diem "Weblog reporting on Advanced Technologies, Grid-Computing, XML WebServices, Semantic Web and Java / Python development"
 
                                                                                                         
   Updated: 10/28/2003; 8:07:17 AM.            

>

Tuesday, August 26, 2003
> Eclipse + Resin + WebWork + Hibernate = Sah-WEET!

Eclipse + Resin + WebWork + Hibernate = Sah-WEET!

Here's how it's done:

  1. Download Eclipse. The 2.1 M2 build has been working well for me.
  2. Download Resin. 2.1.6 was recently released.
  3. Download the Resin plug-in for Eclipse. Version 0.5.2 has been working well for me.
  4. 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.
  5. Download Hibernate. 1.2b2 was recently released.
  6. Download WebWork. Version 1.2.1 has been working well for me.
  7. Download Log4J. Version 1.2.7 was recently released.
  8. Create a Resin Java project in Eclipse.
  9. Right-click on "WEB-INF/src" and select "Import..." from the drop-down.
  10. Navigate through the filesystem to WebWork's skeleton example, importing "webwork.properties", "webwork.vm", "log4j.properties", and "views.properties".
  11. Import WebWork's "web.xml" into "WEB-INF" in the same manner.
  12. From WebWork's "lib" folder import "webwork.jar" and all of the supporting-jars except "saxon.jar" into "WEB-INF/lib".
  13. Import WebWork's "template" folder into the root of your Resin project.
  14. From Hibernate's root import "cache.ccf" and "hibernate.properties" into "WEB-INF/src".
  15. 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".
  16. Also from Hibernate's root import "hibernate.jar" into "WEB-INF/lib".
  17. 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".
  18. Import the Log4J .jar file into "WEB-INF/lib".
  19. Use Hibernate's tools to create your persistent Java classes and their mappings.
  20. 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>

  21. 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.

  22. 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:}
    

  23. 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>
    

  24. 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.
> Deng Mosquito XFORMS
Preparing for the tutorial session at OSCON, I again came across Sebastian Schnitzenbaumer's project: A full implementation of XHTML, XForms, CSS, SVG, XFrames and other neat stuff.

Oh yeah, it's written in Flash+ActionScript, so it runs on any remotely recent browser platform.

This project has come a long way since I last looked at it. I'm impressed. -m

:: Micah Dubinko 11:29 AM [+] ::
... [Push Button Paradise]
> Xforms Tutorial - OSCON 2003
Slides for XForms Tutorial at OSCON 2003. -m
:: Micah Dubinko 11:28 PM [+] ::
... [Push Button Paradise]
> SonyEricsson New J2ME SDK.
SonyEricsson New J2ME SDK. For those of us getting errorrs when attempting to debug midlets using the SE emulator and WTK, SE has put up a new version of the whole package J2ME SDK to solve this issue. The exact error it fixes is that you could not connect to the debug session using a Java IDE after starting the midlet in the SE emulator. [java.blogs Day's Entries]
> Introduction to Java Business Integration (JBI)/JSR-208
Introduction to Java Business Integration (JBI)/JSR-208. Here is a good introduction to Java Business Integration (JBI). JBI is to integration and business process management what J2EE was to development of new application. A very smart move to leverage the emergence of BPEL, XQuery, etc. as first class citizens of the Java platform. It will be interesting to see how Microsoft and the monolithic .NET platform will react to this. In a lot of ways, Microsoft is building a Mac where the Java camp is designing a PC.... [Collaxa's Take]

© Copyright 2003 Ed Pimentel.
 

August 2003
Sun Mon Tue Wed Thu Fri Sat
          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            
Jul   Sep



Subscribe to "bLOGical" in Radio UserLand.

Click to see the XML version of this web page.
 

Click here to send an email to the editor of this weblog.