Struts(10)值栈/OGNL

值栈:

值栈是一个集合中的几个对象保持下列对象提供的顺序:

SN 对象& 描述
1 Temporary Objects
There are various temporary objects which are created during execution of a page. For example the current iteration value for a collection being looped over in a JSP tag.
2 The Model Object
If you are using model objects in your struts application, the current model object is placed before the action on the value stack
3 The Action Object
This will be the current action object which is being executed.
4 Named Objects
These objects include #application, #session, #request, #attr and #parameters and refer to the corresponding servlet scopes

值栈可以通过JSP,Velocity或者Freemarker的标签。有各种不同的标签在单独的章节中,我们将学习,用于获取和设置Struts 2.0 的值栈。 ValueStack的对象里面可以得到动作如下:

<span class="typ" style="margin: 0px; padding: 0px;">ActionContext</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">getContext</span><span class="pun" style="margin: 0px; padding: 0px;">().</span><span class="pln" style="margin: 0px; padding: 0px;">getValueStack</span><span class="pun" style="margin: 0px; padding: 0px;">()</span>

一旦拥有了值对象,就可以用下面的方法来操纵该对象:

SN ValueStack的方法及说明
1 Object findValue(String expr)
Find a value by evaluating the given expression against the stack in the default search order.
2 CompoundRoot getRoot()
Get the CompoundRoot which holds the objects pushed onto the stack.
3 Object peek()
Get the object on the top of the stack without changing the stack.
4 Object pop()
Get the object on the top of the stack and remove it from the stack.
5 void push(Object o)
Put this object onto the top of the stack.
6 void set(String key, Object o)
Sets an object on the stack with the given key so it is retrievable by findValue(key,...)
7 void setDefaultType(Class defaultType)
Sets the default type to convert to if no type is provided when getting a value.
8 void setValue(String expr, Object value)
Attempts to set a property on a bean in the stack with the given expression using the default search order.
9 int size()
Get the number of objects in the stack.

OGNL:

对象图形导航语言(OGNL)是一个功能强大的表达式语言是用来参考值栈上的数据和操纵。 OGNL也有助于在数据传输和类型转换。

OGNL和JSP表达式语言很相似。 OGNL 基础的理念是在 root或默认的对象范围内。默认或根对象的属性,可以参考使用的标记符号(井号)。

如前所述,OGNL是基于上下文和Struts的构建ActionContext 使用OGNL映射。ActionContext中映射包括以下:

  • application - 应用范围的变量

  • session - 会话范围的变量

  • root / value stack - 所有操作变量都保存在这里

  • request - 请求范围的变量

  • parameters - 请求参数

  • atributes - 存储的属性页面,请求,会话和应用范围

重要的是要明白,操作对象是始终可用值栈中的。所以,因此,如果动作对象的属性x和y有随时供使用。

在ActionContext中的对象被称为使用井号的符号,但是,值栈中的对象可以被直接引用,例如,如果员工是一个动作类的属性,那么就可以得到如下参考:

<span class="pln" style="margin: 0px; padding: 0px;">  </span><span class="tag" style="margin: 0px; padding: 0px;"><s:property</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">value</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"name"</span><span class="tag" style="margin: 0px; padding: 0px;">/></span>

来代替

<span class="pln" style="margin: 0px; padding: 0px;">  </span><span class="tag" style="margin: 0px; padding: 0px;"><s:property</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">value</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"#name"</span><span class="tag" style="margin: 0px; padding: 0px;">/></span>

如果会话中有一个属性叫做“login”,可以找回如下:

<span class="pln" style="margin: 0px; padding: 0px;">  </span><span class="tag" style="margin: 0px; padding: 0px;"><s:property</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">value</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"#session.login"</span><span class="tag" style="margin: 0px; padding: 0px;">/></span>

OGNL还支持处理的集合 - 即映射,List和Set。例如,以显示颜色的下拉列表,可以这样做:

<span class="pln" style="margin: 0px; padding: 0px;">  </span><span class="tag" style="margin: 0px; padding: 0px;"><s:select</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"color"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">list</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"{'red','yellow','green'}"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">/></span>

本OGNL表达式是巧妙地的解释 "red","yellow","green"为颜色,并此基础上建立一个列表。 

OGNL表达式将被广泛使用时,在接下来的章节中,我们将研究不同的标签。因此,让我们来看看它使用的一些例子在Form标签/标签/数据标签控制和Ajax标签。

ValueStack/OGNL 例子:

