1。準備
開始する前に、前の記事を参照してください。
struts2.3.24 + sprut4.1.6 + hibernate4.3.11 + mysql5.5.25開発環境の構築と関連する説明は同じですが、struts2はスプリングMVCに置き換えられます
2。異なる場所
プロジェクトディレクトリとJARパッケージ:
アクションパッケージをコントローラーに変更します。
Struts2 JARパッケージを削除し、Spring MVCパッケージを追加します(すでに存在している場合は、追加する必要はありません)。
web.xml構成:
以前との違いは、Struts2フィルターがサーブレットに置き換えられることです。主な目的は、URLをルーティングし、処理のためにスプリングMVCに引き渡すことです。
<?xml version = "1.0" encoding = "utf-8"?> <web-app xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://java.sun.com/xml/nns/javaee xsi:schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/ns/javaee/web-app_0.0.0.xsd" "id" "id" id "id" webaee/wweb-app_0.xsdd <display-name> ssh </display-name> <context-param> <param-name> contextconfiglocation </param-name> <param-value> classpath:applicationContext.xml </param-value> </context-param> <リスナー> <リスナークラス> org.springframework.web.context.context.context.context.context.context.contextlodederlisterer <welcome-file-list> <welcome-file> index.jsp </welcome-file> </welcome-file-list> <servlet> <servlet-name> springmvc </servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet </servlet-configlocon> <param-value> classpath:springmvc-servlet.xml </param-value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name> springmvc </servet-name> <url-pattern>*
ApplicationContext.xml構成:
違いは、主に自動スキャンを構成する場合、@Controllerコンポーネントを除外する必要があります。これらの豆は、スプリングMVCによって生成されます。
他の構成は、前の記事と同じです。
<?xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:p = "http://www.springframework.org/schema/P" xmlns:Context = "http://www.springframework.org/schema/context" xmlns:tx = "http://www.springframework.org/schema/tx" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns:aop = "http://www.springframework.org/schema/aop" xmlns:jdbc = "http://www.springframework.org/schema/jdbc" xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schean http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd http://www.springframework.org/schema/context/spring-context-4.1.xsd "> <! - 注釈付きコンポーネントのクラスパスをスキャンSpringMVC構成ファイル - > <コンテキスト:除外:除外型= "annotation" expression = "org.springframework.controller" /> < /context:component-scan> <! - データソースの選択 - > <bean name = "datasource" destroy-method = "close" value = "com.mysql.jdbc.driver" /> <プロパティ名= "jdbcurl" value = "jdbc:mysql:// localhost:3306 /demo" /> <property name = "user" value = "root" /> <プロパティ名= "パスワード" value = "root" /> <propertion namement = "" quairemcrement " name = "initialpoolsize" value = "80"> </property> <property name = "maxidletime" value = "60"> </property name = "maxpoolsize" value = "80"> </property name = "minpoolsize" value = "50"> </property name = "ockireretrytorytrytorytrytorytrytorytrytorytrytorytorytrytorytrytorytrytorytrytorytrytorytrytrytydelay" 1000 "" 1000 " name = "AcquireretryThempts" value = "60"> </property> <property name = "breakafteracquirefailure" value = "false"> </property> <! - 構成ファイルの変更に注意してくださいmysqlのmy.ini ini of Connections構成の最大数を増やすために(現在の接続を参照してください: name = "dataSource" ref = "dataSource"/> <プロパティname = "hibernateproperties"> <props> <prop key = "hibernate.dialect"> org.hibernate.dialect.mysqldialect </prop> <prop key = "hibernate.show_sql" key = "hibernate.hbm2ddl.auto"> update </prop> <prop key = "hibernate.hbm2ddl.auto"> update </prop> <prop key = "connection.pool_size"> 10 </prop> <prop key = "current_session_context_class" key = "hibernate.cache.use_second_level_cache"> true </prop> <prop key = "hibernate.cache.region.factory_class"> org.hibernate.cache.ehcache.ehcacheregionfactory </prop> <prop key = "hibernate.cache.cache. key = "hibernate.cache.provider_configuration_file_resource_path"> ehcache.xml </prop> </props> </property name> <property name = "mappinglocations"> <balue> classpath:ssh/model/user.hbm.xml </propertion> </compropation> <value> ssh.model.user </value> </list> </property> </bean> <! - configure transaction manager-> <bean id = <bean name "> <property name =" sessionfactory "ref =" sessionfactory "/>> </bean> <! - トランザクションの宣伝特性-> <tx:" txadvice " Transaction-Manager = "TransactionManager"> <tx:attributes> <tx:method name = "add*" propagation = "read-only =" false "lollback-for =" java.lang.exception "/> <tx:method name =" delete*"propagation =" "read-only =" lollback-fials name = "delete*" propagation = "required" read-only = "false" lollback-for = "java.lang.exception"/> <tx:method name = "update*" propagation = "readolly =" false "lollback-for =" java.lang.exception "/> <tx:メソッド=" Rollback-for = "java.lang.exception"/> </tx:astributes> </tx:advise> <aop:config> <aop:aop:aop: "pcmethod" expression = "execution(*ssh.service ..*。 </aop:config> <! - カスタムAOP処理テスト - > <bean id = "aoptest"> </bean> <bean id = "myaop"> </bean> <aop:config proxy-target-class = "true"> <aop:aspect ref = "myaop"> <aop id = "pcmethodtest" "execriptes =" execution ssh.aop.aoptest.test*(..)) "/> <aop:before pointcut-ref =" pcmethodtest "method =" beffore "/> <aop:pointcut-ref =" pcmethodtest "method =" aby "/> </aop:aspect> </aop> </beans> </beans> <
springmvc-servlet.xml構成:
ssh.controllerパッケージの下で@controllerの自動スキャンを構成します。ここでは、ApplicationContext.xmlで構成された除外フィルターを復元する必要があります。
ビューパーサーを構成しますが、多くのパーサーがあります。ここでは、internalResourceViewResolverを例として取ります。
<?xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:context = "http://www.springframework.org/schema/context" XMLNS:MVC = "http://www.springframework.org/schema/mvc" xmlns:p = "http://www.springframework.org/schema/p" " xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns:tx = "http://www.springframework.org/schema/tx" xsi = " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd "> <! - このパッケージの下でのすべての豆の自動スキャンを開始する(@controllerなど) - > <コンテキスト:コンポーネント-scanベースパッケージ="スキャンパスssh.controller-> <コンテキスト:include-filter type = "annotation" expression = "org.springframework.steretype.controller"/> </context:component-scan> <! - define view parser-> <bean> <property name = "prepix"> <balut>/jsp/</</</</</</< <value> .jsp </value> </property> </beans>
コントローラーを書き込む:
スプリングMVCはStruts2の交換に使用されるため、アクションパッケージはありません。新しいコントローラーパッケージを削除して作成します。パッケージの下に新しいusercontrollerクラスを作成します。
@RequestMapping:Map URL;
@ResponseBody:コンテンツはボディとして直接返されます。
usercontroller.java
パッケージssh.controller; import java.io.printwriter; import java.util.list; import javax.annotation.resource; Import javax.http.ervletrequest; Import javax.servlet.httpsertresponse; import org.apache.log4j.log.log.logger.logger.log.logger.log.logger.log.logger. org.springframework.stereotype.controller; Import org.springframework.web.bind.annotation.requestmapping; Import org.springframework.web.bind.annotation.responsebody; import ssh.aop.aoptest; import sshsh.model.ssh.ssh.suser.service com.google.gson.gson;@controller@requestmapping( "/user")public class usercontroller {logger logger = logger.getlogger(usercontroller.class); @Resourceプライベートユーザーサービスユーザーサービス。 @ResourceプライベートAOPTEST AOPTEST; @RequestMapping(value = "/adduser")@responsebody public void adduser(httpservletrequest request、httpservletresponse応答){printwriter out = null; try {Response.setContentType( "text/html; charset = utf-8");文字列アカウント= request.getParameter( "アカウント");文字列名= request.getParameter( "name");文字列アドレス= request.getParameter( "アドレス"); user user = new user(); user.setAccount(account); user.setAddress(アドレス); user.setname(name); userservice.add(user); out = response.getWriter(); out.write(new gson()。tojson( "success")); } catch(Exception e){e.printstacktrace(); logger.error(e.getmessage()); if(out!= null)out.write(new gson()。tojson( "fail")); }最後に{out.flush(); out.close(); }} @requestMapping(value = "/queryuser")@responsebody public void queryalluser(httpservletrequest request、httpservletresponse応答){printwriter out = null; aoptest.test1(); aoptest.test2(); try {Response.setContentType( "text/html; charset = utf-8"); gson gson = new gson();リスト<user> userlist = userservice.queryalluser(); string gsonstr = gson.tojson(userlist); out = response.getWriter(); out.write(gsonstr); } catch(Exception e){e.printstacktrace(); logger.error(e.getmessage()); if(out!= null)out.write(new gson()。tojson( "fail")); }最後に{out.flush(); out.close(); }}} 3。プログラムを実行します
プログラムを実行し、ユーザーを追加し、ユーザーをクエリします。関数は正常です。
さらに、セカンダリキャッシュも正常に機能し、データベースは2番目のクエリでは動作しなくなりました。
@author風のようなコーダー
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。