2011年3月31日木曜日

enumを使いたおしたい

こういうパターンはいかがでしょうか?という内容です。

javaのenumについてです。

先日、ふと、Effective Javaを読み直してenumについての認識を改めました。

enumの変数というのは、「唯一のインスタンス」
その結果、定数のようにイコールの演算子で一致するのです。

以前からそれは理解していました。
が、実は、思ってた以上に普通のクラスと同じで、インターフェースを実装できます。
さらには、mainメソッドを書いて実行することもできるのです。

これを定数としてしか使わないのは勿体無い。
ということで、有効活用をちょっと考えてみた。

こういうのはいかが?
package sample;

import java.math.BigDecimal;

enum NumberUtility {

 SIMPLE {

  @Override
  Integer createInteger(String x_str) {
   return Integer.valueOf(x_str);
  }

  @Override
  BigDecimal createBigDecimal(String x_str) {
   return new BigDecimal(x_str);
  }
 },

 QUIET {

  @Override
  Integer createInteger(String x_str) {
   Integer result = null;
   if (x_str != null) {
    try {
     result = SIMPLE.createInteger(x_str);
    } catch (NumberFormatException e) {
    }
   }
   return result;
  }

  @Override
  BigDecimal createBigDecimal(String x_args) {
   BigDecimal result = null;
   if (x_args != null) {
    try {
     result = SIMPLE.createBigDecimal(x_args);
    } catch (NumberFormatException e) {
    }
   }
   return result;
  }
 };

 abstract Integer createInteger(String x_str);

 abstract BigDecimal createBigDecimal(String x_str);
}

abstract class Test {

 void exec() {
  System.out.println("100 => " + getNumberUtility().createInteger("100"));
  System.out.println("10.0 => " + getNumberUtility().createBigDecimal("10.0"));
  System.out.println("null => " + getNumberUtility().createInteger("null"));
 }

 abstract NumberUtility getNumberUtility();
}

public class TestMain {

 public static void main(String[] args) {

  try {
   new Test() {

    @Override
    NumberUtility getNumberUtility() {
     return NumberUtility.SIMPLE;
    }
   }.exec();
  } catch (Exception e) {
   System.out.println("catch exception. => " + e.getMessage());
  }
 
  System.out.println();

  try {
   new Test() {

    @Override
    NumberUtility getNumberUtility() {
     return NumberUtility.QUIET;
    }
   }.exec();
  } catch (Exception e) {
   System.out.println("catch exception. => " + e.getMessage());
  }
 }
}
enumの中にabstractメソッドがあって、その場で実装しています。こんなことができるんですねぇ。知りませんでした。
書いた内容について簡単に言うと、Utilityクラスの切り替えです。
Utilityの処理の役割としてはstaticなメソッドで実装したいのだけど、ロジックの違いをインスタンスで変えたいって感じです。

Javaの言語仕様での、enumの実現方法がシンプルだからこそ、これだけの自由度があるのでしょう。
単純さが自由になります。

1行目でエラーが出ましたと言わせたい

今回は、ちょっとしたネタです。
知っている人も多いかもしれません。

内容はタイトルの通り「ソースコードの1行目でエラーを出す」方法です。
もしかすると、JVMに依存するかもしれませんのであしからず。

エラーを吐くコードを1行目に書く方法
public class ExceptionAtLine1 {public static void main(String[] x_args) { System.out.println(x_args[100]); } }
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
 at ExceptionAtLine1.main(ExceptionAtLine1.java:1)
すぐに思いつく方法です。

いや、そんなの当たり前だろって思われてる人も多いでしょう。

では、以下の方法はどうでしょうか?

Genericsを ”利用して” エラーを吐く方法
package sample;

public class ExceptionAtLine1Zwei {

 public static void main(String[] x_args) {
  IMethod method = new IMethodImpl();
  method.method(Integer.valueOf(100));
 }
}

interface IMethod {
 void method(T x_obj);
}

class IMethodImpl implements IMethod {

 @Override
 public void method(String x_obj) {
  System.out.println(x_obj);
 }
}
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
 at sample.IMethodImpl.method(ExceptionAtLine1Zwei.java:1)
 at sample.ExceptionAtLine1Zwei.main(ExceptionAtLine1Zwei.java:7)
どう見ても呼ばれるべきメソッドがありませんね。
Genericsを中途半端に使用すると恐ろしいことになる良い例です。