创建动作:

让我们考虑以下动作类,当我们访问值栈,然后设置几个键,我们将在视图,即访问使用OGNL,JSP页面。

<span class="kwd" style="margin: 0px; padding: 0px;">package</span><span class="pln" style="margin: 0px; padding: 0px;"> com</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">yiibai</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">struts2</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">

</span><span class="kwd" style="margin: 0px; padding: 0px;">import</span><span class="pln" style="margin: 0px; padding: 0px;"> java</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">util</span><span class="pun" style="margin: 0px; padding: 0px;">.*;</span><span class="pln" style="margin: 0px; padding: 0px;"> 

</span><span class="kwd" style="margin: 0px; padding: 0px;">import</span><span class="pln" style="margin: 0px; padding: 0px;"> com</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">opensymphony</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">xwork2</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">util</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="typ" style="margin: 0px; padding: 0px;">ValueStack</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="kwd" style="margin: 0px; padding: 0px;">import</span><span class="pln" style="margin: 0px; padding: 0px;"> com</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">opensymphony</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">xwork2</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="typ" style="margin: 0px; padding: 0px;">ActionContext</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="kwd" style="margin: 0px; padding: 0px;">import</span><span class="pln" style="margin: 0px; padding: 0px;"> com</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">opensymphony</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">xwork2</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="typ" style="margin: 0px; padding: 0px;">ActionSupport</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">

</span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">class</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">HelloWorldAction</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">extends</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">ActionSupport</span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="kwd" style="margin: 0px; padding: 0px;">private</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pln" style="margin: 0px; padding: 0px;"> name</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">

   </span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pln" style="margin: 0px; padding: 0px;"> execute</span><span class="pun" style="margin: 0px; padding: 0px;">()</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">throws</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Exception</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="typ" style="margin: 0px; padding: 0px;">ValueStack</span><span class="pln" style="margin: 0px; padding: 0px;"> stack </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">ActionContext</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">getContext</span><span class="pun" style="margin: 0px; padding: 0px;">().</span><span class="pln" style="margin: 0px; padding: 0px;">getValueStack</span><span class="pun" style="margin: 0px; padding: 0px;">();</span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="typ" style="margin: 0px; padding: 0px;">Map</span><span class="pun" style="margin: 0px; padding: 0px;"><</span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Object</span><span class="pun" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;"> context </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">new</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">HashMap</span><span class="pun" style="margin: 0px; padding: 0px;"><</span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Object</span><span class="pun" style="margin: 0px; padding: 0px;">>();</span><span class="pln" style="margin: 0px; padding: 0px;">

      context</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">put</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="str" style="margin: 0px; padding: 0px;">"key1"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">new</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="str" style="margin: 0px; padding: 0px;">"This is key1"</span><span class="pun" style="margin: 0px; padding: 0px;">));</span><span class="pln" style="margin: 0px; padding: 0px;"> 
      context</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">put</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="str" style="margin: 0px; padding: 0px;">"key2"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">new</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="str" style="margin: 0px; padding: 0px;">"This is key2"</span><span class="pun" style="margin: 0px; padding: 0px;">));</span><span class="pln" style="margin: 0px; padding: 0px;">
      stack</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">push</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;">context</span><span class="pun" style="margin: 0px; padding: 0px;">);</span><span class="pln" style="margin: 0px; padding: 0px;">

      </span><span class="typ" style="margin: 0px; padding: 0px;">System</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="kwd" style="margin: 0px; padding: 0px;">out</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">println</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="str" style="margin: 0px; padding: 0px;">"Size of the valueStack: "</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">+</span><span class="pln" style="margin: 0px; padding: 0px;"> stack</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">size</span><span class="pun" style="margin: 0px; padding: 0px;">());</span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="kwd" style="margin: 0px; padding: 0px;">return</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"success"</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;">  

   </span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pln" style="margin: 0px; padding: 0px;"> getName</span><span class="pun" style="margin: 0px; padding: 0px;">()</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="kwd" style="margin: 0px; padding: 0px;">return</span><span class="pln" style="margin: 0px; padding: 0px;"> name</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;">

   </span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">void</span><span class="pln" style="margin: 0px; padding: 0px;"> setName</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pln" style="margin: 0px; padding: 0px;"> name</span><span class="pun" style="margin: 0px; padding: 0px;">)</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="kwd" style="margin: 0px; padding: 0px;">this</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">name </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> name</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="pun" style="margin: 0px; padding: 0px;">}</span>

