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,767评论 18 399
  • 概述 什么是Struts2的框架Struts2是Struts1的下一代产品,是在 struts1和WebWork的...
    inke阅读 2,276评论 0 50
  • 今天进行了第二轮复试,也是生物科技有限公司的,虽然之前进行过一次面试,但是这次紧张很多,也更知道自己要准备什么,不...
    马田心Martinc手作阅读 1,480评论 16 1
  • 锐捷5.20自带的小锐WiFi只能连一个设备,当然这对于很多人来说已经够用了。如果你有两个手机或一个手机一个平板,...
    KK7384阅读 7,130评论 0 4
  • 我挤进拥挤的人潮 忘记所有的骄傲 在明媚的阳光里寻找 从未来到的 那些从未去过的地方 在脑海里越来越清晰 忘记了什...
    枫林景fly阅读 247评论 0 1