ฉันเพิ่งเรียนรู้ Easyi และพบว่ามันมีประโยชน์มากและอินเทอร์เฟซก็สวยงาม เขียนประสบการณ์การเรียนรู้ของคุณที่นี่ บล็อกนี้เขียน SSH และ EasyI เพื่อใช้การแสดงการปนเปื้อนของ DataGrid ฟังก์ชั่นอื่น ๆ เช่นการเพิ่มการแก้ไขการลบการลบแบทช์ ฯลฯ จะถูกเขียนทีละหนึ่งในภายหลัง
ก่อนอื่นมาดูเอฟเฟกต์เพื่อให้ได้: เมื่อมีการแสดงข้อมูล 5 บรรทัดต่อหน้า:
เมื่อมีการแสดงข้อมูล 10 บรรทัดต่อหน้าเอฟเฟกต์มีดังนี้:
ขั้นตอนเฉพาะ:
1. ดาวน์โหลด Easyi และสร้างสภาพแวดล้อม
2. สร้างโครงการ SSH และโครงสร้างไดเรกทอรีของโครงการทั้งหมดแสดงในรูป:
3. สร้างนักเรียนตารางในฐานข้อมูล Oracle และป้อนข้อมูล 6 แถวต่อไปนี้ เนื่องจากการดำเนินการเพิ่มเติมยังไม่ได้ดำเนินการให้เพิ่มข้อมูลลงในตารางฐานข้อมูลก่อน ค่าเริ่มต้นคือ 5 ข้อมูลต่อบรรทัดดังนั้นโปรดป้อนข้อมูลอย่างน้อย 6 บรรทัดเพื่ออำนวยความสะดวกในการทดสอบการเพจ
4. การกำหนดค่า web.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://ww.w3.org/200 XSI: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <! <Tilter-class> org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter </ตัวกรอง-class> </ตัวกรอง> <ฟิลเตอร์-แมป> <filter-name> struts2 </filter-ment> <Sistener-class> org.springframework.web.context.contextloaderlistener </listener-class> </listener> <!-การวางตำแหน่งทางกายภาพของ ApplicationContext.xml-> <context-param> <param-name> </context-param> </eb-app>
5. การกำหนดค่าของ ApplicationContext.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http:/ XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://ww.springframework http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://ww.springframework.org/schema http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://ww.springframework.org/schema/schema Resource = "ApplicationContext_bean.xml"/> <import Resource = "ApplicationContext_db.xml"/> </ebeans>
6. สร้างนักเรียนชั้นเรียนโมเดล. java ใน com.model
แพ็คเกจ com.model; นักเรียนชั้นเรียนสาธารณะ {String studentId; // ชื่อสตริงหลักหลัก; // ชื่อสตริงเพศ; // อายุสตริงอายุ; // อายุสตริงสาธารณะ getStudentId () {return studentId; } โมฆะสาธารณะ setStudentId (String studentId) {this.studentId = studentId; } สตริงสาธารณะ getName () {ชื่อคืน; } โมฆะสาธารณะ setName (ชื่อสตริง) {this.name = name; } สตริงสาธารณะ getGender () {กลับเพศ; } โมฆะสาธารณะ setGender (เพศสตริง) {this.gender = เพศ; } public String getage () {return age; } การตั้งค่าโมฆะสาธารณะ (อายุสตริง) {this.age = อายุ; - 7. สร้างนักเรียนการแมปไฟล์ที่สอดคล้องกัน hbm.xml ตาม student.java
<? xml เวอร์ชัน = "1.0"?> <! Doctype hibernate-mapping สาธารณะ "-// hibernate/hibernate mapping dtd 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" 3.4.0.cr1-> <hibernate-mapping> <class name = "com.model.student" table = "student"> <id name = "studentId" type = "java.lang.string"> <column name = "studentId" /> <generator /> < /id> name = "gender" type = "java.lang.string"> <column name = "gender"/> </property> <property name = "age" type = "java.lang.string"> <column name = "age"/> </porement>
8. เขียนอินเทอร์เฟซ Studentservice.java
แพ็คเกจ com.service; นำเข้า java.util.list; Interface Interface Studentservice {รายการสาธารณะ getStudentList (หน้าสตริงแถวสตริง) โยนข้อยกเว้น; // ดึงข้อมูลตามหน้าและรับข้อมูลจากแต่ละหน้า 9. เขียนอินเทอร์เฟซการใช้งานชั้นเรียน Studentserviceimpl.java
แพ็คเกจ com.serviceimpl; นำเข้า java.util.list; นำเข้า org.hibernate.sessionfactory; นำเข้า com.service.studentservice; นักศึกษาระดับสาธารณะ // ดึงข้อมูลตามหน้าและรับข้อมูลสำหรับหลายแถวต่อหน้ารายการสาธารณะ getStudentList (หน้าสตริงแถวสตริง) {// กำหนดค่าเมื่อเป็นค่าเริ่มต้น int currentPage = จำนวนเต็ม parseInt ((หน้า == null || page == "0")? "1": หน้า); "0")? รายการคืน; } // สถิติเท่าใดข้อมูลมีอยู่เท่าไหร่ใน INT ทั้งหมด int getStudentTotal () โยนข้อยกเว้น {ส่งคืนสิ่งนี้ sessionFactory.getCurrentsession () ค้นหา ("จากนักเรียน"). size (); } public SessionFactory getSessionFactory () {return sessionFactory; } โมฆะสาธารณะ setSessionFactory (SessionFactory SessionFactory) {this.sessionFactory = SessionFactory; - 10. กำหนดค่าไฟล์กำหนดค่าสำหรับการเชื่อมต่อกับฐานข้อมูล ApplicationContext_db.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http:/ XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://ww.springframework http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://ww.springframework.org/schema http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://ww.springframework แหล่งที่มากับถั่ว-> <bean id = "dataSource" destroy-method = "close"> <!-การกำหนดไดรเวอร์ฐานข้อมูล-> <property name = "driverclass"> <value> oracle.jdbc.driver.oracledriver </value> </property> <! <value> JDBC: Oracle: Thin: @LocalHost: 1521: ORCL </value> </คุณสมบัติ> <!-กำหนดชื่อผู้ใช้ของฐานข้อมูล-> <ชื่อคุณสมบัติ = "ผู้ใช้"> <value> LHQ </alue> </คุณสมบัติ> <!-กำหนดรหัสผ่านของฐานข้อมูล name = "minpoolsize"> <value> 1 </value> </คุณสมบัติ> <property name = "MaxPoolSize"> <dance> 40 </alues> </คุณสมบัติ> <property name = "MaxidLetime"> <value> 1800 </alue> </perty> <property name = "InitialPoolSize"> <value> 2 </value> </คุณสมบัติ> <property name = "IdleConnectionTestPeriod"> <value> 1800 </alues> </คุณสมบัติ> <property name = "AcquireretryAttempts"> <alues> 30 </value> name = "testConnectionOnCheckout"> <dange> false </value> </property> </ebean> <!-กำหนด HiberNate SessionFactory-> <bean id = "SessionFactory"> <! <property name = "hibernateProperties"> <props> <prop key = "hibernate.dialect"> org.hibernate.dialect.oracle10gdialect </prop> </props> </porement> <! </list> </property> </ebean> <!-กำหนดค่า Transaction Interceptor-> <bean id = "transactionManager"> <property name = "SessionFactory" ref = "SessionFactory"/> </ebean> <tx: iderning id = "txAdvice" transaction-manager = "TransactionManager" วิธีการเริ่มต้นด้วยการบันทึกลบและการอัปเดตสามารถทำได้-> <tx: method name = "ลบ*" การแพร่กระจาย = "จำเป็น" /> <tx: method name = "update*" การแพร่กระจาย = "จำเป็น" /> <tx: method = "*" propagation = " <aop: config> <aop: pointcut id = "interceptorpointcuts" expression = "การดำเนินการ (*com.serviceimpl ..*.*.*(.. ))" /> <!
11. เขียน studentAction.java ประเภทในเลเยอร์ควบคุม
แพ็คเกจ com.action; นำเข้า java.util.list; นำเข้า javax.servlet.http.httpservletrequest; นำเข้า Javax.servlet.http.httpservletResponse; นำเข้า net.sf.json.jsonobject; Import org.log4j.logger; com.service.studentservice; การศึกษาระดับสาธารณะ {logger logger แบบคงที่ = logger.getLogger (studentAction.class); JsonObject ส่วนตัว jsonobj; แถวสตริงส่วนตัว; // จำนวนของบันทึกที่แสดงในแต่ละหน้าสตริงส่วนตัวหน้า; // หน้าอะไรคือหน้านักเรียนส่วนตัวปัจจุบันบริการ student_services; // การฉีดพึ่งพาสตริง // การสืบค้นข้อมูลนักเรียนทั้งหมดสตริงสาธารณะ getAllstudent () โยนข้อยกเว้น {log.info ("ข้อมูลนักเรียนทั้งหมด"); รายการรายการ = student_services.getStudentList (หน้าแถว); this.tobejson (รายการ, student_services.getStudentTotal ()); คืนค่า null; } // แปลงเป็นรูปแบบ JSON เป็นโมฆะสาธารณะ Tobejson (รายการรายการทั้งหมด) พ่นข้อยกเว้น {httpservletResponse Response = servletactionContext.getResponse (); httpservletRequest request = servletactionContext.getRequest (); jsonObject jobj = new JsonObject (); // ใหม่ JSON JOBJ.CACUMULATE ("รวม", ทั้งหมด); // ทั้งหมดแสดงถึงจำนวนข้อมูลที่มีอยู่ใน JOBJ.CACCUMULATE ("แถว", รายการ); // แถวคือข้อมูลที่แสดงถึงการตอบสนองของหน้าเว็บที่แสดง Response.getWriter (). เขียน (jobj.toString ()); // แปลงเป็น json format log.info (jobj.toString ()); } Public Studentservice getStudent_Services () {return student_services; } โมฆะสาธารณะ setstudent_services (Studentservice student_services) {this.student_services = student_services; } โมฆะสาธารณะ setjsonobj (jsonobject jsonobj) {this.jsonobj = jsonobj; } โมฆะสาธารณะ setrows (แถวสตริง) {this.rows = rows; } public void setPage (หน้าสตริง) {this.page = page; - 12. การเขียนการพึ่งพาการพึ่งพาของ Spring ApplicationContext_bean.xml ไฟล์การกำหนดค่า
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http:/ XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://ww.springframework http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://ww.springframework.org/schema http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://ww.springframework.org/schema -> <bean id = "student_service"> <property name = "sessionfactory"> <ref bean = "sessionfactory"> </ref> </property> </epean> <!
13. เขียนไฟล์การกำหนดค่า struts.xml
<? XML เวอร์ชัน = "1.0" การเข้ารหัส = "UTF-8"?> <! Doctype struts สาธารณะ "-// Apache Software Foundation // dtd struts การกำหนดค่า 2.0 // en" "http://struts.apache.org/dtds/struts-2.0.dtd" ข้อมูลนักเรียน-> <action name = "getAllStudentAction" method = "getAllStudent"> <result type = "JSON
14. เขียน jsp ---- index.jsp
<%@ page language = "java" pageencoding = "utf-8" iselignored = "false"%> <%path String = request.getContextPath ();%> <%@ taglib คำนำหน้า = "s" uri = "/struts-tags"%> <! "http://www.w3.org/tr/html4/loose.dtd"><html> <head> <meta http-equiv =" content-ype "content =" text/html; charset = utf-8 "> <title> src = "<%= path%>/js/easyui/jQuery-1.8.0.min.js" charset = "utf-8"> </script> <!-แนะนำ jQuery_easyui-> <script type = "JavaScript" src = " charset = "utf-8"> </script> <!-แนะนำ jQuery_easyui-> <script type = "ข้อความ/javascript" src = "<%= path%>/js/useui/jQuery.easyui.min.js" charset = "utf-8" src = "<%= path%>/js/easyui/locale/easyui-lang-zh_cn.js" charset = "utf-8"> </script> <! href = "<%= path%>/js/easyui/ธีม/default/easyui.css"/> <!-แนะนำไอคอน Easyui ขนาดเล็ก-> <link rel = "stylesheet" type = "text/css" href = "<%= path $ ('#myDatagrid'). dataGrid ({title: 'dataGrid อินสแตนซ์', iconcls: 'icon-ok', ความกว้าง: 600, pagesize: 5, // หน้าเริ่มต้นที่เลือกคือ 5 แถวต่อหน้า Pagelist: [5, 10, 15, 20], // พื้นหลังแถว ยุบ: จริง, // แสดงแถบเครื่องมือปุ่มพับได้: "#tb", // สิ่งนี้ควรใช้เมื่อเพิ่มการลบและการปรับเปลี่ยน URL: 'getAllStudentAction.Action', // URL เรียกวิธีการโหลด: // sortName: 'xh', // คอลัมน์ใดที่ใช้ในการเรียงลำดับเมื่อตารางข้อมูลเริ่มต้น // sortOrder: 'desc', // กำหนดลำดับการเรียงลำดับซึ่งอาจเป็น 'asc' หรือ 'desc' (ลำดับบวกหรือย้อนกลับ) remotesort: false, frozencolumns: [[{field: 'ck', ช่องทำเครื่องหมาย: true}]], pagination: true, // pagination rownumbers: true // หมายเลขบรรทัด}); - </script> </head> <body> <h2> <b> อินสแตนซ์ DataGrid ของ easyi </b> </h2> <table id = "myDatagrid"> <thead> <tr> <th data-options = "ฟิลด์: data-options = "ฟิลด์: 'ชื่อ', ความกว้าง: 100, จัดตำแหน่ง: 'center'"> ชื่อ </th> <th data-options = "ฟิลด์: 'เพศ', ความกว้าง: 100, จัดตำแหน่ง: 'center'"> เพศ </th> <th> </th> </th> </body> </html>15. เริ่มโปรแกรมและป้อน http: // localhost: 8080/easyui/index.jsp สำหรับการทดสอบ
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น