WEB开发网
开发学院WEB开发Jsp 如何使用Eclipse + Resin + WebWork + Hibernate ... 阅读

如何使用Eclipse + Resin + WebWork + Hibernate 开发

 2008-01-05 08:58:08 来源:WEB开发网   
核心提示:版权申明:答应非盈利性引用,如有转载,如何使用Eclipse + Resin + WebWork + Hibernate 开发,请注明来源:“编译:brickhan;转载自http://www.javapub.com ”字样, 版权归原作/译者所有,对这段代码再进行加工,.编写类似下面的 WebWork action

  版权申明:答应非盈利性引用,如有转载,请注明来源:“编译:brickhan;转载自http://www.javapub.com ”字样, 版权归原作/译者所有。
按照下面的步骤进行:
1. 下载 Eclipse. 2.1 M2 版本
2. 下载Resin. 2.1.7
3. 下载Resin plug-in for Eclipse. 0.5.2 版本
4. 安装并设置上面的软件,假如JDBC DRIVER不在系统的CLASSPATH内,则需要把它复制到Resin的 "lib" 目录里面.
5. 下载 Hibernate. 1.2b2 .
6. 下载WebWork. Version 1.2.1.
7. 下载Log4J. Version 1.2.7
8. 用Eclipse 创建一个 Resin 的Java PRoject.
9. 右击 "WEB-INF/src" 后从下拉菜单选择 "Import...".
10. 浏览文件系统找到 WebWork´s skeleton example, 导入 "webwork.properties", "webwork.vm", "log4j.properties", 和 "views.properties".
11. 用同样的方法导入 WebWork的 "web.xml" 到 "WEB-INF" .
12. 从 WebWork´s "lib" 文件夹中导入 "webwork.jar" 和所有其他支持的.jar文件,除开 "saxon.jar" 文件,到 "WEB-INF/lib".
13. 导入 WebWork´s "template" 文件夹到你 Resin project的根目录.
14. 从 Hibernate´s 根目录导入 "cache.ccf" 和 "hibernate.properties" 到 "WEB-INF/src".
15. 注重这儿另外也有一个Note that there´s also another "log4j.properties" 文件file. 用编辑器打开,将其中的内容复制,然后追加到你已经导入到"WEB-INF/src"目录的"log4j.properties"中。
16. 从Hibernate´s 根目录中导入 "hibernate.jar" 到 "WEB-INF/lib"中.
17. 从Hibernate的 "lib" 目录导入所有jar文件到"WEB-INF/lib",除开 "j2ee.jar," "junit.jar," "xerces.jar," 和 "xml-apis.jar" 文件.
18. 导入Log4J .jar 文件到 "WEB-INF/lib".
19. 使用 Hibernate工具来建立持久层的类和映射.
20. 编辑 "WEB-INF/web.xml".增加如下的节点:
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>

编写类似下面的初始化 servlet.:

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



当然你需要使用真实的持久类的名称,最好,对这段代码再进行加工。.


编写类似下面的 WebWork action classes:

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




使用WebWork´s <property> tag 开发你的views,:

1:<ww:property value="customer">
2: <ww:property value="name"/><br>
3: <ww:property value="address"/><br>
4: ...
5:</ww:property>


在不同的项目上,步骤19-23是需要对action的代码进行一些工作。

Tags:如何 使用 Eclipse

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接