초기 프로젝트는 비교적 간단했으며 대부분 JSP, Servlet + JDBC를 직접 처리했습니다. 나중에, struts1 (struts2)+스프링+hibernate를 사용하여 레이어링 개념에 따라 프로젝트 개발을 엄격하게 주도했습니다. 이번에는 Spring MVC가 개발을 위해 Struts를 대체하는 데 사용되었습니다.
MVC는 이미 현대 웹 개발에서 매우 중요한 부분입니다. 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/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"> <display-name> springmvc </display-name> <welcome-file> index.jsp </welcome-file> </welcome-file-list> <!-스프링 구성-> <context-param> <param-name> contextConfigLocation </param-name> <param- value> classpath*: config/spring-*. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Configure SpringMVC --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name> contextConfigLocation </param-name> <Param-value> classPath*: config/spring-servlet.xml </param- value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name> springmvc </servlet-name> <Url-pattern>/</url-pattern> </servlet-mapping> <!-캐릭터 세트-> <filter> <filter-name> encodingfilter </filter-name> org.spramework.web.filter.characterencodingfilter </filter class> <param-name> actoding> actaram> <Param-value> UTF-8 </param-value> </init-param> <init-param> <param-name> forceencoding </param-name> <param-value> true </param-value> </init-param> </filter> <filter-mapping> <filter-name> encodingfilter </*</url> <llattern> <llattern> </filter-mapping> <!-제어 세션으로 전환-> <filter> <filter-name> Opensession </filter-name> <filter-class> org.springframework.orm.hibernate4.support.opensessioninviewfilter </filter class> </filter-mapping> <filter-name> opensession </filter-name> <Url-pattern>/*</url-pattern> </필터 매핑> </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.w.w.w.w.2001/xmlschemain xsi : schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/contet http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.spramework.org/schema/mvc/spring-mvc-3.0.xsd -> <context : component-scan base-package = "com.jialin" /> <!-열린 주석 구성표 1-> <!-주석 메소드 프로세싱-> <!-<bean />-> <!-주석 클래스 매핑 처리-> <!-<ean> < /bean>-> <!-alperation scheme 2-> <! -> <mvc : 자원 위치 = "/img/"mapping = "/img/**"/> <mvc : 자원 위치 = "/js/"mapping = "/js/**"/> <!-정적 리소스 액세스, 체계 2-> <!-<mvc : default-servlet-handler/> --> <!-<id id = id viewresol ver "> 이름 = "prefix"value = "/"> </propert> <!-extension-> <property name = "toldix"value = ". jsp"> </property> </bean> <!-<bean id = "multipartresolver"> <value "> value"> value "> <utfaultecodver"> value ""> 이름 = "maxuploadsize"value = "10485760000" /> <속성 이름 = "maxInMemorySize"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.w.w.w.w.2001/xmlschemain xsi : schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/contet http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.spramework.org/schema/spring-mvc-3.0.xsd 소스-> <bean id = "dataSource"> <property name = "driver className"value = "com.mysql.jdbc.driver" /> <속성 이름 = "url"value = "jdbc : mysql : //127.0.0.1/springmvc" /> <속성 이름 "value ="root " /> <"password " value = "123456" /> < /bean> <!-최대 절전 모드 SesssessFactory-> <bean id = "sessionFactory"> <property name = "dataSource"ref = "dataSource" /> <propertyproperties "> <props> <props> <props> <hibernate.dialect"> org.hibernate.dialect.dialect. <prop key = "hibernate.hbm2ddl.auto"> update </prop> <prop key = "hibernate.show_sql"> true </prop> <prop key = "hibernate.format_sql"> true </prop> </property name = "configlocations"> <values> classPath*: config/hibernate/hibernate.cfg.xml </value> </list> </property> </bean> <!-트랜잭션 관리자-> <bean id = "transactionManager"> <property name = "sessionfactory"ref = "session actory"> </property> </bean> <!-Transaction Class-> <Bean id = "transactionbese" "traly" <property name = "transactionManager"ref = "transactionManager"> </property> <속성 이름 = "transactionAttributes"> <props> <prop key = "add*"> propagation_required, -exception </propception </prop> <prop key = "update*"> profagation_required, -exception> <prop key = "Insert*"> propagation_recence <proccence <ceplection <ceplection <ceplection <ceplection <ceplection <ceplection <prop <prop key = "update*" key = "insert*"> propagation_required, -exception </prop> <prop key = "modify*"> propagation_required, -exception </prop> <prop key = "modify*"> propagation_required, -exception </prop> <prop key = "delete*"> propagation_required, -exception </prop> key = "del*"> propagation_required, -exception </prop> <prop key = "get*"> propagation_never </prop> </props> </propert> </bean> </beans>
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.w.w.w.w.2001/xmlschemain xsi : schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/contet http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.spramework.org/schema/sprring-mvc-3.0.xsd 다중 일 수있는 파일-> <import resource = "classpath*: config/spring/spring-user.xml"/> </beans>
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"[<! entity contextincle system "org/springframework/web/context/web-inf/contextinclude.xml">]>]>]> <eans> <!-스프링 빈-> <bean id = "userdao"> <property name = "sessionFactory"ref = "sessionFactory"> </bean> </bean> </bean> </bean </property "> ref ="userDao " <!-Parent는 TransactionBese입니다. 트랜잭션이 지원되었음을 나타냅니다-> <bean id = "userManager"parent = "transactionBese"> <property name = "target"ref = "userManagerBase"> </property> </bean> </beans>
Hibernate.cfg.xml
<! docType hibernate-configuration public "-// hibernate/hibernate configuration dtd 3.0 // en" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernat-configuration> <mapting/> <mapering mapering recondring redluest-mapte-factory> </session-factory> </hibernate-configuration>
컨트롤러를 살펴 보겠습니다
패키지 com.jialin.controller; import java.io.ioexception; import java.io.printwriter; Java.util.list 가져 오기; import javax.annotation.resource; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; org.springframework.stereotyp.controller import; org.springframework.web.bind.annotation.requestmapping; com.jianin.entity.user import; com.jianin.service.iusermanager import; @controller // struts-like action @requestmapping ( "/user") public class usercontroller {@resource (name = "usermanager") // 스프링 구성 파일에서 Bean의 ID를 userManager로 가져 와서 개인 iuserManager UserManager를 인젝트합니다. @requestmapping ( "/adduser") // struts-like 액션 매핑 public string adduser (user user)와 유사한 URL 주소 매핑 요청 {if (usermanager.adduser (user)) {// 리디렉션 "Redirect :/user/getalluser"; } else {return "/fail"; }} @requestmapping ( "/updateUser") public String updateUser (user user, httpservletrequest request) {if (usermanager.updateuser (user)) {user = usermanager.getoneuser (user); request.setAttribute ( "사용자", 사용자); 반환 "/useredit"; } else {return "/fail"; }} @requestmapping ( "/deLuser") public void deLuser (사용자, httpservletResponse 응답) {문자열 result = "{/"result/":/"error/"}"; if (userManager.deluser (user)) {result = "{/"result/":/"success/"}"; } printwriter out = null; Response.SetContentType ( "Application/JSON"); try {out = response.getwriter (); out.write (결과); } catch (ioexception e) {e.printstacktrace (); }} @requestmapping ( "/toadduser") public String toadduser () {return "/userAdd"; } @RequestMapping ( "/toupDateUser") public String toupDateUser (user user, httpservletrequest request) {user user1 = usermanager.getOnuser (user); request.setattribute ( "user1", user1); 반환 "/useredit"; } @RequestMapping ( "/getAllUser") public String getAllUser (httpservletRequest request) {list userList = userManager.getallUser (); request.setAttribute ( "사용자리스트", UserList); 반품 "/usermain"; }}소스 코드 다운로드 ...
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.