美文网首页struts2
struts2 获取push和set方法放进ActionCont

struts2 获取push和set方法放进ActionCont

作者: DouDouZH | 来源:发表于2018-05-18 18:06 被阅读0次

一、获取方法

获取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

相关文章

  • struts2 获取push和set方法放进ActionCont

    一、获取方法 获取set方法放入的值:(1) 获取set方法放入的值: (2)获取push方法放入的值: - 使...

  • Java Web之Struts2访问Servlet API

    方法一:直接获取 Map类型 方法二:实现接口,依赖struts2注入 Map 类型 方法三:获取httpServ...

  • Laravel Eloquent 小技巧

    1. push()方法保存模型和关系 您可以使用push()方法保存模型及其关联。 2.获取原始属性 当修改一条 ...

  • Struts2核心知识

    1,Struts2中get/set自动获取/设置数据 比如先在Action类里面添加一个name属性并加上get/...

  • IOS self 和下划线的区别

    1.self 访问属性的方法包含set和get方法。而通过下划线是获取自己的实例变量,不包含set和get方法 2...

  • ES6(Set-Map数据结构)

    一、Set集合 Set利用add添加元素 获取Set集合的长度用size Set集合有两种定义方法方法一: 方法二...

  • OC中下划线和self.的区别

    1.通过self. 访问,包含了set和get方法。通过下划线是获取自己的实例变量,不包含set和get的方法。 ...

  • iOS中_和self.的区别

    通过self.xxx访问的方法包括:set和get方法。但是使用_只是获取了自己的实例变量,并没有包含set ge...

  • iOS set get方法

    get方法是获取属性的值 set方法是给属性赋值 特别要注意的是: 在set方法中不能这样写 -(void)set...

  • struts2 获取web资源的两种方式

    一个struts2的请求就是一个action能够处理struts2请求的类就是Action类 获取web资源的方法...

网友评论

    本文标题:struts2 获取push和set方法放进ActionCont

    本文链接:https://www.haomeiwen.com/subject/apzgdftx.html