struts2 獲取push和set方法放進ActionContext中的值

一、獲取方法

獲取set方法放入的值:
(1) 獲取set方法放入的值: <s:property value="key"/>
(2)獲取push方法放入的值:<s:property value="[0].top"/>
- 使用push方法設置值,沒有屬性,只有值
- 向值棧放數據,把數據存到數組里面,數組名top,獲取數據值

二、代碼

ValuesStackAction1.java

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;

public class ValuesStackAction1 extends ActionSupport{
   @Override
   public String execute() throws Exception {
       //第一種方式使用值棧對象里面的set方法
       //1取值棧對象
       ActionContext context=ActionContext.getContext();
       ValueStack stack=context.getValueStack();
       //2set方法
       stack.set("username", "doudou");
       
       //3調用push方法   最后放的為棧頂元素
       stack.push("lisi");
       
       return SUCCESS;
   }
}

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <package name="default" namespace="/" extends="struts-default">
        <action name="userAction1" class="work.zhangdoudou.Action.ValuesStackAction1" method="execute">
            <result name="success">/ValuesStack.jsp</result>
        </action>   
    </package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>struts2_ognl</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'ValuesStackAction.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  <body>
   <s:debug></s:debug>

     獲取set方法放入的值:
     <s:property value="username"/><br>
     獲取push方法放入的值:
     <!-- 使用push方法設置值,沒有屬性,只有值
          向值棧放數據,把數據存到數組里面,數組名top,獲取數據值
      -->
      <s:property value="[0].top"/>
  </body>
</html>

三、運行效果

image.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容