Struts2框架簡介及用法介紹
原文位址:點選前往
1 什麼是ValueStack
稱為值棧,Struts提供的共享資料的資料結構
2 為什麼要使用ValueStack
從控制器向瀏覽器傳遞資料
儲存與請求相關的物件資訊(session/application)
3 ValueStack物件的生命週期
# 請求進入到伺服器端後,在記憶體中就會傳送一個ValueStack物件;當請求處理結束以後,ValueStack物件就會被清除
4 如何存取ValueStack中的資料
利用OGNL表達式取得
利用EL表達式取得
5 在ValueStack中儲存資料的區域分割
ConContents (堆疊結構) 利用OGNL或EL來取得資料
Context (Map結構) 利用#key 來取得資料
7 案例:從控制器向瀏覽器傳值,展示valueStack區域
7.1 導包


1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 "> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>cn.xiangxu</groupId> 4 <artifactId>ssh03</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <packaging>war</packaging> 7 <dependencies> 8 <dependency> 9 <groupId>org.apache.struts</groupId>10 <artifactId>struts2-core</artifactId>11 <version>2.3.8</version>12 </dependency>13 <dependency>14 <groupId>org.apache.struts</groupId>15 <artifactId>struts2-spring-plugin</artifactId>16 <version>2.3.8</version>17 </dependency>18 <dependency>19 <groupId>org.apache.struts</groupId>20 <artifactId>struts2-json-plugin</artifactId>21 <version>2.3.8</version>22 </dependency>23 </dependencies>24 </project>
7.2 設定檔


1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" 5 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 7 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd10 http://www.springframework.org/schema/context/spring-context-3.0.xsd11 http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd12 http://www.springframework.org/schema/jee/spring-jee-3.0.xsd13 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd14 http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd15 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd16 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd17 http://www.springframework.org/schema/util/spring-util-3.0.xsd">18 19 <!-- 配置组件扫描 -->20 <context:component-scan base-package="cn.xiangxu" />21 22 </beans>


1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <!DOCTYPE struts PUBLIC 4 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 5 "http://struts.apache.org/dtds/struts-2.3.dtd"> 6 7 <struts> 8 9 <!-- 测试struts整合spring时用 -->10 <package name="test" namespace="/test" extends="json-default">11 <action name="demo">12 <result>13 /WEB-INF/jsp/msg.jsp14 </result>15 </action>16 </package>17 18 <package name="vs" namespace="/vs" extends="json-default">19 <action name="valueStack" class="valueStackAction" method="valueStaceMethod">20 <result name="success">21 /WEB-INF/jsp/valueStack.jsp22 </result>23 </action>24 </package>25 26 </struts>27 28
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee " version="2.5"> 3 <display-name>ssh03</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file>10 <welcome-file>default.jsp</welcome-file>11 </welcome-file-list>12 13 <!-- 配置spring监听14 目的:容器启动时自动加载一些东西到缓存中 -->15 <listener>16 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>17 </listener>18 19 <!-- 配置Spring配置文件的位置 -->20 <context-param>21 <param-name>contextConfigLocation</param-name>22 <param-value>classpath:spring_*.xml</param-value>23 </context-param>24 25 <!-- 配置主控制器和过滤条件 -->26 <filter>27 <filter-name>mvc</filter-name>28 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>29 </filter>30 <filter-mapping>31 <filter-name>mvc</filter-name>32 <url-pattern>/*</url-pattern>33 </filter-mapping>34 35 </web-app>

