一、測試頁面:index.jsphtml
<%@ 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>
二、actionjava
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; } }
三、struts.xmlapache
<?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>
四、跳轉頁HelloWorld.jspjsp
<%@ 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>
效果:post