Eclipseで警告が出るようにしておきましょう。
厳しさは易しさです。

2011年3月26日土曜日

【修正版】 Struts2 + Spring + Slim3

「Struts2 + Spring + Slim3の連携に挑戦」の修正版です。

前回の内容を修正しました。
改めてSlim3のLocalTransactionとGlobalTransactionについて調べた結果、前回の内容では不完全だったことがわかりました。

今回は完全に書き直しです。

Slim3の新規プロジェクト作成
まず最初に、EclipseのSlim3プラグインを使用してSlim3の新規プロジェクトを作成します。
プロジェクト名とルートパッケージ
Project names3sample
Root Packagecom.brightgenerous.s3.sample.data
不要なファイルを削除しておきます。
削除するファイルとディレクトリ
/src/application_en.properties
/src/application_ja.properties
/war/css
/war/ktrwjr
/war/common.jsp
Jarファイル追加
以下のjarファイルを「war/WEB-INF/lib」に追加します。
追加するjarファイル
aopalliance-1.0
commons-beanutils-1.7.0
commons-digester-2.0
commons-fileupload-1.2.1
commons-io-1.3.2
commons-lang-2.3
commons-logging-1.0.4
commons-validator-1.3.1
freemarker-gae-pre3
javassist
ognl-3.0
spring-aop
spring-beans
spring-context
spring-core
spring-tx
spring-web
struts2-convention-plugin-2.2.1.1
struts2-core-2.2.1.1
struts2-spring-plugin-2.2.1.1
xwork-core-2.2.1.1
freemarker-gae-pre3は、gae用のfreemarkerです。
Springは2.5を使用します。
web.xml
「war/WEB-INF」に配置するweb.xmlファイルです。


 
  slim3.rootPackage
  com.brightgenerous.s3.sample.data
 

 
  contextConfigLocation
  classpath:applicationContext*.xml
 

 
  DatastoreFilter
  org.slim3.datastore.DatastoreFilter
 

 
  struts2
  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  
   actionPackages
   com.brightgenerous.s3.sample.action
  
 

 
  DatastoreFilter
  /*
  REQUEST
 
 
  struts2
  /*
 

 
  GlobalTransactionServlet
  org.slim3.datastore.GlobalTransactionServlet
  1
 

 
  GlobalTransactionServlet
  /slim3/gtx
 

 
  org.springframework.web.context.ContextLoaderListener
 
 
  com.brightgenerous.s3.sample.gae.GaeInitListener
 

 
  
   /slim3/gtx
  
  
   admin
  
 


Slim3の新規プロジェクトを作成して生成されるweb.xmlの内容のほとんど少しを消すことになります。
「com.brightgenerous.s3.sample.gae.GaeInitListener」は、独自に実装するクラスです。ソースは「実装するクラス」にて後述します。
applicationContext.xml
「classpath:applicationContext*.xml」(src直下)に配置します。


 

 

 

 
 
  
   
    PROPAGATION_REQUIRED
   
  
 
 
  
  
 
 
  
   
    transactionInterceptor
   
  
  
   
    *ServiceImpl
   
  
 


「transactionManager」のみ用意してやれば他はそのまま使えます。なかなかナイスな設計です。
「transactionManager」と「transactionInterceptor」を用意します。
「com.brightgenerous.s3.sample.gae.GaeFixInternalPersistenceAnnotationProcessor」、 「com.brightgenerous.s3.sample.gae.Slim3TransactionManager」、 「com.brightgenerous.s3.sample.gae.Slim3TransactionInterceptor」については、後述です。
実装するクラス
1.com.brightgenerous.s3.sample.gae.GaeInitListener
package com.brightgenerous.s3.sample.gae;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import ognl.OgnlRuntime;

public class GaeInitListener implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener {

 @Override
 public void contextInitialized(ServletContextEvent sce) {
  OgnlRuntime.setSecurityManager(null);
 }

 @Override
 public void contextDestroyed(ServletContextEvent arg0) {
 }

 @Override
 public void sessionCreated(HttpSessionEvent arg0) {
 }

 @Override
 public void sessionDestroyed(HttpSessionEvent arg0) {
 }

 @Override
 public void attributeAdded(HttpSessionBindingEvent arg0) {
 }

 @Override
 public void attributeRemoved(HttpSessionBindingEvent arg0) {
 }

 @Override
 public void attributeReplaced(HttpSessionBindingEvent arg0) {
 }
}
OGNLのセキュリティマネージャがなんとかかんとか...理由については、詳しく調べていません。
2.com.brightgenerous.s3.sample.gae.GaeFixInternalPersistenceAnnotationProcessor
package com.brightgenerous.s3.sample.gae;

public class GaeFixInternalPersistenceAnnotationProcessor {
}
「java.lang.NoClassDefFoundError」を回避することだけが目的です。
3.com.brightgenerous.s3.sample.gae.Slim3TransactionManager
package com.brightgenerous.s3.sample.gae;

import org.slim3.datastore.Datastore;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.support.AbstractPlatformTransactionManager;
import org.springframework.transaction.support.DefaultTransactionStatus;

public class Slim3TransactionManager extends AbstractPlatformTransactionManager {

 private static final long serialVersionUID = 3423679590583692519L;

 @Override
 protected void doBegin(Object x_arg0, TransactionDefinition x_arg1) throws TransactionException {
  TransactionObject transactionObject = (TransactionObject) x_arg0;
  transactionObject.setTransaction(Datastore.beginGlobalTransaction());
 }

 @Override
 protected void doCommit(DefaultTransactionStatus x_arg0) throws TransactionException {
  TransactionObject transactionObject = (TransactionObject) x_arg0.getTransaction();
  transactionObject.getTransaction().commit();
 }

 @Override
 protected Object doGetTransaction() throws TransactionException {
  return new TransactionObject();
 }

 @Override
 protected void doRollback(DefaultTransactionStatus x_arg0) throws TransactionException {
  TransactionObject transactionObject = (TransactionObject) x_arg0.getTransaction();
  transactionObject.getTransaction().rollback();
 }
}
package com.brightgenerous.s3.sample.gae;

import java.io.Serializable;

import org.slim3.datastore.GlobalTransaction;

public class TransactionObject implements Serializable {

 private static final long serialVersionUID = -8353097480608667182L;

 private GlobalTransaction p_transaction;

 public GlobalTransaction getTransaction() {
  return p_transaction;
 }

 public void setTransaction(GlobalTransaction x_transaction) {
  p_transaction = x_transaction;
 }
}
package com.brightgenerous.s3.sample.gae;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Slim3GlobalTransaction {
}
「org.springframework.jdbc.datasource.DataSourceTransactionManager」の実装を参考にして、Slim3用に簡単に対応させています。
「TransactionObject」、「Slim3GlobalTransaction」も作成します。 このクラスのみ自作です。間違い等あれば指摘していただけるとありがたいです。
4.com.brightgenerous.s3.sample.gae.Slim3TransactionInterceptor
 private void injectTransaction(Object x_obj, TransactionInfo x_txInfo) {
  TransactionStatus txStatus = x_txInfo.getTransactionStatus();
  DefaultTransactionStatus dtxStatus;
  if (!(txStatus instanceof DefaultTransactionStatus)) {
   return;
  }
  dtxStatus = (DefaultTransactionStatus) txStatus;
  Object tx = dtxStatus.getTransaction();
  if (tx instanceof TransactionObject) {
   tx = ((TransactionObject) tx).getTransaction();
  }
  if (!(tx instanceof GlobalTransaction)) {
   return;
  }
  @SuppressWarnings("rawtypes")
  Class clazz = x_obj.getClass();
  do {
   Field[] fields = clazz.getDeclaredFields();
   for (Field field : fields) {
    if (!field.isAccessible()) {
     field.setAccessible(true);
    }
    Slim3GlobalTransaction s3gtx = field.getAnnotation(Slim3GlobalTransaction.class);
    if (s3gtx != null) {
     if (field.getType().isInstance(tx)) {
      try {
       field.set(x_obj, tx);
      } catch (IllegalAccessException e) {
       throw new RuntimeException(e);
      } catch (IllegalArgumentException e) {
       throw new RuntimeException(e);
      }
     }
    }
   }
   clazz = clazz.getSuperclass();
  } while ((clazz != null) && !clazz.equals(Object.class));
 }
「org.springframework.transaction.interceptor.TransactionInterceptor」のソースをコピーしてきて、 上記のメソッドが適当な箇所で呼ばれるように書き換えます。
ライセンスを考慮して全体は公開しませんが、元のソースを読めば変更箇所はわかるはずです。

以上でStruts2 + Spring + Slim3の設定は完了です。
次回こそ、このフレームワークを使用して簡単なサンプルを書いてみます。

2011年3月24日木曜日

Struts2 + Spring + Slim3

この内容は修正されました。
修正後はこちら

Struts2 + Spring + Slim3の連携に挑戦。

ソースの省略はしません。
今回と次回で、簡単なサンプルを動作させるまでの手順を載せます。

Slim3の新規プロジェクト作成
まず最初に、EclipseのSlim3プラグインを使用してSlim3の新規プロジェクトを作成します。
プロジェクト名とルートパッケージ
Project names3sample
Root Packagecom.brightgenerous.s3.sample.data
不要なファイルを削除しておきます。
削除するファイルとディレクトリ
/src/application_en.properties
/src/application_ja.properties
/war/css
/war/ktrwjr
/war/common.jsp
Jarファイル追加
以下のjarファイルを「war/WEB-INF/lib」に追加します。
追加するjarファイル
aopalliance-1.0
commons-beanutils-1.7.0
commons-digester-2.0
commons-fileupload-1.2.1
commons-io-1.3.2
commons-lang-2.3
commons-logging-1.0.4
commons-validator-1.3.1
freemarker-gae-pre3
javassist
ognl-3.0
spring-aop
spring-beans
spring-context
spring-core
spring-tx
spring-web
struts2-convention-plugin-2.2.1.1
struts2-core-2.2.1.1
struts2-spring-plugin-2.2.1.1
xwork-core-2.2.1.1
freemarker-gae-pre3は、gae用のfreemarkerです。
Springは2.5を使用します。
web.xml
「war/WEB-INF」に配置するweb.xmlファイルです。



 
  slim3.rootPackage
  com.brightgenerous.s3.sample.data
 

 
  contextConfigLocation
  classpath:applicationContext*.xml
 

 
  struts2
  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  
   actionPackages
   com.brightgenerous.s3.sample.action
  
 

 
  struts2
  /*
 

 
  org.springframework.web.context.ContextLoaderListener
 
 
  com.brightgenerous.s3.sample.gae.GaeInitListener
 


Slim3の新規プロジェクトを作成して生成されるweb.xmlの内容のほとんどを消すことになります。
「com.brightgenerous.s3.sample.gae.GaeInitListener」は、独自に実装するクラスです。ソースは「実装するクラス」にて後述します。
applicationContext.xml
「classpath:applicationContext*.xml」(src直下)に配置します。




 

 

 
 

 
 
 
  
   
    PROPAGATION_REQUIRED
    PROPAGATION_REQUIRED
    PROPAGATION_REQUIRED
    PROPAGATION_REQUIRED,readOnly
   
  
 
 
  
  
 
 
  
   
    transactionInterceptor
   
  
  
   
    *ServiceImpl
   
  
 


「transactionManager」のみ用意してやれば他はそのまま使えます。なかなかナイスな設計です。
「com.brightgenerous.s3.sample.gae.GaeFixInternalPersistenceAnnotationProcessor」、「com.brightgenerous.s3.sample.gae.Slim3TransactionManager」については、後述です。
実装するクラス
1.com.brightgenerous.s3.sample.gae.GaeInitListener
package com.brightgenerous.s3.sample.gae;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import ognl.OgnlRuntime;

public class GaeInitListener implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener {

 @Override
 public void contextInitialized(ServletContextEvent sce) {
  OgnlRuntime.setSecurityManager(null);
 }

 @Override
 public void contextDestroyed(ServletContextEvent arg0) {
 }

 @Override
 public void sessionCreated(HttpSessionEvent arg0) {
 }

 @Override
 public void sessionDestroyed(HttpSessionEvent arg0) {
 }

 @Override
 public void attributeAdded(HttpSessionBindingEvent arg0) {
 }

 @Override
 public void attributeRemoved(HttpSessionBindingEvent arg0) {
 }

 @Override
 public void attributeReplaced(HttpSessionBindingEvent arg0) {
 }
}
OGNLのセキュリティマネージャがなんとかかんとか...理由については、詳しく調べていません。
2.com.brightgenerous.s3.sample.gae.GaeFixInternalPersistenceAnnotationProcessor
package com.brightgenerous.s3.sample.gae;

public class GaeFixInternalPersistenceAnnotationProcessor {

 public GaeFixInternalPersistenceAnnotationProcessor() {
 }
}
「java.lang.NoClassDefFoundError」を回避することだけが目的です。
3.com.brightgenerous.s3.sample.gae.Slim3TransactionManager
package com.brightgenerous.s3.sample.gae;

import java.io.Serializable;

import org.slim3.datastore.Datastore;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.support.AbstractPlatformTransactionManager;
import org.springframework.transaction.support.DefaultTransactionStatus;

import com.google.appengine.api.datastore.Transaction;

public class Slim3TransactionManager extends AbstractPlatformTransactionManager {

 private static final long serialVersionUID = 3423679590583692519L;

 @Override
 protected void doBegin(Object x_arg0, TransactionDefinition x_arg1) throws TransactionException {
  TransactionObject transactionObject = (TransactionObject) x_arg0;
  transactionObject.p_transaction = Datastore.beginTransaction();
 }

 @Override
 protected void doCommit(DefaultTransactionStatus x_arg0) throws TransactionException {
  TransactionObject transactionObject = (TransactionObject) x_arg0.getTransaction();
  transactionObject.p_transaction.commit();
 }

 @Override
 protected Object doGetTransaction() throws TransactionException {
  return new TransactionObject();
 }

 @Override
 protected void doRollback(DefaultTransactionStatus x_arg0) throws TransactionException {
  TransactionObject transactionObject = (TransactionObject) x_arg0.getTransaction();
  transactionObject.p_transaction.rollback();
 }

 private class TransactionObject implements Serializable {

  private static final long serialVersionUID = -8353097480608667182L;

  Transaction p_transaction;
 }
}
「org.springframework.jdbc.datasource.DataSourceTransactionManager」の実装を参考にして、Slim3用に簡単に対応させています。
このクラスのみ自作です。間違い等あれば指摘していただけるとありがたいです。

以上でStruts2 + Spring + Slim3の設定は完了です。
次回は、このフレームワークを使用して簡単なサンプルを書いてみます。

2011年3月22日火曜日

Hack For Japan

先日、3月21日はHack For Japnanに参加してきました。
京都会場のオフラインでの参加です。

今回は、これまでに何度か参加させていただいたHackathonとは趣旨が違いました。
Hackathonでは「お祭りのような感じで、その時間を楽しむ」のですが、 Hack For Japanでは、震災支援という「目的のために動く」のです。

参加を決めるに際して、おそらくは良い経験ができるだろうと思っていましたが、それ以上の貴重な経験がありました。
経験というより、体感です。
私のしょぼい語彙力では表現しきれませんが、あえて書かせてもらうと
「なんかすげー体験したのが良い経験になった」って感じです。
...書かないほうがよかったかな?


正直言って 役に立てませんでした。
以前から感じていたことですが、自分の弱点も明確になりました。
「準備無しに、いきなり走り出すと間違いなく転ぶ」
日ごろの準備が大切です。 そして、Launch and Iterateです。

最後にこれだけは言っておきます。
Hack For Japanは終わってません。始まったばかりです。
私にとってもこれからです。 いったん走り出してしまえば、自分はできる奴...だと思う。

2011年3月21日月曜日

Struts2 + Spring + MyBatis (その2)

Struts2 + Spring + MyBatis (その1)の続きです。

WEBアプリケーション開発で使用するフレームワークをStruts2.2 + Spring2.5 + MyBatis2.3 で構成してみます。
(その2)では、各xmlファイルとpropertiesファイルを揃えます。

必要な設定ファイルを記載しておきます。
(省略)と書かれているファイルの内容については、一例を記述する程度に留まるので説明しません。詳細について知りたい場合は、解説している他サイトを参照することをお勧めします。
設定ファイル
web.xmlServletの配備記述子
struts.xmlStruts2の設定ファイル(省略)
struts.propertiesStruts2のパラメータファイル(省略)
applicationContext.xmlSpringの設定ファイル
detabase.propertiesデータベースのパラメータファイル(省略)
sqlMapConfig.xmlMyBatisの設定ファイル(省略)
tiles.xmlTilesの設定ファイル(省略)
web.xml
「WEB-INF」フォルダ内にある配備記述子です。これが無いと始まりません。
どのプロジェクトでも共通した記述なので、ほぼそのままコピーすれば使えます。
Servlet2.5の構文で書いてみます。



 
  contextConfigLocation
  classpath:applicationContext*.xml
 

 
  struts2
  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  
   actionPackages
   sample.action
  
 

 
  struts2
  /*
 

 
  JspSupportServlet
  org.apache.struts2.views.JspSupportServlet
  1
 

 
  JspSupportServlet
  /*
 

 
  org.springframework.web.context.ContextLoaderListener
 
 
  org.apache.struts2.tiles.StrutsTilesListener
 

 
  30
 

 
  index.html
  index.jsp
 

 
  
   http://tiles.apache.org/tags-tiles
   /WEB-INF/tld/tiles-jsp.tld
  
 


Struts2とSpringを使用するには、上記のように記述します。
画面(View)の実装では、JspではなくFreeMarkerを使用します。 FreeMarker内でJspのtaglib(tiles等)を使用するためには、「JspSupportServlet」を動作させておく必要があります。
applicationContext.xml
applicationContex.xmlはweb.xmlに記述した「classpath:applicationContext*.xml」で参照される場所に配置します。
以下の内容は記述例です。



 

 
 

 
  
   
    classpath:database.properties
   
  
 

 
 
  
  
  
  
  
  
 

 
 
  
 
 
  
   
    PROPAGATION_REQUIRED
    PROPAGATION_REQUIRED
    PROPAGATION_REQUIRED
    PROPAGATION_REQUIRED,readOnly
   
  
 
 
  
  
 
 
  
   
    transactionInterceptor
   
  
  
   
    *ServiceImpl
   
  
 

 
 
  
  
 


MyBatis(iBatis)をSpringの管理下に置きます。
Springがトランザクションの管理(Begin、Rollback、Commit等)を、MyBatisがSQLの実行のみを担当するといった役割分担を可能にするためです。
そうすることで、MyBatisをSlim3に置き換えた場合にも同じ設計で実装できます。

大半を省略しましたが、設定ファイルについては以上です。
次回は、Google App Engine上でSlim3と連携させてみます。

2011年3月20日日曜日

Struts2 + Spring + MyBatis (その1)

WEBアプリケーション開発で使用するフレームワークをStruts2.2 + Spring2.5 + MyBatis2.3 で構成してみます。
最終的には、MyBatisをSlim3に置き換えてGoogle App Engine上で動作させるまでを予定しているので、そのための手順も含まれています。
(その1)では、必要なjarファイルの選定をします。

使用するフレームワークとそのバージョンを記載しておきます。
フレームワークとバージョン
Struts2Struts2.2.1.1
SpringSpring2.5.6
MyBatisMyBatis2.3.6

空のWebアプリケーションのプロジェクトを作成しておきます。
「Eclipse IDE for Java EE Developers」の「Dynamic Web Project」から新規作成を行うと簡単です。

Struts2
公式サイトからStruts2.2.1.1をダウンロードします。
ダウンローが完了したらファイルを解凍し、使用するjarファイルを選定してプロジェクトの「WEB-INF/lib」フォルダ内にコピーします。
使用するjarファイル
aopalliance-1.0
commons-beanutils-1.7.0
commons-digester-2.0
commons-fileupload-1.2.1
commons-io-1.3.2
commons-lang-2.3
commons-logging-1.0.4
commons-validator-1.3.1
freemarker-2.3.16
ognl-3.0
struts2-convention-plugin-2.2.1.1
struts2-core-2.2.1.1
struts2-json-plugin-2.2.1.1
struts2-spring-plugin-2.2.1.1
struts2-tiles-plugin-2.2.1.1
tiles-api-2.0.6
tiles-core-2.0.6
tiles-jsp-2.0.6
xwork-core-2.2.1.1
開発を進めていくうちに上記以外のjarファイルが必要になる事がありますが、その時点で追加します。
struts2にはspringのjarファイルも含まれていますが、springのjarファイルは別途用意します。
javassist
source forgeサイトからJavassist3.14をダウンロードします。
ダウンローが完了したらファイルを解凍し、javassist.jarファイルをプロジェクトの「WEB-INF/lib」フォルダ内にコピーします。
Spring
公式サイトからSpring2.5.6をダウンロードします。
ダウンローが完了したらファイルを解凍し、使用するJarファイルを選定してプロジェクトの「WEB-INF/lib」フォルダ内にコピーします。
使用するJarファイル
spring-aop
spring-beans
spring-context
spring-core
spring-tx
spring-web
MyBatis
公式サイトからMyBatis2.3.5をダウンロードします。
ダウンローが完了したらファイルを解凍し、mybatis-2.3.5.jarファイルをプロジェクトの「WEB-INF/lib」フォルダ内にコピーします。

以上でjarファイルの選定は終わりです。
(その2)では、各フレームワークの設定ファイルであるxmlについて書きます。