STEP4.プレゼンテーション層INDEX1.SpringとStrutsの連携Struts部分については、@IT連載のStrutsを使うWebアプリケーション構築術で紹介されている、書籍情報の登録・更新機能を参考にしました。
まず、struts-config.xmlの設定に以下を追加します。
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml,
/WEB-INF/applicationContext-dao.xml" />
</plug-in>
2.DelegatingActionProxyを使う。strutsとの連携方法はいくつかありますが、今回は、DelegatingActionProxyを使ってみます。 <action-mappings>:の設定を以下のように修正しました。例えば、/BookViewActionがリクエストされると、DelegatingActionProxyクラスが呼び出されます。
<action path="/BookViewAction"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="success" path="/book/BookView.jsp" />
</action>
<action path="/BookWriteViewAction"
type="org.springframework.web.struts.DelegatingActionProxy"
name="BookUpdateViewForm"
scope="request">
<forward name="success" path="/book/BookUpdate.jsp" />
</action>
<action path="/BookWriteAction"
type="org.springframework.web.struts.DelegatingActionProxy"
name="BookUpdateForm"
scope="request"
input="/book/BookUpdate.jsp"
validate="true">
<forward name="success" path="/BookViewAction.html" />
</action>
3.Bean定義ファイルの追加Bean定義ファイル([APPHOME]/WEB-INF/applicationContext.xml )にStrusのアクションクラスの定義を追記します。 このようにしておくと、struts-config.xmlで定義した、pathに対してActionクラスが利用されます。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/database.properties</value>
</list>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${database.connection.driver_class}</value>
</property>
<property name="url">
<value>${database.connection.url}</value>
</property>
<property name="username">
<value>${database.connection.username}</value>
</property>
<property name="password">
<value>${database.connection.password}</value>
</property>
</bean>
<!-- Transaction -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource"><ref bean="dataSource"/></property>
</bean>
<bean id="txProxyTemplate" lazy-init="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="bookInfoService" parent="txProxyTemplate">
<property name="target">
<bean class="com.powerdee.service.BookInfoServiceImpl">
<property name="bookInfoDao">
<ref bean="bookInfoDao"/>
</property>
</bean>
</property>
</bean>
*** ここから追加部分 ***
<bean name="/BookViewAction"
class="com.powerdee.presentation.book.BookViewProcess">
<property name="service">
<ref bean="bookInfoService"/>
</property>
</bean>
<bean name="/BookWriteViewAction"
class="com.powerdee.presentation.book.BookUpdateViewProcess">
<property name="service">
<ref bean="bookInfoService"/>
</property>
</bean>
<bean name="/BookWriteAction"
class="com.powerdee.presentation.book.BookUpdateProcess">
<property name="service">
<ref bean="bookInfoService"/>
</property>
</bean>
*** ここまで ***
</beans>
4.Actionクラスの実装ここでは、サンプルとして /BookWriteActionがリクエストされたときに呼び出される、BookUpdateProcess クラスを見てみます。 SpringのBean定義ファイルで指定しているプロパティに対応して、変数serviceの定義とそれのsetterメソッドを実装しています。オブジェクトの生成と取得はSpringが行ってくれますので、BookUpdateProcessクラス内では、IBookInfoServiceインターフェイスのみで、サービスクラスを利用できます。 サービスクラスの変更は、Bean定義ファイルのみで可能となっています。
package com.powerdee.presentation.book;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.powerdee.entity.BookInfo;
import com.powerdee.service.IBookInfoService;
public final class BookUpdateProcess extends Action {
IBookInfoService service;
public ActionForward execute (ActionMapping map,
ActionForm fm,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
// アクションフォームBeansクラスを取得
BookUpdateForm objFrm = (BookUpdateForm) fm;
// BookInfoクラスを介して、入力データをデータベースに登録(更新)
BookInfo objBok = new BookInfo();
objBok.setIsbn(objFrm.getIsbn());
objBok.setTitle(objFrm.getTitle());
objBok.setAuthorName(objFrm.getAuthorName());
objBok.setPrice(new Integer(objFrm.getPrice()));
objBok.setPublish(objFrm.getPublish());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
objBok.setPublicationDay(sdf.parse(objFrm.getPublicationDay()));
service.saveBookInfo(objBok);
return map.findForward("success");
}
public void setService(IBookInfoService service) {
this.service = service;
}
}
おすすめ書籍
|