原文位址:點選前往
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> 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>pom.xml
7.2 設定檔


1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans>18 19 <!-- 配置组件扫描 -->20 <component-scan></component-scan>21 22 </beans>spring_context.xml


1 <?xml version="1.0" encoding="UTF-8"?> 2 3 nbsp;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>11 <action>12 <result>13 /WEB-INF/jsp/msg.jsp14 </result>15 </action>16 </package>17 18 <package>19 <action>20 <result>21 /WEB-INF/jsp/valueStack.jsp22 </result>23 </action>24 </package>25 26 </struts>27 28#struts.xml ##
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app> 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>#struts.xml

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 }#struts.xml

1 3 4 <!-- 引入struts2标签库 --> 5 6 7 nbsp;html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 8 9 10 <meta>11 <title>Insert title here</title>12 13 14 <h2 id="跟valueStack有关的页面">跟valueStack有关的页面</h2>15 <hr><hr>16 17 <h2 id="利用EL表达式从valuesStack中获取数据">利用EL表达式从valuesStack中获取数据</h2>18 <h3 id="message">${message }</h3>19 <hr>20 <h3 id="loginName">${loginName }</h3>21 <hr>22 <h3 id="password">${password }</h3>23 <hr><hr>24 25 <h2 id="利用OGNL表达式获取valueStack中的数据">利用OGNL表达式获取valueStack中的数据</h2>26 <h3><property></property></h3>27 <hr>28 <h3><property></property></h3>29 <hr>30 <h3><property></property></h3>31 32 <hr><hr>33 34 <debug></debug>35 36



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


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

類加載器通過統一的類文件格式、動態加載、雙親委派模型和平台無關的字節碼,確保Java程序在不同平台上的一致性和兼容性,實現平台獨立性。

Java編譯器生成的代碼是平台無關的,但最終執行的代碼是平台特定的。 1.Java源代碼編譯成平台無關的字節碼。 2.JVM將字節碼轉換為特定平台的機器碼,確保跨平台運行但性能可能不同。

多線程在現代編程中重要,因為它能提高程序的響應性和資源利用率,並處理複雜的並發任務。 JVM通過線程映射、調度機制和同步鎖機制,在不同操作系統上確保多線程的一致性和高效性。

Java的平台獨立性是指編寫的代碼可以在任何安裝了JVM的平台上運行,無需修改。 1)Java源代碼編譯成字節碼,2)字節碼由JVM解釋執行,3)JVM提供內存管理和垃圾回收功能,確保程序在不同操作系統上運行。

Javaapplicationscanindeedencounterplatform-specificissuesdespitetheJVM'sabstraction.Reasonsinclude:1)Nativecodeandlibraries,2)Operatingsystemdifferences,3)JVMimplementationvariations,and4)Hardwaredependencies.Tomitigatethese,developersshould:1)Conduc

云计算显著提升了Java的平台独立性。1)Java代码编译为字节码,由JVM在不同操作系统上执行,确保跨平台运行。2)使用Docker和Kubernetes部署Java应用,提高可移植性和可扩展性。

Java'splatformindependenceallowsdeveloperstowritecodeonceandrunitonanydeviceorOSwithaJVM.Thisisachievedthroughcompilingtobytecode,whichtheJVMinterpretsorcompilesatruntime.ThisfeaturehassignificantlyboostedJava'sadoptionduetocross-platformdeployment,s

容器化技術如Docker增強而非替代Java的平台獨立性。 1)確保跨環境的一致性,2)管理依賴性,包括特定JVM版本,3)簡化部署過程,使Java應用更具適應性和易管理性。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

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

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

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