初期のプロジェクトは比較的単純で、ほとんどがJSP、サーブレット + JDBCを直接扱っていました。その後、Struts1(Struts2)+Spring+Hibernateを使用して、レイヤー化の概念に従ってプロジェクト開発を厳密に駆動しました。今回は、Spring MVCを使用して、開発のためにストラットを置き換えました。
MVCはすでに現代のWeb開発の非常に重要な部分です。 SpringMVC+Spring3+Hibernate4の開発環境構造を紹介しましょう。
プロジェクト構造を見てみましょう。
特定のコードはもはや実証されません。主に非常に普通のルート、MVC-Servcie-Dao-Hibernateの構造を使用します。主に構成ファイルを見て、ソースコードをダウンロードできます。説明についてはコメントを参照してください
web.xml
<?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 xmlns:web = "http://java.sun.com/xml/ns/javaee" xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id = "webapp_id"バージョン= "2.5" <welcome-file> index.jsp </welcome-file> </welcome-file-list> <! - spring-> <context-param> <param-name> contextconfiglocation </param-name> <param-value> classpath*:config/spring - *。 <リスナークラス> org.springframework.web.context.contextloaderlistener </ristener-class> </risteners <int-param> <param-name> contextconfiglocation </param-name> <param-value> classpath*:config/spring-servlet.xml </param-value> </init-s-startup> 1 </load-on-startup> </servet> <servlet-mapping> <servlet-name> </servlet-mapping> <! - 文字セット - > <filter> <filter-name> encodingfilter </filter-name> <filter-class> org.springframework.web.filter.characterencodingfilter </filter-class> <init-param> <param-name> ecoding </param-balue> <param-balue> </param-mamue> <Init-Param> <param-name> forceConding </param-name> <param-value> true </param-value> </init-param> </filter> <filter-name> encodingfilter </filter-name> <url-pattern>/*</url-pattern> </filter-maps> <filter-class> org.springframework.orm.hibernate4.support.opensessioninviewfilter </filter-class> </filter> <filter-mapping> <filter-name> opensession> <url-pattern>/*</url-pattern> </filt-mapping> </web-app>
spring-servlet.xml
<?xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:context = "http://www.springframework.org/schema/context" xmlns:p = "http://www.springframework.org/schema/p" xmlns:mvc = "http://www.springframework.org/schema/mvc" " xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context.context.xd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <! - 注釈スキャンパッケージ - > <! - アノテーションメソッド処理 - > <! - <bean/> - > <! - アノテーションクラスマッピング処理 - > <! - <bean> </bean> - > <! - オープンアノテーションスキーム2-> <MVC:アノテーション駆動型/> <! - 静的リソースアクセス、ソリューション1-> <MVC:<mvc/<mvc/"/"/"/"/"/>場所= "/js/" mapping = "/js/**"/> <! - 静的リソースアクセス、スキーム2-> <! - <mvc:default-servlethandler/> - > <! - view rettrenation class-> <bean id = "viedResolver"> <プロパティ名= "preperix" value = "/" <プロパティname = "suffix" value = "。jsp"> < /property> < /bean> <! - file bean - > <! - <bean id = "multipartresolver"> <プロパティ名= "デフォルトエンコード"値= "utf-8" /> <プロパティ名= "maxuploadsize"値 "値" value = "40960"/> </bean> - > </beans>
spring-hibernate.xml
<?xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:context = "http://www.springframework.org/schema/context" xmlns:p = "http://www.springframework.org/schema/p" xmlns:mvc = "http://www.springframework.org/schema/mvc" " xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context.context.xd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <! - データソースの構成 - > <bean id =" datasource "> <propertial name = value = "com.mysql.jdbc.driver" /> <プロパティ名= "url" value = "jdbc:mysql://127.0.1/springmvc" /> <プロパティ名= "username" value = "root" /> <プロパティname = "パスワード" id = "SessionFactory"> <プロパティ名= "dataSource" ref = "dataSource"/> <プロパティ名= "hibernateproperties"> <props> <prop key = "hibernate.dialect"> org.hibernate.dialect.mysqldialect </prop> <prop </prop </prop> auto key = "hibernate.show_sql"> true </prop> <prop key = "hibernate.format_sql"> true </prop> </prop> </property name = "configlocations"> <bule> classpath*:config/hibernate/hibernate.cfg.xml </value < <bean id = "transactionManager"> <プロパティ名= "SessionFactory" Ref = "sessionFactory"> </property> </bean> <! - トランザクションエージェントクラス - > <bean id = "transactionbese" lazy-init = "true" abst = "true"> <property name = "transactionmanager" = "transactionmanager"> <propecttr "> key = "add*"> propagation_required、-exception </prop> <prop key = "update*"> propagation_required、-excepure </prop> <prop key = "insert*"> propagation_required、exception </prop> <prop key = "insert*"> propagation_required、-exception </prop> <prop key = "Modify*"> propagation_required、-exception </prop> <prop key = "modify*"> propagation_required、-excepure </prop> <prop key = "delete*"> propagation_required、-exception </prop> <prop key = "del*"> propagation_requation </prop> prop> </prop> </prop> </props> </property> </bean> </bean>
Spring-Core.xml
<?xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:context = "http://www.springframework.org/schema/context" xmlns:p = "http://www.springframework.org/schema/p" xmlns:mvc = "http://www.springframework.org/schema/mvc" " xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context.context.xd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <! - 他の構成ファイルを紹介します。 </豆>
spring-user.xml
<?xml version = "1.0" encoding = "utf-8"?> <!doctype beans public " - // spring // dtd bean 2.0 // en" "" http://www.springframework.org/dtd/spring-beans-2.0.dtd "[< 「Org/Springframework/web/context/web-inf/contextinclude.xml ">]> <beans> <! - spring bean id =" userdao "> <プロパティ名=" sessionfactory "ref =" sessionfactory "> </property> </bean> <bean id =" usermanagerbase "> <propation" </bean> <! - 親はトランザクションベーゼであり、トランザクションがサポートされていることを示しています - > <bean id = "usermanager" parent = "transactionbese"> <プロパティ名= "ターゲット" ref = "usermanagerbase"> </property> </bean> </beans>
hibernate.cfg.xml
< </session-factory> </hibernate-configuration>
コントローラーを見てみましょう
パッケージcom.jialin.controller; java.io.ioexceptionをインポートします。 java.io.printwriterをインポートします。 java.util.listをインポートします。 javax.annotation.resourceをインポートします。 javax.servlet.http.httpservletrequestをインポートします。 javax.servlet.http.httpservletResponseをインポートします。 org.springframework.stereotype.controllerをインポートします。 org.springframework.web.bind.annotation.requestmappingをインポートします。 com.jianin.entity.userをインポートします。 com.jianin.service.iusermanagerをインポートします。 @controller // struts-like action @requestmapping( "/user")public class usercontroller {@resource(name = "usermanager") @RequestMapping( "/adduser")// struts like action-mapping public string adduser(user user){if(usermanager.adduser(user)){// redirect return "redirect:/user/getalluser"; } else {return "/fail"; }} @RequestMapping( "/updateUser")public string upproaduser(user user、httpservletrequest request){if(usermanager.updateuser(user)){user = usermanager.getounuser(user); request.setattribute( "user"、user); return "/useredit"; } else {return "/fail"; }} @RequestMapping( "/deluser")public void deluser(user user、httpservletresponse応答){string result = "{/" result/":/" error/"}"; if(usermanager.deluser(user)){result = "{/" result/":/" success/"}"; } printwriter out = null; Response.setContentType( "Application/JSON"); try {out = response.getWriter(); out.write(result); } catch(ioexception e){e.printstacktrace(); }} @requestMapping( "/toadduser")public string toadduser(){return "/useradd"; } @RequestMapping( "/toupdateuser")public string toupdateuser(user user、httpservletrequest request){user user1 = usermanager.getoneuser(user); request.setattribute( "user1"、user1); return "/useredit"; } @RequestMapping( "/getAlluser")public string getalluser(httpservletrequest request){list userlist = usermanager.getalluser(); request.setattribute( "userlist"、userlist); "/usermain"を返します。 }}ソースコードのダウンロード...
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。