SpringのWEBモジュール
SpringのWEBモジュールは、Struts 1、Struts 2、JSFなどのWebフレームを統合するためのものです.
Struts 1を統合
継承方式
Springフレームは、アクションSupport類がStruts 1をサポートするアクションを提供しています.アクションSupportを継承することでSpringのBeanFactoryを取得し、各種Spring容器内の各種資源を獲得します.
プロキシ方式
継承方式はSpringに溶け込むのは簡単ですが、コードとSpringが結合していてアクションはSpringに管理されていないので、SpringのAOP、IoC特性は使えません.代理方式を使うとこれらの欠陥を回避できます.
struts-congfig.xml配置
プロキシ方式のアクションを使用して、スクリーンセーバなどのSpring特性を構成することができ、例えば、CatActionの設定方法の前ブロックとリターンスクリーンセーバのような.
Spring統合Struts 2 struts 2-spring-2.11.jarパッケージが必要です.
通常の構成に加えて、struts.object Factoryという定数を追加して、値をspringに設定して、このアクションがSpringから発生することを表します.そして「action/」のclass属性をcatActに変更し、Struts 2はSpringにcatActionというbeanを探しに行きます.
Spring配置
web.xml設定
Struts 1を統合
継承方式
Springフレームは、アクションSupport類がStruts 1をサポートするアクションを提供しています.アクションSupportを継承することでSpringのBeanFactoryを取得し、各種Spring容器内の各種資源を獲得します.
import org.springframework.web.struts.ActionSupport;
public class CatAction extends ActionSupport{
public ICatService getCarService(){
return (ICatService) getWebApplicationContext().getBean("catService");
}
public ActionForward execute(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){
CatForm catForm = (CatForm) form;
if("list".equals(catForm.getAction())){
returnthis.list(mapping,form,request,response);
}
}
public ActionForward list(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){
CatForm catForm = (CatForm) form;
ICatService catService =getCatService();
List<Cat> catList =catService.listCats();
request.setAttribute("carList",catList);
return mapping.find("list");
}
}
Springのweb.xmlの構成<context-param><!-- Spring -->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener><!-- Listener Spring -->
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter><!-- Spring -->
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Hibernateと併用する場合は、OpenSession InViewelterフィルタをweb.xmlに追加し、session範囲をJSP層に拡大し、スロー遅延負荷異常を防止する必要があります.<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support. OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name> hibernateFilter</filter-name>
<url-pattern>*.do</url-pattern><!-- Struts 1 Action -->
</filter-mapping>
プロキシ方式
継承方式はSpringに溶け込むのは簡単ですが、コードとSpringが結合していてアクションはSpringに管理されていないので、SpringのAOP、IoC特性は使えません.代理方式を使うとこれらの欠陥を回避できます.
public class CatAction extends Action{ // Struts 1 Action
private ICatService catService;
//setter、getter
public ActionForward execute(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){
CatForm catForm = (CatForm) form;
if("list".equals(catForm.getAction())){
returnthis.list(mapping,form,request,response);
}
}
public ActionForward list(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){
CatForm catForm = (CatForm) form;
ICatService catService =getCatService();
List<Cat> catList =catService.listCats();
request.setAttribute("carList",catList);
return mapping.find("list");
}
}
このアクションはSpringとは結合されていません.ICatoService属性を定義しただけで、Springが注入を担当します.struts-congfig.xml配置
<form-beans>
<form-bean name="catForm" type="com.clf.spring.CatForm">
</form-beans>
<action-mappings>
<action name=" catForm" path="/cat" type="com.clf.spring.CatAction">
<forward name="list" path="/jsp/listCat.jsp"></forward>
</action>
</action-mappings>
<!-- , Struts Action Spring -->
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
<!-- controller ,Action type ,Struts type CatAction, Spring , Spring CatAction -->
<!-- Spring Action name id,Spring "/cat.do" , catService setter CatAction , execute() -->
<bean name="/cat" class=" com.clf.spring.CatAction">
<property name="catService" ref="catService" />
</bean>
web.xmlの構成は上の継承方式と同じです.プロキシ方式のアクションを使用して、スクリーンセーバなどのSpring特性を構成することができ、例えば、CatActionの設定方法の前ブロックとリターンスクリーンセーバのような.
<bean id="catBeforeInterceptor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvodor">
<property name="advice">
<bean class="com.clf.spring.MethodBeforeInterceptor" />
</property>
<property name="mappedName" value="*"></property>
</bean>
<bean id="catAfterInterceptor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvodor">
<property name="advice">
<bean class="com.clf.spring.MethodAfterInterceptor" />
</property>
<property name="mappedName" value="*"></property>
</bean>
<bean name="/cat" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interceptorNames">
<list>
<value> catBeforeInterceptor</value>
<value> catAfterInterceptor</value>
</list>
</property>
<property name="target">
<bean class="com.clf.spring.CatAction">
<property name="catService" ref="catService"></property>
</bean>
</property>
</bean>
Struts 2を統合Spring統合Struts 2 struts 2-spring-2.11.jarパッケージが必要です.
public class CatAction{
private ICatService catService;
private Cat cat;
//setter、getter
public String list(){
catService.listCats();
return "list";
}
public String add(){
catService.createCat(cat);
return list();
}
}
struts.xml配置通常の構成に加えて、struts.object Factoryという定数を追加して、値をspringに設定して、このアクションがSpringから発生することを表します.そして「action/」のclass属性をcatActに変更し、Struts 2はSpringにcatActionというbeanを探しに行きます.
<constant name=" struts.objectFactory" value="spring" />
<packagename="cat" extends="struts-default">
<action name="*_cat" method="{1}" class="catAction">
<param name="action" >{1}</param>
<result>/list.jsp</result>
<result name="list">/list.jsp</result>
</action>
</package>
Spring配置
<bean id="catAction" scope="prototype" class="com.clf.spring.CatAction">
<property name="catService" ref="catService"></property>
</bean>
web.xml設定
<context-param><!-- Spring -->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener><!-- Listener Spring -->
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>Struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name> Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>