Les premiers projets étaient relativement simples, principalement traités directement avec JSP, Servlet + JDBC. Plus tard, Struts1 (Struts2) + Spring + Hibernate a été utilisé pour entraîner le développement du projet strictement selon le concept de superposition. Cette fois, Spring MVC a été utilisé pour remplacer les entretoises pour le développement.
MVC est déjà une partie très importante du développement Web moderne. Présentez la construction de l'environnement de développement de SpringMVC + Spring3 + Hibernate4.
Jetons un coup d'œil à la structure du projet:
Le code spécifique ne sera plus démontré, il prend principalement un itinéraire très ordinaire, la structure de MVC-Servcie-dao-Hibernate, le code source peut être téléchargé, examine principalement le fichier de configuration. Voir les commentaires pour explication
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/ns/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" version = "2.5"> <splay-name> Springmvc </ Display-name> <Leloy-File> index.jsp </ bienvenue-file> </ welcome-file-list> <! - Configurez Spring -> <Axte context-param> <param-name> contextConfiglocation </ Param-name> <Amam-Value> ClassPath *: config / Spring - *. Xml </ Param-Value> </ftext-Param> <Dever> <écouteur-classe> org.springframework.web.context.contextLoaderListener </ auteur-class> </diner> <! - Configurez SpringMvc -> <Servlet> <Servlet-Name> Springmvc </vrlet-name> <Servlet-Class> Org.Springframework.web.servlet.Dispatcherservlet </ Servlett-cass> <Web.Servlet.Dispatcherservlet </ Servlett-cass> <Web.Servlet.Dispatcherservlet </ Servlett-cass> <Web.Servlet.Dispatcherservlet </ Servlett-cass> <Web.Servlet.Dispatcher <Am param-name> ContextConfiglocation </ Param-name> <Am param-Value> CLASSPATH *: config / spring-servlet.xml </ param-value> </ init-param> <ony-on-startup> 1 </ Load-on-Startup> </vrlet> <url-mapping> <Servlet-Name> Springmvc </serplet-name> </vrlet-mapping> <! - Définir le jeu de caractères -> <filter> <filter-name> EncodingFilter </filter-Name> <Lilter-Class> org.springFramework.web.filter.CaracTerencodingFilter </filter-Class> <Init-Param> <Param-Name> Encoding </Amam-name> </1nit-Param> <Init-Param> <AmAn-Name> Forceencoding </onsam-name> <param-value> true </ param-Value> EncodingFilter </filter> <filter-mapping> <filter-name> EncodingFilter </filter-name> <url-stepter> / * </url-sattern> </frolter-mapping> <! <filter-name> openssesse </filter-name> <filter-class> org.springframework.orm.hibernate4.support.opencessionInviewFilter </filter-class> </filter> <filter-mapping> <filter-name> opensession </filter-name> <url-Pattern> / * </url-Pattern>
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/xmlma xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/springframeworks-3.0.xschemma/contex http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0. -> <Context: Component-Scan Base-Package = "com.jialin" /> <! - Open Annotation Scheme 1 -> <! - Annotation Method Processing-> <! - <Eban / Bean> -> <! - Annotation Class Mapping Processing -> <! - <ean> </ bean <MVC: Ressources location = "/ img /" mapping = "/ img / **" /> <mvc: ressources location = "/ js /" mapping = "/ js / **" /> <! - Accès statique des ressources, schéma 2 -> <! - <mvc: default-servlet-handler /> -> <! Value = "/"> </ propriété> <! - Peut être vide, pratique pour implémenter la logique de la classe d'interprétation de la vue en fonction de votre extension -> <propriété name = "suffixe" value = ". jsp"> </ propriété> </ank> <! - Téléchargez le fichier bean -> <! - <bean id = "multipartreSolver"> <propriété name = "defaultening" value = "utf-8" /> <wofetwaye value = "10485760000" /> <propriété name = "maxinMemorySize" value = "40960" /> </ank> -> </ beans>
printemps-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/xmlma xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/springframeworks-3.0.xschemma/contex http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0. Source -> <bean id = "dataSource"> <propriété name = "driverclassname" value = "com.mysql.jdbc.driver" /> <propriété name = "url" value = "jdbc: mysql: //127.0.0.1/springmvc" /> <propriété name = "username" value = "root" /> <propriété "" Value = "123456" /> </EAN> <! - Configurez Hibernate SessionFactory -> <Bean Id = "SessionFactory"> <propriété name = "DataSource" Ref = "DataSource" /> <propriété Name = "HiberNateProperties"> <props> <prop Key = "HiberNate.Dialect"> Org.Hibernate.Dialict.mysq. <prop key = "hibernate.hbm2ddl.auto"> update </ prop> <prop key = "hibernate.show_sql"> true </ prop> <prop> key = "Hibernate.format_sql"> true </ prop> </ prop> </ propriété> <propriété name = "configLocations"> <list> <value> CLASSPATH *: config / hibernate / hibernate.cfg.xml </value> </sist> </ propriété> </ bean> <! - Transaction Manager -> <bean id = "TransactionManager"> <propriété name = "SessionFactory" Ref = "SessionFactory"> </ Property> </ Bean> <! - Transaction Agent -> <Ean Id = "TransactionCese" Lazy-init = "true" Abstract => <Ean Id = "TransactionCese" Lazy-init = "true" Abstract => <Ean Id = "TransactionCese" LAZY INIT = "ABSTRAT <propriété name = "TransactionManager" ref = "TransactionManager"> </ Property> <propriété name = "TransactionAttributes"> <propS> <Prop Key = "Add *"> propagation_required, -Exception </Prop> <prop. key = "insert *"> propagation_required, -Exception </ prop> <prop key = "modifier *"> propagation_required, -Exception </ prop> <prop key = "modifier *"> propagation_required, -exception </ prop> <prop. key = "del *"> propagation_required, -Exception </ prop> <prop key = "get *"> propagation_never </ prop> </props> </ propriété> </ bean> </bans>
printemps-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/xmlma xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/springframeworks-3.0.xschemma/contex http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0. Fichiers, qui peuvent être multiples -> <importation ressource = "classpath *: config / spring / spring-user.xml" /> </bans>
Spring-user.xml
<? xml version = "1.0" Encoding = "UTF-8"?> <! Doctype Beans publics "- // printemps // dtd bean 2.0 // en" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [<! Entity Contextincde System System "org / springframework / web / context / web-inf / contextinclude.xml">]> <eans> <! - Spring bean -> <bean id = "userdao"> <propriété name = "sessionfactory" ref = "sessionfactory"> </ propriété> </ bean> <bean id = "usermanagerbase"> <propriété name = "userdao" ref = "userda"> </prewetwen name = "userdao" Ref = "userda"> </prewetwep name = "userdao" Ref = "userda"> </prewetwepn <! - Le parent est TransactionBese, indiquant que les transactions sont prises en charge -> <bean id = "usermanager" parent = "transactionBese"> <propriété name = "cible" ref = "usermanagerbase"> </ propriété> </ank> </bans>
hibernate.cfg.xml
<! Doctype Hibernate-Configuration Public "- // Hibernate / Hibernate Configuration DTD 3.0 // en" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <sessionfactory> <! <mapping /> </ session-factory> </ hibernate-configuration>
Jetons un coup d'œil à Controller
package com.jialin.Controller; Importer java.io.ioException; import java.io.printwriter; Importer java.util.list; import javax.annotation.resource; Importer javax.servlet.http.httpservletRequest; import javax.servlet.http.httpservletResponse; import org.springframework.sterreotype.Controller; import org.springframework.web.bind.annotation.requestmapping; import com.jianin.entity.user; Import com.jianin.service.iusemanager; @Controller // Struts-Like Action @RequestMapping ("/ user") public class userController {@Resource (name = "usermanager") // Obtenez l'ID du Bean dans le fichier de configuration de printemps en tant que Usermanager, et inject iuserManager Private Usermanager; @RequestMapping ("/ addUser") // Demande le mappage d'adresses URL, similaire à la chaîne de mappage d'action de Struts AddUser (utilisateur utilisateur) {if (usermanager.adduser (user)) {// redirection return "redirect: user / getAllUser"; } else {return "/ fail"; }} @RequestMapping ("/ UpdateUser") public String UpdateUser (utilisateur utilisateur, httpservletRequest request) {if (userManager.upDateUser (user)) {user = usermanager.getOneuser (user); request.setAttribute ("utilisateur", utilisateur); return "/ userredit"; } else {return "/ fail"; }} @RequestMapping ("/ DeLUser") public void DeLuser (utilisateur utilisateur, réponse httpservletResponse) {string result = "{/" result / ": /" error / "}"; if (userManager.Deluser (user)) {result = "{/" result / ": /" Success / "}"; } Printwriter out = null; réponse.setContentType ("application / json"); try {out = réponse.getWriter (); out.write (résultat); } catch (ioException e) {e.printStackTrace (); }} @RequestMapping ("/ ToadDuser") String public ToadDuser () {return "/ userAdd"; } @RequestMapping ("/ ToupDateUser") String public ToupDateUser (utilisateur utilisateur, requête httpservletRequest) {user user1 = usermanager.getOneuser (utilisateur); request.setAttribute ("user1", user1); return "/ userredit"; } @RequestMapping ("/ GetAlLuser") String public getAllUser (HttpServLetRequest Request) {list userList = usermanager.getAllUser (); request.setAttribute ("UserList", UserList); retourner "/ usermain"; }}Téléchargement du code source ...
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.