Struts2学习:值栈(value stack)

1、测试页面:index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Index</title>
</head>
<body>
<form action="hello" method="post">
    <label for="pername">Please enter your name</label>
    <input id="pername" type="text" name="name" />
    <input type="submit" value="提交" />
</form>
</body>
</html>

2、action

package com.struts2demo.demo.action;

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

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

public class HelloWorldAction {
    private String name;

    public String excute(){
        ValueStack stack = ActionContext.getContext().getValueStack();
        Map<String, Object> context = new HashMap<String, Object>();

        context.put("key1", new String("This is key1"));
        context.put("key2", new String("This is key2"));
        stack.push(context);

        System.out.println("Size of the valueStack:" + stack.size());
        return "success";
    }

    public String getName() {
        return name;
    }

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

3、struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!-- 设置struts是否为开发模式,默认为false,测试阶段一般设为true. -->
    <constant name="struts.devMode" value="true" />
    <package name="suibian" extends="struts-default">
        <action name="hello" class="com.struts2demo.demo.action.HelloWorldAction"
                method="excute">
            <result name="success">/HelloWorld.jsp</result>
        </action>
    </package>
</struts>

4、跳转页HelloWorld.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Hello World !</title>
</head>
<body>
    Entered value: <s:property value="name" /><br>
    Value of key1: <s:property value="key1" /><br>
    Value of key2: <s:property value="key2" /><br>
</body>
</html>

效果:

转载于:https://my.oschina.net/u/4108765/blog/3059595

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值