struts2 中请求转发与请求重定向方法,重定向携带参数

Struts2请求方式有七种,主要介绍转发和重定向,其它有兴趣可以自行去了解。

转发和重定向又分为转发、重定向到jsp、和到action

下面就直接给出他们的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	
	
	<package name="result" namespace="/" extends="struts-default">
		<!-- 转发到jsp -->
		<action name="Demo1Action" class="struts2.result.Demo1Action" method="execute">
			
			<result name="success" type="dispatcher">/hello.jsp</result>
		</action>
		<!-- 重定向到jsp -->
		<action name="Demo2Action" class="struts2.result.Demo2Action" method="execute">
			
			<result name="success" type="redirect">/hello.jsp</result>
		</action>
		<!-- 转发到Action -->
		<action name="Demo3Action" class="struts2.result.Demo3Action" method="execute">
			<result name="success" type="chain">
				<!-- action的名字 -->
				<param name="actionName">Demo1Action</param>
				<!-- action的命名空间 -->
				<param name="namespace">/</param>
			</result>
			
		</action>
		<!-- 重定向到Action -->
		<action name="Demo4Action" class="struts2.result.Demo4Action" method="execute">
			<result name="success" type="redirectAction">
				<param name="actionName">Demo1Action</param>
				<param name="namespace">/</param>
			</result>
			
		</action>
	</package>
	
	
</struts>

下面再介绍一下重定向的时候携带参数的问题(直接给出代码)

package test;

import com.opensymphony.xwork2.ActionSupport;

public class DemoAction extends ActionSupport{
	//必须要有get / set方法
	private String name;
	private Integer age;
	
	@Override
	public String execute() throws Exception {
		//模仿从数据库取数据
		name = "tom";
		age = 18;
		
		
		return SUCCESS;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}	

}
package test;

public class Demo1Action {


	public String execute(){
		
		return "success";
	}
	
}
<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

	<package name="hello" namespace="/" extends="struts-default">
		<action name="DemoAction" class="test.DemoAction" method="execute">
			<result name="success" type="redirectAction">
				<param name="actionName">Demo1Action</param>
				<param name="namespace">/</param>
				<!-- 
					如果添加的参数struts“看不懂”,就会作为参数附加重定向的路径后面
					如果参数是动态的,可以使用${}包裹ognl表达式,动态取值
					<param name="age">${age}</param>
				 -->
				<param name="name">${name}</param>
				<param name="age">${age}</param>
			</result>
		</action>
		
		<action name="Demo1Action" class="test.Demo1Action" method="execute">
			<result name="success" type="dispatcher">/hello.jsp</result>
		</action>
	</package>
</struts>

代码亲测有效

效果图

1、访问地址

2、结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值