//http://localhost:8080/struts2_03/user/useradd?type=1,传个参数type=1
public class UserAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private int type;
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String add(){
return SUCCESS;
}
}
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>
<constant name="struts.devMode" value="true" />
<package name="user" extends="struts-default" namespace="/user">
<action name="useradd" class="com.struts2_01.user.UserAction" method="add">
<!--这里result的type类型是redirect,跳转到user_add.jsp,还带着参数t,这里的t应该等于1,如果是result类型是forward,会共享一个值栈。-->
<result type="redirect">/user_add.jsp?t=${type}</result>
</action>
</package>
</struts>
user_add.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>
from value stack:<s:property value="t"/><!-- 这里的取值为空,因为是跳转到jsp页面,值栈中为空 -->
from Stack Context:<s:property value="#parameters.t"/><!-- 这里可以取到值,因为这里取得是Stack Context中的值 -->
</h1>
<s:debug></s:debug>
</body>
</html>
如图:value stack为空
Stack Context有t值