struts2 ValueStack的操作

本文详细介绍了如何在Struts框架中使用值栈进行数据传递,包括在Action类中向值栈添加数据,以及在JSP页面上通过Struts标签库读取这些数据的具体实现。

input.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<a href="deal2.action">点击跳转</a>
</body>
</html>

out.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>调用ValueStack本身的方法</h1>
	<s:debug></s:debug><br>
	<%-- <s:property value="user.name"/><br>
	<s:property value="user.password"/><br> --%>
	username:<s:property value="name"/><br>
	password:<s:property value="password"/><br>
	<h1>set方法</h1>
	<s:property value="name"/><br>
</body>
</html>

struts.xml

        <action name="deal2" class="Action.deal2">
			<result>/值栈的操作out.jsp</result>
		</action>

Action.java

package Action;

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

import bean.User;

public class deal2 extends ActionSupport {
	@Override
	public String execute() throws Exception {
		//向值栈中保存数据:
		//会的值栈对象:
		ValueStack valueStack=ActionContext.getContext().getValueStack();
		//使用push(Object obj); set(String key,Object obj);
		User user=new User("老马","666");
		//现在user在站定位置
		valueStack.push(user);
		
		valueStack.set("name", "老池");  //创建一个Map集合,将Map压入到栈中
		return SUCCESS;
	}
}

bean.java

package bean;

public class User {
	private String name;
	private String password;
	public User(String name,String password) {
		this.name=name;
		this.password=password;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值