Seasar2入门(一)
<P><FONT style="BACKGROUND-COLOR: #ffffff">Seasar2(S2Container),简单的说,是比Spring一样提供DI功能的框架,但比Sping轻量级。</FONT></P><P><FONT style="BACKGROUND-COLOR: #ffffff">比理论来说本人更喜欢实际操作和调试,接下来,就让我们开始Seasar2之旅吧。</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">首先,先下载和安装JDK,Eclipse,Tomcat</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">JDK 下载:<a href="http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html" target="_blank">http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html</A> 下载JDK 6 Update 22 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">Eclipse 下载:<a href="http://www.eclipse.org/downloads/" target="_blank">http://www.eclipse.org/downloads/</A> 下载Eclipse IDE for Java EE Developers</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">Tomcat 下载:<a href="http://tomcat.apache.org/download-60.cgi" target="_blank">http://tomcat.apache.org/download-60.cgi</A> 下载32-bit Windows zip</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">接下来,下载Seasar2(S2Container)</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff"><a href="http://s2container.seasar.org/2.4/ja/downloads.html" target="_blank">http://s2container.seasar.org/2.4/ja/downloads.html</A> 下载S2Container</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">把下载后的S2Container解压缩,主要关注下2个文件夹</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">「lib」文件夹:包含S2Container以及相关库。</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">「doc」文件夹:S2Container的相关文档。</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">接下来,开始用S2Container开发。</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">使用Eclipse新建项目,「File」⇒「New」⇒「Dynamic Web Project」,项目名叫“seasar”。</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff"> </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">把下面jar包拷贝到lib目录下,并加入到Build Path里。</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">(1)使用S2Container必须的文件</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">s2-framework-2.4.42.jar</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">commons-logging-1.1.1.jar</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">javassist-3.4.ga.jar</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">ognl-2.6.9-patch-20090427.jar</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">(2)使用S2AOP必须的文件</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">aopalliance-1.0.jar</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">(3)使用Java EE服务器以外的Servlet容器(比如Tomcat)时必须的文件</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">geronimo-jta_1.1_spec-1.0.jar</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">新建Bean类(DataBean.java)</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">view plaincopy to clipboardprint?<BR>01.package jp.seasar.framework; <BR>02. <BR>03.import java.util.Date; <BR>04. <BR>05.public class DataBean { <BR>06. private String title; <BR>07. private java.util.Date time; <BR>08. private String memo; <BR>09. <BR>10. public DataBean() { <BR>11. } <BR>12. <BR>13. public DataBean(String title, Date time, String memo) { <BR>14. this.title = title; <BR>15. this.time = time; <BR>16. this.memo = memo; <BR>17. } <BR>18. <BR>19. public String getTitle() { <BR>20. return title; <BR>21. } <BR>22. <BR>23. public void setTitle(String title) { <BR>24. this.title = title; <BR>25. } <BR>26. <BR>27. public java.util.Date getTime() { <BR>28. return time; <BR>29. } <BR>30. <BR>31. public void setTime(java.util.Date time) { <BR>32. this.time = time; <BR>33. } <BR>34. <BR>35. public String getMemo() { <BR>36. return memo; <BR>37. } <BR>38. <BR>39. public void setMemo(String memo) { <BR>40. this.memo = memo; <BR>41. } <BR>42. <BR>43. @Override <BR>44. public String toString() { <BR>45. return "Title:" + title + ",Date:" + time + ",Memo:" + memo; <BR>46. } <BR>47. <BR>48.} <BR> </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">新建管理Data的接口(TestBean.java)</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">view plaincopy to clipboardprint?<BR>01.package jp.seasar.framework; <BR>02. <BR>03.import java.util.ArrayList; <BR>04. <BR>05.public interface TestBean { <BR>06. <BR>07. public java.util.ArrayList<DataBean> getDatalist(); <BR>08. <BR>09. public void setDatalist(ArrayList<DataBean> list); <BR>10. <BR>11. public void addData(String title, java.util.Date time, String memo); <BR>12. <BR>13. public void removeData(int i); <BR>14. <BR>15. public String toString(); <BR>16.} <BR> </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">新建管理Data的实现类 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff"> <BR>view plaincopy to clipboardprint?<BR>01.package jp.seasar.framework; <BR>02. <BR>03.import java.util.ArrayList; <BR>04.import java.util.Date; <BR>05. <BR>06.public class TestBeanImpl implements TestBean { <BR>07. <BR>08. private ArrayList<DataBean> datalist; <BR>09. <BR>10. public TestBeanImpl() { <BR>11. datalist = new ArrayList<DataBean>(); <BR>12. } <BR>13. <BR>14. @Override <BR>15. public ArrayList<DataBean> getDatalist() { <BR>16. return datalist; <BR>17. } <BR>18. <BR>19. @Override <BR>20. public void setDatalist(ArrayList<DataBean> list) { <BR>21. datalist = list; <BR>22. <BR>23. } <BR>24. <BR>25. @Override <BR>26. public void addData(String title, Date time, String memo) { <BR>27. datalist.add(new DataBean(title, time, memo)); <BR>28. <BR>29. } <BR>30. <BR>31. @Override <BR>32. public void removeData(int i) { <BR>33. datalist.remove(i); <BR>34. } <BR>35. <BR>36. @Override <BR>37. public String toString() { <BR>38. String result = "<table border=\"1\">"; <BR>39. <BR>40. for (DataBean bean : datalist) { <BR>41. result += "<tr>"; <BR>42. result += "<td>" + bean.getTitle() + "</td>"; <BR>43. result += "<td>" + bean.getTime().toString() + "</td>"; <BR>44. result += "<td>" + bean.getMemo() + "</td>"; <BR>45. result += "</tr>"; <BR>46. } <BR>47. result += "</table>"; <BR>48. return result; <BR>49. } <BR>50. <BR>51.} <BR> </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff"> 新建S2Container的配置文件(app.dicon)</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff"> <BR>view plaincopy to clipboardprint?<BR>01.<?xml version="1.0" encoding="UTF-8"?> <BR>02.<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN" <BR>03. "<a href="http://www.seasar.org/dtd/components24.dtd" target="_blank">http://www.seasar.org/dtd/components24.dtd</A>"> <BR>04.<components> <BR>05. <component name="mybean" class="jp.seasar.framework.TestBeanImpl" /> <BR>06.</components> <BR> </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">新建Servlet(Servlet1.java)</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">view plaincopy to clipboardprint?<BR>01.package jp.seasar.framework; <BR>02. <BR>03.import java.io.IOException; <BR>04. <BR>05.import javax.servlet.ServletException; <BR>06.import javax.servlet.annotation.WebServlet; <BR>07.import javax.servlet.http.HttpServlet; <BR>08.import javax.servlet.http.HttpServletRequest; <BR>09.import javax.servlet.http.HttpServletResponse; <BR>10. <BR>11.import org.seasar.framework.container.S2Container; <BR>12.import org.seasar.framework.container.factory.S2ContainerFactory; <BR>13. <BR><A href='mailto:14.@WebServlet("/serv1'>14.@WebServlet("/serv1</A>") <BR>15.public class Servlet1 extends HttpServlet { <BR>16. <BR>17. private static final long serialVersionUID = 1L; <BR>18. <BR>19. @Override <BR>20. protected void doGet(HttpServletRequest req, HttpServletResponse resp) <BR>21. throws ServletException, IOException { <BR>22. <BR>23. S2Container container = S2ContainerFactory.create("app.dicon"); <BR>24. container.init(); <BR>25. TestBeanImpl bean = (TestBeanImpl) container.getComponent("mybean"); <BR>26. <BR>27. bean.addData("1", new java.util.Date(), "This is test."); <BR>28. bean.addData("2", new java.util.Date(), "这是测试。"); <BR>29. <BR>30. resp.setContentType("text/html; charset=UTF-8"); <BR>31. resp.setCharacterEncoding("UTF-8"); <BR>32. <BR>33. java.io.PrintWriter out = resp.getWriter(); <BR>34. out.print("<html><head></head>"); <BR>35. out.print("<body>"); <BR>36. out.print("<h5>"); <BR>37. out.print(bean.toString()); <BR>38. out.print("</h5>"); <BR>39. out.print("</body></html>"); <BR>40. <BR>41. } <BR>42. <BR>43.} <BR> </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">配置web.xml</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">view plaincopy to clipboardprint?<BR>01.<?xml version="1.0" encoding="UTF-8"?> <BR>02.<web-app xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance" target="_blank">http://www.w3.org/2001/XMLSchema-instance</A>" xmlns="<a href="http://java.sun.com/xml/ns/javaee" target="_blank">http://java.sun.com/xml/ns/javaee</A>" xmlns:web="<a href="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" target="_blank">http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd</A>" xsi:schemaLocation="<a href="http://java.sun.com/xml/ns/javaee" target="_blank">http://java.sun.com/xml/ns/javaee</A> <a href="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" target="_blank">http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd</A>" id="WebApp_ID" version="2.5"> <BR>03. <display-name>seasar2</display-name> <BR>04. <servlet> <BR>05. <servlet-name>serv1</servlet-name> <BR>06. <servlet-class>jp.seasar.framework.Servlet1</servlet-class> <BR>07. </servlet> <BR>08. <servlet-mapping> <BR>09. <servlet-name>serv1</servlet-name> <BR>10. <url-pattern>/serv1</url-pattern> <BR>11. </servlet-mapping> <BR>12. <welcome-file-list> <BR>13. <welcome-file>index.html</welcome-file> <BR>14. </welcome-file-list> <BR>15.</web-app> <BR> </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">编译发布到Tomcat</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">==============================================</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">本文没有写完,还需要修改,不明之处,还请谅解。</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff"></FONT> </P>
<P><FONT style="BACKGROUND-COLOR: #ffffff">本文来自CSDN博客,转载请标明出处:<a href="http://blog.csdn.net/engchina/archive/2010/11/25/6035304.aspx" target="_blank">http://blog.csdn.net/engchina/archive/2010/11/25/6035304.aspx</A></FONT></P>
页:
[1]