Seasar2(S2Container),简单的说,是比Spring一样提供DI功能的框架,但比Sping轻量级。
比理论来说本人更喜欢实际操作和调试,接下来,就让我们开始Seasar2之旅吧。
首先,先下载和安装JDK,Eclipse,Tomcat
JDK 下载:http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html 下载JDK 6 Update 22
Eclipse 下载:http://www.eclipse.org/downloads/ 下载Eclipse IDE for Java EE Developers
Tomcat 下载:http://tomcat.apache.org/download-60.cgi 下载32-bit Windows zip
接下来,下载Seasar2(S2Container)
http://s2container.seasar.org/2.4/ja/downloads.html 下载S2Container
把下载后的S2Container解压缩,主要关注下2个文件夹
「lib」文件夹:包含S2Container以及相关库。
「doc」文件夹:S2Container的相关文档。
接下来,开始用S2Container开发。
使用Eclipse新建项目,「File」⇒「New」⇒「Dynamic Web Project」,项目名叫“seasar”。
把下面jar包拷贝到lib目录下,并加入到Build Path里。
(1)使用S2Container必须的文件
s2-framework-2.4.42.jar
commons-logging-1.1.1.jar
javassist-3.4.ga.jar
ognl-2.6.9-patch-20090427.jar
(2)使用S2AOP必须的文件
aopalliance-1.0.jar
(3)使用Java EE服务器以外的Servlet容器(比如Tomcat)时必须的文件
geronimo-jta_1.1_spec-1.0.jar
新建Bean类(DataBean.java)
view plaincopy to clipboardprint?
01.package jp.seasar.framework;
02.
03.import java.util.Date;
04.
05.public class DataBean {
06. private String title;
07. private java.util.Date time;
08. private String memo;
09.
10. public DataBean() {
11. }
12.
13. public DataBean(String title, Date time, String memo) {
14. this.title = title;
15. this.time = time;
16. this.memo = memo;
17. }
18.
19. public String getTitle() {
20. return title;
21. }
22.
23. public void setTitle(String title) {
24. this.title = title;
25. }
26.
27. public java.util.Date getTime() {
28. return time;
29. }
30.
31. public void setTime(java.util.Date time) {
32. this.time = time;
33. }
34.
35. public String getMemo() {
36. return memo;
37. }
38.
39. public void setMemo(String memo) {
40. this.memo = memo;
41. }
42.
43. @Override
44. public String toString() {
45. return "Title:" + title + ",Date:" + time + ",Memo:" + memo;
46. }
47.
48.}
新建管理Data的接口(TestBean.java)
view plaincopy to clipboardprint?
01.package jp.seasar.framework;
02.
03.import java.util.ArrayList;
04.
05.public interface TestBean {
06.
07. public java.util.ArrayList getDatalist();
08.
09. public void setDatalist(ArrayList list);
10.
11. public void addData(String title, java.util.Date time, String memo);
12.
13. public void removeData(int i);
14.
15. public String toString();
16.}
新建管理Data的实现类
view plaincopy to clipboardprint?
01.package jp.seasar.framework;
02.
03.import java.util.ArrayList;
04.import java.util.Date;
05.
06.public class TestBeanImpl implements TestBean {
07.
08. private ArrayList datalist;
09.
10. public TestBeanImpl() {
11. datalist = new ArrayList();
12. }
13.
14. @Override
15. public ArrayList getDatalist() {
16. return datalist;
17. }
18.
19. @Override
20. public void setDatalist(ArrayList list) {
21. datalist = list;
22.
23. }
24.
25. @Override
26. public void addData(String title, Date time, String memo) {
27. datalist.add(new DataBean(title, time, memo));
28.
29. }
30.
31. @Override
32. public void removeData(int i) {
33. datalist.remove(i);
34. }
35.
36. @Override
37. public String toString() {
38. String result = "
" + bean.getTitle() + " | " + bean.getTime().toString() + " | " + bean.getMemo() + " |
48. return result;
49. }
50.
51.}
新建S2Container的配置文件(app.dicon)
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="UTF-8"?>
02.nbsp;
03. "http://www.seasar.org/dtd/components24.dtd">
04.
05.
06.
新建Servlet(Servlet1.java)
view plaincopy to clipboardprint?
01.package jp.seasar.framework;
02.
03.import java.io.IOException;
04.
05.import javax.servlet.ServletException;
06.import javax.servlet.annotation.WebServlet;
07.import javax.servlet.http.HttpServlet;
08.import javax.servlet.http.HttpServletRequest;
09.import javax.servlet.http.HttpServletResponse;
10.
11.import org.seasar.framework.container.S2Container;
12.import org.seasar.framework.container.factory.S2ContainerFactory;
13.
14.@WebServlet("/serv1")
15.public class Servlet1 extends HttpServlet {
16.
17. private static final long serialVersionUID = 1L;
18.
19. @Override
20. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
21. throws ServletException, IOException {
22.
23. S2Container container = S2ContainerFactory.create("app.dicon");
24. container.init();
25. TestBeanImpl bean = (TestBeanImpl) container.getComponent("mybean");
26.
27. bean.addData("1", new java.util.Date(), "This is test.");
28. bean.addData("2", new java.util.Date(), "这是测试。");
29.
30. resp.setContentType("text/html; charset=UTF-8");
31. resp.setCharacterEncoding("UTF-8");
32.
33. java.io.PrintWriter out = resp.getWriter();
34. out.print("");
35. out.print("");
36. out.print("
");
37. out.print(bean.toString());
38. out.print("
");
39. out.print("