模拟初始化的jsp页面
<body> <a href="user/login">登录</a> </body>
public class User { private String name; //只要和前台页面name属性值一致 private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public User(String name, int age) { super(); this.name = name; this.age = age; } public User() { super(); } }
<?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.devMode" value="true"/> <package name="default" namespace="/" extends="struts-default"> </package> <!-- 引入关联的xml文件 --> <include file="user.xml"/> </struts>
<?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> <package name="user" namespace="/user" extends="default"> <action name="login" class="cn.bdqn.action.UserAction" method="login"> <result name="success">/success.jsp</result> </action> </package> </struts>
<?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> <package name="user" namespace="/user" extends="default"> <action name="login" class="cn.bdqn.action.UserAction" method="login"> <result name="success">/success.jsp</result> </action> </package> </struts>
public class UserAction extends ActionSupport{ public String login(){ System.out.println("进入了login"); //因为valueStack对象保存在了请求作用域中!所以必须获取request对象 Map<String, Object> request= (Map<String, Object>) ActionContext.getContext().get("request"); //向作用域中传递一个值 request.put("name", "小黑黑"); //获取ValueStack对象 ValueStack valueSatck= (ValueStack) request.get("struts.valueStack"); System.out.println("valueSatck的值是==》"+valueSatck); /** * valueSatck的值是==》com.opensymphony.xwork2.ognl.OgnlValueStack@5c5e85d3 * OgnlValueStack:是我们需要研究的对象! 是ValueStack的实现类! */ /** * 向值栈中增加数据 * 前台的s:debug中的Value Stack Contents中, * 增加了 name和age两个属性! * Value Stack Contents就是 CompoundRoot对象! * 这里面的内容!直接获取! */ valueSatck.getRoot().add(0, new User("小白",50)); /** * 向栈的上下文中增加数据 就是 前台 s:debug中的Stack Context * key:requestName * value:大家辛苦了 */ valueSatck.getContext().put("requestName", "大家辛苦了"); Map<String,Object> map=new HashMap(); map.put("1", "这是在Stack Context中"); map.put("2", "必须使用#才能访问我"); valueSatck.getContext().put("map",map); return "success"; } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'success.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:property value="name"/> <s:property value="age"/> <br/> <%-- 01.requestName 确实是在我们的栈上下文中的 key 如果对应的值是一个具体的数值! 不用使用# 02.request是一个作用域, 作用域中肯定有很多对象或者是属性! 如果想获取某个具体的对象应该使用# --%> <s:property value="requestName"/> <s:property value="#request.name"/> <s:debug/> </body> </html>