其他分享
首页 > 其他分享> > Struts2学习:值栈(value stack)

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>

效果:

 

标签:name,value,stack,util,Struts2,import,public,值栈,String
来源: https://www.cnblogs.com/wsfu/p/10362013.html