1. การเตรียม
ก่อนที่คุณจะเริ่มโปรดดูบทความก่อนหน้า:
struts2.3.24 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 การสร้างสภาพแวดล้อมการพัฒนาและคำอธิบายที่เกี่ยวข้องเหมือนกัน แต่ struts2 ถูกแทนที่ด้วยสปริง MVC
2. สถานที่ต่าง ๆ
Project Directory and Jar แพ็คเกจ:
เปลี่ยนแพ็คเกจการกระทำเป็นคอนโทรลเลอร์
ลบแพ็คเกจ struts2 jar และเพิ่มแพ็คเกจสปริง MVC (ถ้ามีอยู่แล้วคุณไม่จำเป็นต้องเพิ่ม);
การกำหนดค่า web.xml:
ความแตกต่างจากก่อนหน้านี้คือตัวกรอง struts2 ถูกแทนที่ด้วย servlet วัตถุประสงค์หลักคือการกำหนดเส้นทาง URL และส่งไปยังสปริง MVC สำหรับการประมวลผล
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <web-app xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://java.sun.com/xml XSI: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3 <pramplay-Name> SSH </display-Name> <dexterce-Param> <param-Name> contextConfigLocation </param-name> <param-value> classpath: applicationcontext.xml </param-value> </context-param> <sarenter> <welcome-file-list> <welcome-file> index.jsp </welcome-file> </welcome-file-list> <servlet> <servlet-name> springmvc </servlet-name> <param-value> classpath: springmvc-servlet.xml </param-value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping>
การกำหนดค่า ApplicationContext.xml:
ความแตกต่างส่วนใหญ่เมื่อกำหนดค่าการสแกนอัตโนมัติส่วนประกอบ @Controller จะต้องไม่รวม ถั่วเหล่านี้สร้างขึ้นโดย Spring MVC;
การกำหนดค่าอื่น ๆ นั้นเหมือนกับในบทความก่อนหน้า
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: p = "http://ww.springframework xmlns: context = "http://www.springframework.org/schema/context" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: xsi = "http:/ http:/ 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/schema/beans http://www.springframework.org/schema/aop http:/ http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://ww.springframework.org/schema http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc/spring-jdbc/spring-jdbc http://www.springframework.org/schema/context/spring-context-4.1.xsd "> <!-สแกน classpath สำหรับส่วนประกอบที่ใส่คำอธิบายประกอบ (รวมถึง @repostory และ @service ซึ่งสแกนโดยไฟล์การกำหนดค่า SpringMvc-> <บริบท: ยกเว้นตัวกรองประเภท = "คำอธิบายประกอบ" นิพจน์ = "org.springframework.steretype.controller" /> < /บริบท: องค์ประกอบ-scan> <!-การเลือกแหล่งข้อมูล-> value = "com.mysql.jdbc.driver" /> <ชื่อคุณสมบัติ = "jdbcurl" value = "jdbc: mysql: // localhost: 3306 /demo" /> <property name = "user" value = "root" /> NAME = "InitialPoolSize" value = "80"> </คุณสมบัติ> <property name = "maxidLetTime" value = "60"> </คุณสมบัติ> <property name = "maxPoolSize" value = "80"> </คุณสมบัติ> <property name = "minPoolSize" value = "50"> name = "acquireeretretempts" value = "60"> </คุณสมบัติ> <property name = "breakafteracquirefailure" value = "false"> </property> <!-หากการเชื่อมต่อมากเกินไปให้ให้ความสนใจกับการปรับเปลี่ยนไฟล์การกำหนดค่า mysql name = "dataSource" ref = "dataSource"/> <property name = "hibernateProperties"> <props> <prop key = "hibernate.dialect"> org.hibernate.dialect.mysqldialect </prop> <prop key = "hibernate.show_sql" key = "hibernate.hbm2ddl.auto"> อัปเดต </prop> <prop key = "hibernate.hbm2ddl.auto"> อัปเดต </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.ehcacheregionformatory </prop> key = "hibernate.cache.provider_configuration_file_resource_path"> ehcache.xml </prop> </props> </คุณสมบัติ> <property name = "MappingLocations"> <list> <value> classpath: ssh/model/user.hbm.xml <value> ssh.model.user </value> </list> </property>-> </ebean> <!-กำหนดค่าตัวจัดการธุรกรรม-> <bean id = "transactionManager"> <property name = "sessionFactory" ref = "sessionfactory"/> </ebean> <! <tx: แอตทริบิวต์> <tx: method name = "เพิ่ม*" การแพร่กระจาย = "จำเป็น" อ่านอย่างเดียว = "false" rollback-for = "java.lang.exception"/> <tx: method name = "delete*" การแพร่กระจาย = "จำเป็น" Read-only = "False" Rollback-for = "java.lang.exception"/> <tx: method name = "update*" การแพร่กระจาย = "จำเป็น" read-only = "false" rollback-for = "java.lang.exception"/> <tx: method name = "save* </tx: แอตทริบิวต์> </tx: คำแนะนำ> <aop: config> <aop: pointcut id = "pcmethod" expression = "การดำเนินการ (*ssh.service ..*.*(.. ))"/> <aop: ที่ปรึกษา pointcut-ref = "pcmethod" id = "aoptest"> </ebean> <bean id = "myaop"> </ebean> <aop: config proxy-target-class = "true"> <aop: ref = "myaop"> <aop: pointcut id = "pcmethodtest" การแสดงออก pointcut-ref = "pcmethodtest" method = "ก่อน"/> <aop: หลังจาก pointcut-ref = "pcmethodtest" method = "หลังจาก"/> </aop: แง่มุม> </aop: config> </ beans>
การกำหนดค่า SpringMvc-servlet.xml:
กำหนดค่าการสแกนอัตโนมัติของ @Controller ภายใต้แพ็คเกจ SSH.Controller ที่นี่คุณต้องกู้คืนตัวกรองที่กำหนดค่าไว้ใน ApplicationContext.xml;
กำหนดค่ามุมมอง Parser มีตัวแยกวิเคราะห์มากมายที่นี่เราใช้ InternalResourceViewResolver เป็นตัวอย่าง
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: context = "http://www.springframework 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 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx <!-เริ่มการสแกนอัตโนมัติของถั่วทั้งหมดภายใต้แพ็คเกจนี้ (ตัวอย่างเช่น @Controller)-> <บริบท: คอมโพเนนต์-สแกนฐานแพคเกจ = "ssh.controller"> <!-คืนค่าตัวกรองที่ตั้งค่าโดยคอนเทนเนอร์แม่ expression = "org.springframework.steretype.controller"/> </บริบท: Component-scan> <!-กำหนดมุมมอง parser-> <bean> <property name = "คำนำหน้า"> <value>/web-inf/jsp/</value> </property>
เขียนคอนโทรลเลอร์:
เนื่องจากสปริง MVC ใช้เพื่อแทนที่ Struts2 จึงไม่มีแพ็คเกจการกระทำ ลบและสร้างแพ็คเกจคอนโทรลเลอร์ใหม่ สร้างคลาส UserController ใหม่ภายใต้แพ็คเกจ
@RequestMapping: URL แผนที่;
@ResponseBody: เนื้อหาจะถูกส่งกลับโดยตรงเป็นร่างกาย
usercontroller.java
แพ็คเกจ ssh.controller; นำเข้า 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; นำเข้า org.springframework.web.bind.annotation.responsebody; นำเข้า ssh.aop.aoptest; com.google.gson.gson;@controller@requestmapping ("/ผู้ใช้") คลาสสาธารณะ userController {logger logger = logger.getLogger (userController.class); @Resource ผู้ใช้ส่วนตัวผู้ใช้บริการ; @Resource ส่วนตัว AOPTEST AOPTEST; @RequestMapping (value = "/addUser") @ResponseBody โมฆะสาธารณะ addUser (คำขอ httpservletRequest, การตอบสนอง httpservletResponse) {printwriter out = null; ลอง {response.setContentType ("text/html; charset = utf-8"); บัญชีสตริง = request.getParameter ("บัญชี"); ชื่อสตริง = request.getParameter ("ชื่อ"); String address = request.getParameter ("ที่อยู่"); ผู้ใช้ผู้ใช้ = ผู้ใช้ใหม่ (); user.setAccount (บัญชี); user.setAddress (ที่อยู่); user.setName (ชื่อ); userservice.add (ผู้ใช้); out = response.getWriter (); out.write (new gson (). tojson ("ความสำเร็จ")); } catch (exception e) {e.printstacktrace (); logger.error (e.getMessage ()); ถ้า (ออก! = null) out.write (new gson (). tojson ("ล้มเหลว")); } ในที่สุด {out.flush (); out.close (); }} @RequestMapping (value = "/queryuser") @ResponseBody โมฆะสาธารณะ QueryAlluser (คำขอ httpservletRequest, การตอบสนอง httpservletResponse) {printwriter out = null; aoptest.test1 (); aoptest.test2 (); ลอง {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 ()); ถ้า (ออก! = null) out.write (new gson (). tojson ("ล้มเหลว")); } ในที่สุด {out.flush (); out.close (); - 3. เรียกใช้โปรแกรม
เรียกใช้โปรแกรมเพิ่มผู้ใช้และผู้ใช้แบบสอบถาม ฟังก์ชั่นเป็นเรื่องปกติ
นอกจากนี้แคชทุติยภูมิยังทำงานได้ตามปกติและฐานข้อมูลจะไม่ทำงานในแบบสอบถามที่สองอีกต่อไป
@author ผู้เขียนแบบมีลมเหมือนลม
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น