2011年4月8日金曜日

Struts2 + Spring + Slim3 サンプル

今回は、 【修正版】 Struts2 + Spring + Slim3で構築したフレームワークを使ってサンプルのソースを書いてみます。

サンプルは簡単なデータ登録と表示の実装です。

作成するファイル一覧
今回、作成するファイルは以下の通りです。それ以外のファイルは一切触りません。
ファイル一覧
/src/com/brightgenerous/s3/sample/action/Sample_Action.java
/src/com/brightgenerous/s3/sample/action/Sample_Service.java
/src/com/brightgenerous/s3/sample/action/Sample_ServiceImpl.java
/src/com/brightgenerous/s3/sample/data/model/Account.java
/src/com/brightgenerous/s3/sample/data/meta/AccountMeta.java
/war/WEB-INF/content/sample/index.jsp
/war/WEB-INF/content/sample/show.jsp
「Account.java」と「AccountMeta.java」はSlim3のプラグインを使って自動生成します。
javaのソースコード
1.com.brightgenerous.s3.sample.action.Sample_Action.java
package com.brightgenerous.s3.sample.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import com.brightgenerous.s3.sample.data.model.Account;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("struts-default")
@Namespace("/sample")
public class Sample_Action implements ModelDriven {

 private Account p_model = new Account();

 @Autowired
 private Sample_Service p_service;

 @Override
 public Account getModel() {
  return p_model;
 }

 public List getAccounts() {
  return p_service.get();
 }

 /**
  * [Action:初期表示]
  */
 @Action("index")
 public String index() {

  return "index";
 }

 /**
  * [Action:表示]
  */
 @Action("show")
 public String show() {

  return "show";
 }

 /**
  * [Action:登録]
  */
 @Action("regist")
 public String regist() {
  p_service.regist(p_model);
  return show();
 }
}
2.com.brightgenerous.s3.sample.action.Sample_Service.java
package com.brightgenerous.s3.sample.action;

import java.util.List;

import com.brightgenerous.s3.sample.data.model.Account;

interface Sample_Service {

 Account regist(Account x_account);

 List<Account> get();
}
3.com.brightgenerous.s3.sample.action.Sample_ServiceImpl.java
package com.brightgenerous.s3.sample.action;

import java.util.List;

import org.slim3.datastore.Datastore;
import org.springframework.stereotype.Component;

import com.brightgenerous.s3.sample.data.meta.AccountMeta;
import com.brightgenerous.s3.sample.data.model.Account;

@Component
class Sample_ServiceImpl implements Sample_Service {

 @Slim3GlobalTransaction
 private GlobalTransaction p_tx;

 @Override
 public Account regist(Account x_account) {
  p_tx.put(x_account);
  return x_account;
 }

 @Override
 public List get() {
  AccountMeta meta = AccountMeta.get();
  return Datastore.query(meta).asList();
 }
}
4.com.brightgenerous.s3.sample.data.model.Account.java
package com.brightgenerous.s3.sample.data.model;

import java.io.Serializable;

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

import org.slim3.datastore.Attribute;
import org.slim3.datastore.Model;

@Model(schemaVersion = 1)
public class Account implements Serializable {

 private static final long serialVersionUID = 1L;

 @Attribute(primaryKey = true)
 private Key key;

 @Attribute(version = true)
 private Long version;

 private String nickname;

 private String email;

 /**
  * Returns the key.
  * 
  * @return the key
  */
 public Key getKey() {
  return key;
 }

 /**
  * Sets the key.
  * 
  * @param key the key
  */
 public void setKey(Key key) {
  this.key = key;
 }

 /**
  * Returns the version.
  * 
  * @return the version
  */
 public Long getVersion() {
  return version;
 }

 /**
  * Sets the version.
  * 
  * @param version the version
  */
 public void setVersion(Long version) {
  this.version = version;
 }

 public String getNickname() {
  return nickname;
 }

 public void setNickname(String x_nickname) {
  nickname = x_nickname;
 }

 public String getEmail() {
  return email;
 }

 public void setEmail(String x_email) {
  email = x_email;
 }

 @Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + ((key == null) ? 0 : key.hashCode());
  return result;
 }

 @Override
 public boolean equals(Object obj) {
  if (this == obj) {
   return true;
  }
  if (obj == null) {
   return false;
  }
  if (getClass() != obj.getClass()) {
   return false;
  }
  Account other = (Account) obj;
  if (key == null) {
   if (other.key != null) {
    return false;
   }
  } else if (!key.equals(other.key)) {
   return false;
  }
  return true;
 }
}
Slim3プラグインのantから「gen-model」を選択して、自動生成されたソースをもとにしています。
追加部分は「nickname」と「email」の変数と、そのgetter・setterだけです。
jspのソースコード
1./WEB-INF/content/sample/index.jsp
<%@page pageEncoding="UTF-8" isELIgnored="false" session="false"%>
<%@ taglib uri="/struts-tags" prefix="s" %>


 
 
 

 

 表示
2./WEB-INF/content/sample/show.jsp
<%@page pageEncoding="UTF-8" isELIgnored="false" session="false"%>
<%@ taglib uri="/struts-tags" prefix="s" %>


  
ニックネームEmail
戻る

以上で実装は完了です。

ここまで書いておいて、言ってしまうのですが、GlobalTransactionのインスタンスが直に注入されるのは気持ち悪いです。無理にSpringを使うことは無いと思い直しました。さらに、Springだけが原因かは調べていませんが、spin-upの時間もかなり掛かっています。

実行してみる
折角なので、動かしてみます。

Transactionは上手く機能しました。画像ではお見せできていませんが、例外が発生した場合は、ちゃんとrollbackしてくれています。

なんとかNo-configurationで書けたのですが、この方法を続けるにはDIを軽量化する必要があるのかなぁ...と思ってみたりみなかったり。

近いうちに、GWTとStruts2の連携についても書ければいいかな、なんて考えています。

0 件のコメント:

コメントを投稿