องค์ประกอบเว็บที่ใช้กันทั่วไปรวมถึง: คำขอเซสชันแอปพลิเคชัน ฯลฯ และโดยทั่วไปเราจะใช้เซสชันเพิ่มเติม วิธีการเข้าถึงองค์ประกอบเว็บใน struts2 สิ่งนี้สำคัญมากเพราะสามารถทำให้การโต้ตอบข้อมูลระหว่างพื้นหลังโปรแกรมและผู้ใช้เสร็จสมบูรณ์ ต่อไปนี้เป็นตัวอย่างการลงทะเบียนเพื่อแสดงกระบวนการ:
1. index.jsp ไฟล์
<%@ page language = "java" contentType = "text/html; charset = utf-8" pageencoding = "utf-8"%> <%String Path = request.getContextPath (); String basepath = request.getScheme ()+": //"+request.getServerName ()+":"+request.getServerport ()+path+"/"; %> <! doctype html สาธารณะ "-// w3c // dtd html 4.01 transitional // en" "http://www.w3.org/tr/html4/loose.dtd"> <html> charset = utf-8 "> <base href =" < %= basepath %> "/> <title> แทรกชื่อที่นี่ </title> </head> <body> <h1> ตัวอย่าง </h1> <form action =" user02! register "method =" post " name = "user.password"> </input> <br/> <อินพุต type = "subment" value = "register"/> </form> </body> </html>
ฟังก์ชั่นนั้นง่ายมาก - นั่นคือผู้ใช้จะป้อนชื่อผู้ใช้และรหัสผ่านจากนั้นสามารถรับได้ในพื้นหลังจากนั้นจะแสดงต่อผู้ใช้หลังจากการลงทะเบียนสำเร็จ
2. struts.xml การกำหนดค่า
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <! doctype struts สาธารณะ "-// Apache ซอฟต์แวร์ Foundation // dtd struts การกำหนดค่า 2.0 // en" "http://struts.apache.org/dtds/struts-2.0.dtd" name = "front" namespace = "/user" ขยาย = "struts-default"> <action name = "user*"> <result> /success.jsp </result> <ชื่อผลลัพธ์ = "ข้อผิดพลาด"
มีสองวิธีในการทำฟังก์ชั่นนี้ให้สมบูรณ์
3. ประเภทแรก (useraction01)
แพ็คเกจ com.myservice.web; นำเข้า java.util.map; นำเข้า com.opensymphony.xwork2.actionContext; นำเข้า com.opensymphony.xwork2.actionsupport; คลาสสาธารณะ UserAction01 ขยาย ActionsUpPort { / ** * * / ส่วนตัวคงที่ Long SerialVersionUid = 1L; ผู้ใช้ส่วนตัว คำขอแผนที่ส่วนตัว เซสชั่นแผนที่ส่วนตัว แอปพลิเคชันแผนที่ส่วนตัว Public UserAction01 () {request = (MAP) ActionContext.getContext (). รับ ("คำขอ"); session = actionContext.getContext (). getSession (); application = actionContext.getContext (). getApplication (); } public String register () {request.put ("ชื่อ", user.getName ()); request.put ("รหัสผ่าน", user.getPassword ()); กลับมาประสบความสำเร็จ } ผู้ใช้สาธารณะ getUser () {return user; } โมฆะสาธารณะ setUser (ผู้ใช้ผู้ใช้) {this.user = ผู้ใช้; -วิธีนี้คือการใช้เมธอด ActionContext.getContext () เพื่อรับบริบทจากนั้นรับคำขอเซสชันและแอปพลิเคชัน
4. วิธีการอื่น (useraction02) เป็นเรื่องธรรมดาและมีชื่อเสียงมาก - IOC (การผกผันควบคุม) และ DI (การฉีดพึ่งพา) มันต้องใช้การใช้งาน 3 อินเทอร์เฟซดังนี้:
แพ็คเกจ com.myservice.web; นำเข้า java.util.map; นำเข้า org.apache.struts2.interceptor.applicationaware; นำเข้า org.apache.struts2.interceptor.requestaware; นำเข้า org.apache.struts2.interceptor.sessionaware; นำเข้า com.opensymphony.xwork2.actionsupport; Public Class UserAction02 ขยายการดำเนินการดำเนินการดำเนินการตามคำขอของ SessionAware, ApplicationAware {แผนที่ส่วนตัว <String, Object> คำขอ; แผนที่ส่วนตัว <สตริงวัตถุ> เซสชัน; แผนที่ส่วนตัว <สตริงวัตถุ> แอปพลิเคชัน; ผู้ใช้ส่วนตัว ผู้ใช้สาธารณะ getUser () {return user; } โมฆะสาธารณะ setUser (ผู้ใช้ผู้ใช้) {this.user = ผู้ใช้; } public String register () {request.put ("ชื่อ", user.getName ()); request.put ("รหัสผ่าน", user.getPassword ()); กลับมาประสบความสำเร็จ } @Override โมฆะสาธารณะ setapplication (แผนที่ <สตริง, วัตถุ> แอปพลิเคชัน) {// วิธีการที่สร้างขึ้นอัตโนมัติ todo stub this.application = แอปพลิเคชัน; } @Override โมฆะสาธารณะ setSession (แผนที่ <String, Object> เซสชัน) {// วิธีการที่สร้างขึ้นอัตโนมัติ todo stub this.session = เซสชัน; } @Override โมฆะสาธารณะ setRequest (แผนที่ <String, Object> คำขอ) {// วิธีการที่สร้างขึ้นอัตโนมัติ todo stub this.request = คำขอ; -สิ่งนี้ใช้ฟังก์ชั่น - ใส่ชื่อและรหัสผ่านของผู้ใช้ลงในคำขอและเมื่อใช้งานเราเพียงแค่ต้องนำออกมา
5. Success.jsp นำเนื้อหาของคำขอออกมาและแสดง
<%@ page language = "java" contentType = "ข้อความ/html; charset = utf-8" pageencoding = "utf-8"%> <%@ taglib uri = "/struts-tags" prefix = "s"%> <! "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"> <title> value = "#request.name"/> ลงทะเบียนเรียบร้อยแล้วด้วยรหัสผ่าน: <s: ค่าคุณสมบัติ = "#request.password"/> </body> </html>
ผลลัพธ์จะแสดงเป็น:
ข้างต้นเป็นเนื้อหาทั้งหมดของการเข้าถึงองค์ประกอบเว็บใน struts2 ฉันหวังว่าคุณจะให้ข้อมูลอ้างอิงและฉันหวังว่าคุณจะสนับสนุน wulin.com มากขึ้น