struts2 向值棧中放數據的三種方式

一、獲取值棧的對象,調用值棧的set方法

1、代碼

ValuesStackAction1.java

package work.zhangdoudou.Action;

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();
        stack.set("username", "doudou");
        
        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>

ValuesStack.jsp

<%@ 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>
  </body>
</html>

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>
2、運行結果
image.png
  • 有一個java.util.HashMap 這個是我們放的值
  • root其實是list集合在list中放了一個map

二、獲取值棧對象,調用值棧的push方法

1、代碼

ValuesStackAction1.java

package work.zhangdoudou.Action;

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;
    }
}
2、執行效果
image.png
  • 它把push的元素當做字符串放進值棧

三、在action定義變量,生成get方法(常用)

1、代碼

ValuesStackAction2.java

package work.zhangdoudou.Action;

import com.opensymphony.xwork2.ActionSupport;

public class ValuesStackAction2 extends ActionSupport{
    //1定義
    private String name;
    //2生成個get方法
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String execute() throws Exception {
        //在執行的方法里向變量設置值
        name="wangwu";
        
        return SUCCESS;
    }
}

struts.xml

<action name="userAction2" class="work.zhangdoudou.Action.ValuesStackAction2" method="execute">
            <result name="success">/ValuesStack.jsp</result>
        </action>
2、運行結果
image.png
  • 這樣做不用重新分配空間,可以減少空間的langfeo
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,766評論 18 399
  • 概述 什么是Struts2的框架Struts2是Struts1的下一代產品,是在 struts1和WebWork的...
    inke閱讀 2,276評論 0 50
  • 今天進行了第二輪復試,也是生物科技有限公司的,雖然之前進行過一次面試,但是這次緊張很多,也更知道自己要準備什么,不...
    馬田心Martinc手作閱讀 1,471評論 16 1
  • 銳捷5.20自帶的小銳WiFi只能連一個設備,當然這對于很多人來說已經夠用了。如果你有兩個手機或一個手機一個平板,...
    KK7384閱讀 7,114評論 0 4
  • 我擠進擁擠的人潮 忘記所有的驕傲 在明媚的陽光里尋找 從未來到的 那些從未去過的地方 在腦海里越來越清晰 忘記了什...
    楓林景fly閱讀 246評論 0 1