スプリングフレームワークの7つの主要なモジュールの簡単な紹介
春のMVCモジュールコードの詳細な説明
SpringのWebモジュールは、Struts1、Struts2、JSFなどのWebフレームワークを統合するために使用されます。
struts1を統合します
継承方法
Springフレームワークは、Struts1のアクションをサポートするActionSupportクラスを提供します。 ActionSupportを継承した後、SpringのBeanFactoryを取得し、さまざまなスプリングコンテナでさまざまなリソースを取得できます。
org.springframework.web.struts.actionsupportをインポートします。 public class catactionはactionsupportを拡張します{public icatservice getCarservice(){return(icatservice)getWebapplicationContext()。getBean( "catservice"); } public ActionForward execute(ActionMappingMapping、ActionForm Form、httpservletRequest request、httpservletResponsePonse){catform catform =(catform)form; if( "list" .equals(catform.getAction())){returnthis.list(mapping、form、request、response); }} public ActionForwardリスト(ActionMappingMapping、ActionForm Form、httpservletRequest request、httpservletResponsePonse){catform catform =(catform)form; icatservice catservice = getCatservice(); list <cat> catlist = catservice.listcats(); request.setattribute( "carlist"、catlist); return mapping.find( "list"); }}web.xmlのスプリング構成
<context-param> <! - スプリング構成ファイルの場所 - > <param-name> contextconfiglocation </param-name> <param-value> /web-inf/classes/applicationcontext.xml </param-value> </context-param-> <リスナー> <! org.springframework.web.context.contextloaderlistener </ristener-class> </ristener> <filter> <! - spring独自のキャラクターフィルターを使用 - > <filter-name> carpitalencodingfilter </filter-name> <filter-class> org.springframework.wiltter.charecterencodingdingfilter <param-name> encoding </param-name> <param-value> utf-8 </param-value> </init-param> <init-param> <init-mame> forceconding </param-name> <param-value> true </parm-value> </init-param> </filter-mapting> <フィルターマッピング> <フィルターマッピング> <フィルターマッピング> <フィルターマッピング<url-pattern>/*</url-pattern> </フィルターマッピング>
Hibernateと組み合わせて使用する場合は、Web.xmlにOpenSessionInviewFilterフィルターを追加して、セッションスコープを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>*。
エージェントメソッド
継承方法を春に統合することは非常に簡単ですが、不利な点は、コードが春と結合されており、アクションが管理のために春に引き渡されないため、春のAOPとIOC機能を使用できないことです。プロキシメソッドを使用すると、これらの欠陥を回避できます。
パブリッククラスのキャティションはアクションを拡張します{//ここに継承されるアクションプライベートIcatservice catserviceはcatserviceです。 // Setter and Getterわずかにpublic Public ActionForward execute(ActionMappingMapping、ActionForm Form、httpservletRequest request、httpservletResponsponse){catform catform =(catform)form; if( "list" .equals(catform.getAction())){returnthis.list(mapping、form、request、response); }} public ActionForwardリスト(ActionMappingMapping、ActionForm Form、httpservletRequest request、httpservletResponsponse){catform catform =(catform)form; icatservice catservice = getCatservice(); list <cat> catlist = catservice.listcats(); request.setattribute( "carlist"、catlist); return mapping.find( "list"); }}このアクションは春と結合されるのではなく、Icatserviceプロパティを定義するだけで、春までに注入されます
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 Strutsのアクションをスプリングエージェントに渡す構成 - > <コントローラーProcessorClass = "org.springframework.web.struts.delegatingRequestProcessor" /> <! - コントローラーの構成が効果をもたらした後、アクションのタイププロパティが使用されます。 Strutsは、Typeプロパティで指定されたクラスを使用してキャテラクションを作成するのではなく、Spring構成で検索します。したがって、カタクションは春に構成する必要があります - > <! - 春のアクションは、IDの代わりに名前プロパティを使用します。 Springは「/cat.do」要求を傍受し、セッターメソッドを介してキャットサービスにキャットサービスを注入し、execute()メソッドを呼び出します - > <bean name = "/cat"> <プロパティ名= "catservice" ref = "catservice"/> </bean>
web.xmlの構成は、上記の継承方法と同じです。
プロキシメソッドアクションを使用すると、メソッドの前後のインターセプターの前後のキャピタクションの構成など、インターセプターなどのスプリング機能を構成できます。
<bean id = "catbeforeinterceptor"> <プロパティ名= "アドバイス"> <bean/> </property> <プロパティ名= "mappedname" value = "*"> </property> </bean> <bean id = "catafterInterceptor"> <プロパティ名= "アドバイス"> <bean/> </propertion> <property = </bean "> name = "/cat"> <プロパティ名= "interceptornames"> <list> <value> catbefore interceptor </value> <value> catafterInterceptor </value> </list> </jepperty name> <property name = "target"> <bean> <property name = "catservice" ref = "catservice">
Struts 2を統合します
Sprutの統合Struts 2にはStruts2-Spring-2.011.Jarパッケージが必要です
Public Class Cataction {private icatservice catservice;プライベート猫の猫; // setter and getter public string list(){catservice.listcats(); 「リスト」を返します。 } public string add(){catservice.createcat(cat); return list(); }}struts.xml構成
通常の構成に加えて、struts.objectFactoryの定数を追加し、springに値を設定して、アクションが春までに生成されることを示す必要があります。次に、<アクション/>のクラス属性をCATACTIONに変更して、Struts2は春にCatactionという名前のBeanを検索します。
<constant name = "struts.objectfactory" value = "spring"/> <packagenamepackagename = "cat" extends = "struts-default"> <action name = "*_ cat" method = "{1}"> <param name = "action"> {1} </param> <result.jsp </result ams </package>スプリング構成
<bean id = "actaction" scope = "prototype"> <プロパティ名= "catservice" ref = "catservice"> </property> </bean>
web.xml構成
<context-param> <! - スプリング構成ファイルの場所 - > <param-name> contextconfiglocation </param-name> <param-value> /web-inf/classes/applicationcontext.xml </param-value> </context-param-> <リスナー> <! org.springframework.context.contextloaderlistener </ristener-class> </ristener> <filter> <filter-name> struts2 </filter-name> <filter-class> org.apache.struts2.dispatcher.filterdispatcher </filter> </filter> </filter> </フィルター> <url-pattern>/*</url-pattern> </フィルターマッピング>
要約します
上記は、SpringのWebモジュール構成の詳細な説明です。誰にとっても役立つことを願っています。興味のある友人は、このサイトの他の関連トピックを引き続き参照できます。欠点がある場合は、それを指摘するためにメッセージを残してください。
参照:
SpringMVCのページジャンプの問題に関する簡単な議論
スプリングAOPはじめにデモ共有
Spring Framework Webプロジェクト実践的なコード共有