struts2 使用redirect实现带参重定向显示action中的变量

本文详细介绍了在Struts2框架中如何使用action配置实现重定向功能,并将参数通过redirect方式传递到下一个页面。通过建立特定的action类和配置文件,实现了在重定向时将数据作为URL参数展示,同时展示了如何在目标页面接收并解码这些参数。此过程涉及了Struts2框架的基本应用、参数传递机制以及HTTP重定向原理。
1、在struts.xml中加入action配置:
<action name="redirect2"  class="cn.xs.action.HelloWorldAction" method = "execute">
	<result type="redirect">/employeeAdd.jsp?username=${username}</result>
</action>

2、在src中的建立cn.xs.action包,并建立HelloWorldAction.java,内容如下:

// ---------------------------------------------------------
// @author    sheng.xu
// @version   1.0.0
// @date	2014年5月29日
// ---------------------------------------------------------
package cn.xs.action;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @author sheng.xu
 *
 */
public class HelloWorldAction {
	
	public String message;
	public String username;
	
	

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	public String execute() throws Exception{
		this.username = URLEncoder.encode("strus2学习","UTF-8"); //将username通过redirect传到前台,将username显示在url中
		this.message = "aaaaaaaaaaaaa";
		return "success";
	}
}
3、在WebContent中建立employeeAdd.jsp,内容如下:

<%@ page language="java" import="java.util.*,java.net.URLDecoder" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%= URLDecoder.decode(new String(request.getParameter("username").getBytes("ISO8859-1"),"UTF-8"),"UTF-8") %>
	<form action="/xxx">
		姓名2:<input type="text" name="xxx" />
	</form>
</body>
</html>


4、访问方式:localhost:8080/Struts2/test/redirect2


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值