1 package cn.xiangxu.action; 2 3 import org.springframework.context.annotation.Scope; 4 import org.springframework.stereotype.Controller; 5 6 import com.opensymphony.xwork2.ActionContext; 7 import com.opensymphony.xwork2.util.ValueStack; 8 9 import cn.xiangxu.entity.Person;10 11 @Controller12 @Scope("prototype")13 public class ValueStackAction {14 15 private String message;16 17 public String valueStaceMethod() {18 System.out.println("跟valueStack相关的action类");19 20 message = "我是控制类中的属性message";21 22 // 利用工厂方法来获取session对象时就使用下面两行代码23 ActionContext context = ActionContext.getContext();24 context.getSession().put("loginName", "warrior"); // 向session中插入数据25 26 context.getSession().put("password", "123456"); // 向session中插入数据27 28 // 利用上下文对象来获取ValueStack对象29 ValueStack valueStack = context.getValueStack();30 31 Person person = new Person();32 person.setId("333");33 person.setName("fury");34 person.setMessage("hello fury");35 valueStack.push(person); // 将数据插入到对象栈中36 37 return "success";38 }39 40 public String getMessage() {41 return message;42 }43 44 public void setMessage(String message) {45 this.message = message;46 }47 48 }

1 package cn.xiangxu.entity; 2 3 import java.io.Serializable; 4 5 public class Person implements Serializable { 6 7 private static final long serialVersionUID = -7221161390673280278L; 8 private String id; 9 private String name;10 private String message;11 public Person() {12 super();13 // TODO Auto-generated constructor stub14 }15 public Person(String id, String name, String message) {16 super();17 this.id = id;18 this.name = name;19 this.message = message;20 }21 @Override22 public int hashCode() {23 final int prime = 31;24 int result = 1;25 result = prime * result + ((id == null) ? 0 : id.hashCode());26 return result;27 }28 @Override29 public boolean equals(Object obj) {30 if (this == obj)31 return true;32 if (obj == null)33 return false;34 if (getClass() != obj.getClass())35 return false;36 Person other = (Person) obj;37 if (id == null) {38 if (other.id != null)39 return false;40 } else if (!id.equals(other.id))41 return false;42 return true;43 }44 public String getId() {45 return id;46 }47 public void setId(String id) {48 this.id = id;49 }50 public String getName() {51 return name;52 }53 public void setName(String name) {54 this.name = name;55 }56 public String getMessage() {57 return message;58 }59 public void setMessage(String message) {60 this.message = message;61 }62 @Override63 public String toString() {64 return "Person [id=" + id + ", name=" + name + ", message=" + message + "]";65 }66 67 68 }

1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 4 <!-- 引入struts2标签库 --> 5 <%@ taglib prefix="s" uri="/struts-tags" %> 6 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 8 <html> 9 <head>10 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">11 <title>Insert title here</title>12 </head>13 <body>14 <h2>跟valueStack有关的页面</h2>15 <hr /><hr />16 17 <h2>利用EL表达式从valuesStack中获取数据</h2>18 <h3>${message }</h3>19 <hr />20 <h3>${loginName }</h3>21 <hr />22 <h3>${password }</h3>23 <hr /><hr />24 25 <h2>利用OGNL表达式获取valueStack中的数据</h2>26 <h3><s:property value="message"/></h3>27 <hr />28 <h3><s:property value="#session.loginName"/></h3>29 <hr />30 <h3><s:property value="#session.password"/></h3>31 32 <hr /><hr />33 34 <s:debug></s:debug>35 </body>36 </html>



7.4 编写jsp页面
7.4.1 利用EL表达式访问ValueStack中的数据的格式
${变量名}
7.4.2 利用OGNL表达式访问ValueStack中的数据的格式
注意:为什么访问sesseion中的数据时需要在前面加 #session. 是因为....【自己百度去,或者参见本博客顶端的连接;三少能力有限,讲不清楚】
注意:在读取栈结构中的数据时是从栈顶开始读的,如果有两个变量的名字相同,那么读取到的只会是相对前面的那个变量的值


1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 4 <!-- 引入struts2标签库 --> 5 <%@ taglib prefix="s" uri="/struts-tags" %> 6 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 8 <html> 9 <head>10 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">11 <title>Insert title here</title>12 </head>13 <body>14 <h2>跟valueStack有关的页面</h2>15 <hr /><hr />16 17 <h2>利用EL表达式从valuesStack中获取数据</h2>18 <h3>${message }</h3>19 <hr />20 <h3>${loginName }</h3>21 <hr />22 <h3>${password }</h3>23 <hr /><hr />24 25 <h2>利用OGNL表达式获取valueStack中的数据</h2>26 <h3><s:property value="message"/></h3>27 <hr />28 <h3><s:property value="#session.loginName"/></h3>29 <hr />30 <h3><s:property value="#session.password"/></h3>31 32 <hr /><hr />33 34 <s:debug></s:debug>35 </body>36 </html>
7.5 项目结构图
以上是Struts2框架簡介及用法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

評估Java框架商業支援的性價比涉及以下步驟:確定所需的保障等級和服務等級協定(SLA)保證。研究支持團隊的經驗和專業知識。考慮附加服務,如昇級、故障排除和效能最佳化。權衡商業支援成本與風險緩解和提高效率。

PHP框架的學習曲線取決於語言熟練度、框架複雜性、文件品質和社群支援。與Python框架相比,PHP框架的學習曲線較高,而與Ruby框架相比,則較低。與Java框架相比,PHP框架的學習曲線中等,但入門時間較短。

輕量級PHP框架透過小體積和低資源消耗提升應用程式效能。其特點包括:體積小,啟動快,記憶體佔用低提升響應速度和吞吐量,降低資源消耗實戰案例:SlimFramework創建RESTAPI,僅500KB,高響應性、高吞吐量

根據基準測試,對於小型、高效能應用程序,Quarkus(快速啟動、低記憶體)或Micronaut(TechEmpower優異)是理想選擇。 SpringBoot適用於大型、全端應用程序,但啟動時間和記憶體佔用稍慢。

編寫清晰全面的文件對於Golang框架至關重要。最佳實踐包括:遵循既定文件風格,例如Google的Go程式設計風格指南。使用清晰的組織結構,包括標題、子標題和列表,並提供導覽。提供全面且準確的信息,包括入門指南、API參考和概念。使用程式碼範例說明概念和使用方法。保持文件更新,追蹤變更並記錄新功能。提供支援和社群資源,例如GitHub問題和論壇。建立實際案例,如API文件。

根據應用場景選擇最佳Go框架:考慮應用類型、語言特性、效能需求、生態系統。常見Go框架:Gin(Web應用)、Echo(Web服務)、Fiber(高吞吐量)、gorm(ORM)、fasthttp(速度)。實戰案例:建構RESTAPI(Fiber),與資料庫互動(gorm)。選擇框架:效能關鍵選fasthttp,靈活Web應用選Gin/Echo,資料庫互動選gorm。

在Go框架開發中,常見的挑戰及其解決方案是:錯誤處理:利用errors套件進行管理,並使用中間件集中處理錯誤。身份驗證和授權:整合第三方庫並建立自訂中間件來檢查憑證。並發處理:利用goroutine、互斥鎖和通道來控制資源存取。單元測試:使用gotest包,模擬和存根隔離,並使用程式碼覆蓋率工具確保充分性。部署和監控:使用Docker容器打包部署,設定資料備份,透過日誌記錄和監控工具追蹤效能和錯誤。

Go框架學習的迷思有以下5種:過度依賴框架,限制彈性。不遵循框架約定,程式碼難以維護。使用過時庫,帶來安全和相容性問題。過度使用包,混淆程式碼結構。忽視錯誤處理,導致意外行為和崩潰。