其实,Struts 2的值栈的顶部增加了动作时执行。所以,通常的方法是把东西值栈添加 getter/setter方法以使这些值在Action类,然后使用<s:property>标签来访问值。以下是展示如何在struts ActionContext 中 ValueStack  工作。

创建视图

让我们创建以下JSP文件 helloWorld.jsp 的要 WebContent 文件夹。这个视图将被显示动作返回“success”:

<span class="pun" style="margin: 0px; padding: 0px;"><%@</span><span class="pln" style="margin: 0px; padding: 0px;"> page contentType</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"text/html; charset=UTF-8"</span><span class="pln" style="margin: 0px; padding: 0px;"> %>
</span><span class="pun" style="margin: 0px; padding: 0px;"><%@</span><span class="pln" style="margin: 0px; padding: 0px;"> taglib prefix</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"s"</span><span class="pln" style="margin: 0px; padding: 0px;"> uri</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"/struts-tags"</span><span class="pln" style="margin: 0px; padding: 0px;"> %>
</span><span class="tag" style="margin: 0px; padding: 0px;"><html></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><head></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><title></span><span class="pln" style="margin: 0px; padding: 0px;">Hello World</span><span class="tag" style="margin: 0px; padding: 0px;"></title></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></head></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><body></span><span class="pln" style="margin: 0px; padding: 0px;">
   Entered value : </span><span class="tag" style="margin: 0px; padding: 0px;"><s:property</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">value</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"name"</span><span class="tag" style="margin: 0px; padding: 0px;">/><br/></span><span class="pln" style="margin: 0px; padding: 0px;">
   Value of key 1 : </span><span class="tag" style="margin: 0px; padding: 0px;"><s:property</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">value</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"key1"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">/><br/></span><span class="pln" style="margin: 0px; padding: 0px;">
   Value of key 2 : </span><span class="tag" style="margin: 0px; padding: 0px;"><s:property</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">value</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"key2"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">/></span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;"><br/></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></body></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></html></span>

我们还需要创建的index.jsp在WebContent文件夹,其内容如下:

<span class="pun" style="margin: 0px; padding: 0px;"><%@</span><span class="pln" style="margin: 0px; padding: 0px;"> page language</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"java"</span><span class="pln" style="margin: 0px; padding: 0px;"> contentType</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"text/html; charset=ISO-8859-1"</span><span class="pln" style="margin: 0px; padding: 0px;">
   pageEncoding</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"ISO-8859-1"</span><span class="pln" style="margin: 0px; padding: 0px;">%>
</span><span class="pun" style="margin: 0px; padding: 0px;"><%@</span><span class="pln" style="margin: 0px; padding: 0px;"> taglib prefix</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"s"</span><span class="pln" style="margin: 0px; padding: 0px;"> uri</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"/struts-tags"</span><span class="pln" style="margin: 0px; padding: 0px;">%>
   </span><span class="dec" style="margin: 0px; padding: 0px;"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><html></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><head></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><title></span><span class="pln" style="margin: 0px; padding: 0px;">Hello World</span><span class="tag" style="margin: 0px; padding: 0px;"></title></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></head></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><body></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"><h1></span><span class="pln" style="margin: 0px; padding: 0px;">Hello World From Struts2</span><span class="tag" style="margin: 0px; padding: 0px;"></h1></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"><form</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">action</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"hello"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"><label</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">for</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"name"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">Please enter your name</span><span class="tag" style="margin: 0px; padding: 0px;"></label><br/></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"><input</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">type</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"text"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"name"</span><span class="tag" style="margin: 0px; padding: 0px;">/></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"><input</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">type</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"submit"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">value</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"Say Hello"</span><span class="tag" style="margin: 0px; padding: 0px;">/></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"></form></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></body></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></html></span>

配置文件

以下是struts.xml文件的内容:

<span class="pln" style="margin: 0px; padding: 0px;"> 
</span><span class="pun" style="margin: 0px; padding: 0px;"><?</span><span class="pln" style="margin: 0px; padding: 0px;">xml version</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"1.0"</span><span class="pln" style="margin: 0px; padding: 0px;"> encoding</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"UTF-8"</span><span class="pun" style="margin: 0px; padding: 0px;">?></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="dec" style="margin: 0px; padding: 0px;"><!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"></span><span class="pln" style="margin: 0px; padding: 0px;">

