struts2 使用redirect实现带参重定向显示action中的变量
2014-05-30 15:45
465 查看
1、在struts.xml中加入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
<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
相关文章推荐
- struts2的各种视图转发类型:redirect,redirectAction,plainText和字符编码类URLEncoder和URLDecoder的使用
- struts2使用action方式实现文件下载(支持中文文件名)
- struts2中redirectAction方式 重定向带参数/中文参数乱码
- struts2_day01_15_Action的方法访问(使用通配符实现)_16_Action的方法访问(通配符分析)
- struts2 type="redirectAction"重定向 与动态调用方法
- struts2的各种视图转发类型:redirect,redirectAction,plainText和字符编码类URLEncoder和URLDecoder的使用
- struts2中使用JSTL读取action中的变量注意事项
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
- 应用jQuery Ajax 实现前台与Struts2中Action的交互,并返回一个ArrayList的JSON对象,在前端表格中显示
- struts2基础学习收录重点之OGNL动态配置result , 使用注解配置action,实现java国际化
- jQuery使用load函数访问Struts2中的Action实现局部刷新和注册事件
- 使用struts2标签<s:action无法显示引用页面问题
- 使用struts2和AJAX实现文件上传并显示进度条
- struts2使用action方式实现文件下载(支持中文文件名)
- 在Struts2中,如何使用JSTL来读取Action中的变量?
- jsp页面取struts2 action的变量供java、servlet使用
- Struts2中redirectAction重定向后乱码问题与解决
- 使用struts2标签<s:action无法显示引用页面问题
- struts2使用ajax实现页面和action之间通过json数据交互
- 关于ajax载入窗口使用RedirectToAction在窗口显示的问题