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)
package jp.seasar.framework; import java.util.Date; public class DataBean { private String title; private java.util.Date time; private String memo; public DataBean() { } public DataBean(String title, Date time, String memo) { this.title = title; this.time = time; this.memo = memo; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public java.util.Date getTime() { return time; } public void setTime(java.util.Date time) { this.time = time; } public String getMemo() { return memo; } public void setMemo(String memo) { this.memo = memo; } @Override public String toString() { return "Title:" + title + ",Date:" + time + ",Memo:" + memo; } }
新建管理Data的接口(TestBean.java)
package jp.seasar.framework; import java.util.ArrayList; public interface TestBean { public java.util.ArrayList
新建管理Data的实现类
package jp.seasar.framework; import java.util.ArrayList; import java.util.Date; public class TestBeanImpl implements TestBean { private ArrayList"; for (DataBean bean : datalist) { result += "
"; return result; } } "; result += " "; } result += "" + bean.getTitle() + " "; result += "" + bean.getTime().toString() + " "; result += "" + bean.getMemo() + " "; result += "
新建S2Container的配置文件(app.dicon)
新建Servlet(Servlet1.java)
package jp.seasar.framework; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.seasar.framework.container.S2Container; import org.seasar.framework.container.factory.S2ContainerFactory; @WebServlet("/serv1") public class Servlet1 extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { S2Container container = S2ContainerFactory.create("app.dicon"); container.init(); TestBeanImpl bean = (TestBeanImpl) container.getComponent("mybean"); bean.addData("1", new java.util.Date(), "This is test."); bean.addData("2", new java.util.Date(), "这是测试。"); resp.setContentType("text/html; charset=UTF-8"); resp.setCharacterEncoding("UTF-8"); java.io.PrintWriter out = resp.getWriter(); out.print("
"); out.print(""); out.print("
配置web.xml
编译发布到Tomcat
==============================================
本文没有写完,还需要修改,不明之处,还请谅解。