SpringのWEBモジュール配置の詳細

8155 ワード

Springフレームの7つのモジュールを簡単に紹介します.
SpringにおけるMVCモジュールコードの詳細
SpringのWEBモジュールは、Struts 1、Struts 2、JSFなどのWebフレームを統合するために使用されます.
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 catList =catService.listCats(); 
    request.setAttribute("carList",catList); 
 
    return mapping.find("list"); 
  } 
} 
Springのweb.xmlの構成

 
  contextConfigLocation 
  /WEB-INF/classes/applicationContext.xml 
 
 
 
   
    org.springframework.web.context.ContextLoaderListener 
   
 
 
 
  CharacterEncodingFilter 
  org.springframework.web.filter.CharacterEncodingFilter 
   
    encoding 
    UTF-8 
   
   
    forceEncoding 
    true 
   
 
 
  CharacterEncodingFilter 
  /* 
 
Hibernateと併用する場合は、OpenSession InViewelterフィルタをweb.xmlに追加し、session範囲をJSP層に拡大し、スロー遅延負荷異常を防止する必要があります.

 
  hibernateFilter 
  org.springframework.orm.hibernate3.support. OpenSessionInViewFilter 
 
 
   hibernateFilter 
  *.do 
 
プロキシ方式
継承方式は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 catList =catService.listCats(); 
    request.setAttribute("carList",catList); 
 
    return mapping.find("list"); 
  } 
} 
このアクションはSpringとは結合されていません.ICatoService属性を定義しただけで、Springが注入を担当します.
struts-congfig.xml配置

 
   
 
 
 
   
     
   
 
 
 
 
 
 
 
 
   
 
web.xmlの構成は上の継承方式と同じです.
プロキシ方式のアクションを使用して、スクリーンセーバなどのSpring特性を構成することができ、例えば、CatActionの設定方法の前ブロックとリターンスクリーンセーバのような.

 
   
     
   
   
 
 
 
   
     
   
   
 
 
 
   
     
      catBeforeInterceptor 
      catAfterInterceptor 
     
   
   
     
      
     
   
 
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から発生することを示す.その後、class属性をcatActionに変更し、Struts 2はSpringにcatActionというbeanを探しに行きます.

 
 
 
 
  {1} 
  /list.jsp 
  /list.jsp 
 
 
Spring配置

 
   
 
web.xml設定

 
  contextConfigLocation 
  /WEB-INF/classes/applicationContext.xml 
 
 
 
   
    org.springframework.web.context.ContextLoaderListener 
   
 
 
 
  Struts2 
  org.apache.struts2.dispatcher.FilterDispatcher 
 
 
   Struts2 
  /* 
 
締め括りをつける
以上が本文のSpringのWEBモジュールの配置についての詳しい内容です.皆さんの助けをお願いします.興味のある方は引き続き当駅の他のテーマを参照してください.
参考:
Springmvcのページジャンプの問題について話します.
Spring AOP入門Demoシェア
Springフレームワークwebプロジェクトの実戦全コード共有