您的位置:首页 > 编程语言 > Java开发

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

2014-05-30 15:45 465 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