Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » 技术文章库  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 (转)Eclipse + Resin + WebWork + Hibernate = Sah-WEET!
merlin45





发贴: 137
积分: 20
于 2003-12-04 10:00 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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.

http://radio.weblogs.com/0100136/stories/2002/11/15/eclipseResinWebworkHiberna.html



作者 Re:(转)Eclipse + Resin + WebWork + Hibernate = Sah-WEET! [Re:merlin45]
club3



发贴: 0
积分: 0
于 2003-12-21 23:13 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923