struts2自定义result中的type

3 篇文章 0 订阅

我们清楚<action>中默认type="dispatcher",dispatcher对应的就是

<result-types>
	<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult"></result-type>
</result-types>

我们知道在一个action中如果没有配置name那么它默认就是success,如果没有配置type,那么默认就是dispatcher。

如果我们要使用自定义的result-type我们只需要将对应class继承ServletDispatcherResult,然后重写其中的doExecute方法即可。然后再在action中配置对象的type名称即可。ServletDispatcherResult可以将请求导向至目标资源(通常为jsp页面)。

parentAction.java

@SuppressWarnings("serial")
public class ParentAction extends ActionSupport
{
	
	public String execute() throws Exception
	{
		return Action.SUCCESS;
	}
}
struts.xml配置

<?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.1.dtd">
<struts>
  <package name="parent" extends="struts-default">
		<result-types>
			<result-type name="mainParentPage" 
			 class="com.xuzengqiang.project.dispatcher.ParentPageDispatcherResult"/>
		</result-types>
		<action name="parentAction" class="com.xuzengqiang.project.struts2.action.ParentAction">
			<result type="mainParentPage"></result>
		</action>
	</package>
</struts>
ParentDispatcherResult.java

public class ParentPageDispatcherResult extends ServletDispatcherResult
{
	private static final long serialVersionUID = -1110416292983125424L;

	@Override
	public void doExecute(String path, ActionInvocation invocation) throws Exception
	{
		path="/WEB-INF/parent.jsp";
		super.doExecute(path, invocation);
	}
}
这样就可以实现跳转了。

由于在parentAction中我没有配置name,默认就是success,就会找到与之对应的mainParentPage,从而找到对应的ParentPageDispatcher类。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值