โครงการแรก ๆ ค่อนข้างง่ายส่วนใหญ่เกี่ยวข้องโดยตรงกับ JSP, Servlet + JDBC ต่อมา struts1 (struts2)+สปริง+ไฮเบอร์เนตถูกใช้เพื่อผลักดันการพัฒนาโครงการอย่างเคร่งครัดตามแนวคิดของการฝังรากลึก เวลานี้สปริง MVC ถูกใช้เพื่อแทนที่เสาเพื่อการพัฒนา
MVC เป็นส่วนสำคัญของการพัฒนาเว็บที่ทันสมัยอยู่แล้ว มาแนะนำการสร้างสภาพแวดล้อมการพัฒนาของ SpringMVC+Spring3+Hibernate4
มาดูโครงสร้างโครงการ:
รหัสเฉพาะจะไม่แสดงให้เห็นอีกต่อไปโดยส่วนใหญ่ใช้เส้นทางธรรมดามากโครงสร้างของ MVC-Servcie-dao-Hibernate สามารถดาวน์โหลดซอร์สโค้ดได้โดยส่วนใหญ่จะดูไฟล์การกำหนดค่า ดูความคิดเห็นสำหรับคำอธิบาย
web.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <web-app xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://java.sun.com/xml xmlns: web = "http://java.sun.com/xml/ns/javaee" xmlns: web = "http://java.sun.com/xml/ns/javaee/web-app_2_5_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> <!-กำหนดค่าสปริง-> <context-param> <param-name> contextconfiglocation </param-name> <param-value> classpath*: config/spring-*. xml </param-value> <Sistener-class> org.springframework.web.context.contextloaderListener </listener-class> </listener> <!-กำหนดค่า SpringMvc-> <servlet> <param-Name> contextConfigLocation </param-name> <param-value> classpath*: config/spring-servlet.xml </param-value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet-mapping> <!-ตั้งค่าตัวอักษร-> <filter> <s Filter-Name> EncodingFilter </filter-name> <filter-class> org.springframework.web.filter.characterencodingfilter </param-name> <init-param> <init-param> </init-param> <init-Param> <param-Name> ForceEncoding </param-name> <param-value> True </param-value> </itt-param> </filter> <sidter-mapping> <filter-name> การเข้ารหัส </filter-name> <Tilter-Name> OpenSession </filter-Name> <Tilter-Class> org.springframework.orm.hibernate4.support.opensessionInviewFilter </filter-class> </filter> <filter-mapping> </eb-app>
Spring-servlet.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: context = "http://www.springframework 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <! -> <!-การประมวลผลคำอธิบายประกอบ-> <!-<ถั่ว/>-> <!-การประมวลผลการทำแผนที่คลาสคำอธิบายประกอบ-> <!-<Bean> </epean>-> <!-เปิดแผนการเพิ่มความคิดเห็น 2-> <MVC: การเพิ่มความคิดเห็น/> <! <MVC: ทรัพยากรตำแหน่ง = "/js/" การแม็พ = "/js/**"/> <!-การเข้าถึงทรัพยากรแบบคงที่, โครงการ 2-> <!-<mvc: การบันทึกค่าเริ่มต้น/>>-> <! คลาสการตีความขึ้นอยู่กับส่วนขยายของคุณ-> <property name = "คำต่อท้าย" value = ". jsp"> </property> </epean> <!-อัปโหลดถั่วไฟล์-> <!-<bean id = "MultipartResolver"> <property name = "defaultEncoding" value = "UTF-8" /> < name = "MaxinMemorySize" value = "40960"/> </ebean> -> </ebeans>
Spring-hibernate.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: context = "http://www.springframework 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <! value = "com.mysql.jdbc.driver" /> <property name = "url" value = "jdbc: mysql: //127.0.0.1/springmvc" /> <property name = "username" value = "root" /> <property name = "รหัสผ่าน" id = "SessionFactory"> <property name = "dataSource" ref = "dataSource"/> <property name = "hibernateProperties"> <props> <prop key = "hibernate.dialect"> org.hibernate.dialect.mysqldialect </prop> key = "hibernate.show_sql"> true </prop> <prop key = "hibernate.format_sql"> true </prop> </prop> </property> <property name = "configlocations"> <list> <dalue> classpath*: config/hibernate/hibernate.cfg.xml <bean id = "transactionManager"> <property name = "sessionFactory" ref = "sessionfactory"> </คุณสมบัติ> </ebean> <!-คลาสตัวแทนธุรกรรม-> <bean id = "transactionBese" lazy-init = "true" abstract = "true"> < key = "เพิ่ม*"> propagation_required, -Exception </prop> <prop key = "update*"> propagation_required, -Exception </prop> <prop key = "แทรก*"> propagation_required, -Exception </prop> <prop key = "แทรก*" key = "modify*"> propagation_required, -Exception </prop> <prop key = "modify*"> propagation_required, -Exception </prop> <prop key = "ลบ*"> propagation_required, -Exception </prop> <prop key = "del*del* </props> </property> </ebean> </ebeans>
Spring-core.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: context = "http://www.springframework 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <!-แนะนำไฟล์การกำหนดค่าอื่น ๆ </epeans>
Spring-user.xml
<? xml เวอร์ชัน = "1.0" การเข้ารหัส = "utf-8"?> <! ถั่ว doctype สาธารณะสาธารณะ "-// 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-> <bean id = "userdao"> <property name = "sessionfactory" ref = "sessionfactory"> </ebean> <!-Parent เป็นธุรกรรม BESES ซึ่งบ่งชี้ว่าการทำธุรกรรมได้รับการสนับสนุน-> <bean id = "userManager" parent = "transactionBese"> <property name = "target" ref = "userManagerBase"> </property> </epean>
hibernate.cfg.xml
<! doctype hibernate-configuration สาธารณะ "-// hibernate/hibernate การกำหนดค่า dtd 3.0 // en" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" <การแม็พ/> </ession-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 action @RequestMapping ("/ผู้ใช้") คลาสสาธารณะ userController {@Resource (name = "userManager") // รับ ID ของถั่วในไฟล์การกำหนดค่าฤดูใบไม้ผลิเป็น userManager @RequestMapping ("/adduser") // คำขอการแมป URL ที่อยู่คล้ายกับ struts-mapping public String adduser (ผู้ใช้ผู้ใช้) {ถ้า (userManager.adduser (ผู้ใช้)) {// เปลี่ยนเส้นทางกลับ "เปลี่ยนเส้นทาง:/ผู้ใช้/getAlluser"; } else {return "/fail"; }} @RequestMapping ("/updateUser") updateUser สตริงสาธารณะ (ผู้ใช้ผู้ใช้, คำขอ httpservletRequest) {ถ้า (userManager.UpDateUser (ผู้ใช้)) {user = userManager.getOneUser (ผู้ใช้); request.setAttribute ("ผู้ใช้" ผู้ใช้); กลับ "/useredit"; } else {return "/fail"; }} @RequestMapping ("/deluser") โมฆะสาธารณะ deluser (ผู้ใช้ผู้ใช้, การตอบสนอง httpservletResponse) {string result = "{/" result/":/" ข้อผิดพลาด/"}"; if (userManager.deluser (ผู้ใช้)) {result = "{/" result/":/" ความสำเร็จ/"}"; } printWriter out = null; Response.SetContentType ("Application/JSON"); ลอง {out = response.getWriter (); out.write (ผลลัพธ์); } catch (ioexception e) {e.printstacktrace (); }} @requestmapping ("/toadduser") สตริงสาธารณะ toadduser () {return "/useradd"; } @RequestMapping ("/toupDateUser") สตริงสาธารณะ toupDateUser (ผู้ใช้ผู้ใช้, คำขอ httpservletRequest) {ผู้ใช้ผู้ใช้ 1 = userManager.getOneUser (ผู้ใช้); request.setAttribute ("user1", user1); กลับ "/useredit"; } @RequestMapping ("/getAlluser") สตริงสาธารณะ getAlluser (คำขอ httpservletRequest) {รายการ userlist = userManager.getAlluser (); request.setAttribute ("userlist", userlist); กลับ "/Usermain"; -ดาวน์โหลดซอร์สโค้ด ...
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น