</span><span class="tag" style="margin: 0px; padding: 0px;"><struts></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"><constant</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"struts.devMode"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">value</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"true"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">/></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"><package</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"helloworld"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">extends</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"struts-default"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">

      </span><span class="tag" style="margin: 0px; padding: 0px;"><action</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"hello"</span><span class="pln" style="margin: 0px; padding: 0px;"> 
         </span><span class="atn" style="margin: 0px; padding: 0px;">class</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"com.yiibai.struts2.HelloWorldAction"</span><span class="pln" style="margin: 0px; padding: 0px;"> 
         </span><span class="atn" style="margin: 0px; padding: 0px;">method</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"execute"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">
         </span><span class="tag" style="margin: 0px; padding: 0px;"><result</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"success"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">/HelloWorld.jsp</span><span class="tag" style="margin: 0px; padding: 0px;"></result></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"></action></span><span class="pln" style="margin: 0px; padding: 0px;">

   </span><span class="tag" style="margin: 0px; padding: 0px;"></package></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></struts></span>

以下是web.xml文件中的内容:

<span class="pun" style="margin: 0px; padding: 0px;"><?</span><span class="pln" style="margin: 0px; padding: 0px;">xml version</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"1.0"</span><span class="pln" style="margin: 0px; padding: 0px;"> encoding</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"UTF-8"</span><span class="pun" style="margin: 0px; padding: 0px;">?></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><web-app</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">xmlns:xsi</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"http://www.w3.org/2001/XMLSchema-instance"</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="atn" style="margin: 0px; padding: 0px;">xmlns</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"http://java.sun.com/xml/ns/javaee"</span><span class="pln" style="margin: 0px; padding: 0px;"> 
   </span><span class="atn" style="margin: 0px; padding: 0px;">xmlns:web</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="atn" style="margin: 0px; padding: 0px;">xsi:schemaLocation</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"http://java.sun.com/xml/ns/javaee 
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="atn" style="margin: 0px; padding: 0px;">id</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"WebApp_ID"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">version</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"3.0"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">
   
   </span><span class="tag" style="margin: 0px; padding: 0px;"><display-name></span><span class="pln" style="margin: 0px; padding: 0px;">Struts 2</span><span class="tag" style="margin: 0px; padding: 0px;"></display-name></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"><welcome-file-list></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"><welcome-file></span><span class="pln" style="margin: 0px; padding: 0px;">index.jsp</span><span class="tag" style="margin: 0px; padding: 0px;"></welcome-file></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"></welcome-file-list></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"><filter></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"><filter-name></span><span class="pln" style="margin: 0px; padding: 0px;">struts2</span><span class="tag" style="margin: 0px; padding: 0px;"></filter-name></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"><filter-class></span><span class="pln" style="margin: 0px; padding: 0px;">
         org.apache.struts2.dispatcher.FilterDispatcher
      </span><span class="tag" style="margin: 0px; padding: 0px;"></filter-class></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"></filter></span><span class="pln" style="margin: 0px; padding: 0px;">

   </span><span class="tag" style="margin: 0px; padding: 0px;"><filter-mapping></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"><filter-name></span><span class="pln" style="margin: 0px; padding: 0px;">struts2</span><span class="tag" style="margin: 0px; padding: 0px;"></filter-name></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"><url-pattern></span><span class="pln" style="margin: 0px; padding: 0px;">/*</span><span class="tag" style="margin: 0px; padding: 0px;"></url-pattern></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"></filter-mapping></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></web-app></span>

右键点击项目名称,并单击Export > WAR File创建一个WAR文件。然后将此WAR 部署在Tomcat 的 webapps目录下。最后,启动Tomcat服务器和尝试访问URL http://localhost:8080/HelloWorldStruts2/index.jsp。如以下画面:

Struts(10)值栈/OGNL_第1张图片

现在在给定的文本框中输入任何单词,然后点击"Say Hello"按钮执行已定义的动作。现在,如果检查生成的日志,会发现下面的文本底部:

<span class="pln" style="margin: 0px; padding: 0px;"> 
</span><span class="typ" style="margin: 0px; padding: 0px;">Size</span><span class="pln" style="margin: 0px; padding: 0px;"> of the valueStack</span><span class="pun" style="margin: 0px; padding: 0px;">:</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="lit" style="margin: 0px; padding: 0px;">3</span>

这将显示以下画面,这将显示任何的值,将进入值为key1和key2,我们已经把它们放入 ValueStack。

你可能感兴趣的:(对象,栈)