詳解Java的Struts框架中棧值和OGNL的使用
值棧:
值棧是一個集合中的幾個對象保持下列對象提供的順序:

值棧可以通過JSP,Velocity或者Freemarker的標簽。有各種不同的標簽在單獨的章節(jié)中,我們將學習,用于獲取和設(shè)置Struts 2.0 的值棧。 ValueStack的對象里面可以得到動作如下:
ActionContext.getContext().getValueStack()
一旦擁有了值對象,就可以用下面的方法來操縱該對象:

OGNL:
對象圖形導航語言(OGNL)是一個功能強大的表達式語言是用來參考值棧上的數(shù)據(jù)和操縱。 OGNL也有助于在數(shù)據(jù)傳輸和類型轉(zhuǎn)換。
OGNL和JSP表達式語言很相似。 OGNL 基礎(chǔ)的理念是在 root或默認的對象范圍內(nèi)。默認或根對象的屬性,可以參考使用的標記符號(井號)。
如前所述,OGNL是基于上下文和Struts的構(gòu)建ActionContext 使用OGNL映射。ActionContext中映射包括以下:
application - 應用范圍的變量
session - 會話范圍的變量
root / value stack - 所有操作變量都保存在這里
request - 請求范圍的變量
parameters - 請求參數(shù)
atributes - 存儲的屬性頁面,請求,會話和應用范圍
重要的是要明白,操作對象是始終可用值棧中的。所以,因此,如果動作對象的屬性x和y有隨時供使用。
在ActionContext中的對象被稱為使用井號的符號,但是,值棧中的對象可以被直接引用,例如,如果員工是一個動作類的屬性,那么就可以得到如下參考:
<s:property value="name"/>
來代替
<s:property value="#name"/>
如果會話中有一個屬性叫做“l(fā)ogin”,可以找回如下:
<s:property value="#session.login"/>
OGNL還支持處理的集合 - 即映射,List和Set。例如,以顯示顏色的下拉列表,可以這樣做:
<s:select name="color" list="{'red','yellow','green'}" />
本OGNL表達式是巧妙地的解釋 "red","yellow","green"為顏色,并此基礎(chǔ)上建立一個列表。
OGNL表達式將被廣泛使用時,在接下來的章節(jié)中,我們將研究不同的標簽。因此,讓我們來看看它使用的一些例子在Form標簽/標簽/數(shù)據(jù)標簽控制和Ajax標簽。
ValueStack/OGNL 例子: 創(chuàng)建動作:
讓我們考慮以下動作類,當我們訪問值棧,然后設(shè)置幾個鍵,我們將在視圖,即訪問使用OGNL,JSP頁面。
package com.yiibai.struts2;
import java.util.*;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport{
private String name;
public String execute() throws Exception {
ValueStack stack = ActionContext.getContext().getValueStack();
Map<String, Object> context = new HashMap<String, Object>();
context.put("key1", new String("This is key1"));
context.put("key2", new String("This is key2"));
stack.push(context);
System.out.println("Size of the valueStack: " + stack.size());
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
其實,Struts 2的值棧的頂部增加了動作時執(zhí)行。所以,通常的方法是把東西值棧添加 getter/setter方法以使這些值在Action類,然后使用<s:property>標簽來訪問值。以下是展示如何在struts ActionContext 中 ValueStack 工作。
創(chuàng)建視圖
讓我們創(chuàng)建以下JSP文件 helloWorld.jsp 的要 WebContent 文件夾。這個視圖將被顯示動作返回“success”:
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Hello World</title> </head> <body> Entered value : <s:property value="name"/><br/> Value of key 1 : <s:property value="key1" /><br/> Value of key 2 : <s:property value="key2" /> <br/> </body> </html>
我們還需要創(chuàng)建的index.jsp在WebContent文件夾,其內(nèi)容如下:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Hello World</title> </head> <body> <h1>Hello World From Struts2</h1> <form action="hello"> <label for="name">Please enter your name</label><br/> <input type="text" name="name"/> <input type="submit" value="Say Hello"/> </form> </body> </html>
配置文件
以下是struts.xml文件的內(nèi)容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="helloworld" extends="struts-default">
<action name="hello"
class="com.yiibai.struts2.HelloWorldAction"
method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
以下是web.xml文件中的內(nèi)容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
右鍵點擊項目名稱,并單擊Export > WAR File創(chuàng)建一個WAR文件。然后將此WAR 部署在Tomcat 的 webapps目錄下。最后,啟動Tomcat服務(wù)器和嘗試訪問URL http://localhost:8080/HelloWorldStruts2/index.jsp。如以下畫面:

現(xiàn)在在給定的文本框中輸入任何單詞,然后點擊"Say Hello"按鈕執(zhí)行已定義的動作?,F(xiàn)在,如果檢查生成的日志,會發(fā)現(xiàn)下面的文本底部:
Size of the valueStack: 3
這將顯示以下畫面,這將顯示任何的值,將進入值為key1和key2,我們已經(jīng)把它們放入 ValueStack。
相關(guān)文章
Java實現(xiàn)人臉識別登錄、注冊等功能(最新完整版)
這段時間由于學校實行靜態(tài)化管理,寢室門和校門都是用了人臉識別的裝置,本系列項目從設(shè)計到實現(xiàn)源碼全部開源免費學習使用,對Java實現(xiàn)人臉識別登錄、注冊功能感興趣的朋友一起看看吧2022-05-05
java短網(wǎng)址服務(wù)(TinyURL)生成算法
這篇文章主要為大家詳細介紹了java短網(wǎng)址服務(wù)生成算法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08

