刨根问底-struts-自定义Result的返回type类型

struts.xml配置action的相应信息:


<struts>
	
	<package name="manage" extends="default" namespace="/manage">
		<action name="pici" class="com.xing.action.crawl.PiciAction" method="pici">
			<result name="success" >/WEB-INF/crawl/success.jsp</result>
			<result name="error" >/WEB-INF/web/error.jsp</result>
		</action>
....
result标签定义action执行完成后跳转的方向。result标签的type类型默认是dispatcher ,用来呈现JSP页面。下面我们来自定义一个type类型。


1、开发自定义Result是非常简单的,只需要实现com.opensymphony.xwork2.Result接口就可以了,这里也想同时观察一下ServletDispatcherResult,所以我们就定义ServletDispatcherResult

package test.dispatcher;

import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;

import org.apache.commons.lang.ObjectUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionInvocation;


 * <!-- START SNIPPET: description -->
public class ServletDispatcherResult extends StrutsResultSupport {

    private static final long serialVersionUID = -1970659272360685627L;

    private static final Logger LOG = LoggerFactory.getLogger(ServletDispatcherResult.class);

    public ServletDispatcherResult() {
        super();
    }

    public ServletDispatcherResult(String location) {
        super(location);
    }

    /**
     * Dispatches to the given location. Does its forward via a RequestDispatcher. If the
     * dispatch fails a 404 error will be sent back in the http response.
     *
     * @param finalLocation the location to dispatch to.
     * @param invocation    the execution state of the action
     * @throws Exception if an error occurs. If the dispatch fails the error will go back via the
     *                   HTTP request.
     */
    public void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Forwarding to location " + finalLocation);
        }

        PageContext pageContext = ServletActionContext.getPageContext();

        if (pageContext != null) {
            pageContext.include(finalLocation);
        } else {
            HttpServletRequest request = ServletActionContext.getRequest();
            HttpServletResponse response = ServletActionContext.getResponse();
            RequestDispatcher dispatcher = request.getRequestDispatcher(finalLocation);

            //add parameters passed on the location to #parameters
            // see WW-2120
            if (invocation != null && finalLocation != null && finalLocation.length() > 0
                    && finalLocation.indexOf("?") > 0) {
                String queryString = finalLocation.substring(finalLocation.indexOf("?") + 1);
                Map parameters = (Map) invocation.getInvocationContext().getContextMap().get("parameters");
//                Map queryParams = UrlHelper.parseQueryString(queryString, true);
//                if (queryParams != null && !queryParams.isEmpty())
//                    parameters.putAll(queryParams);
            }

            // if the view doesn't exist, let's do a 404
            if (dispatcher == null) {
                response.sendError(404, "result '" + finalLocation + "' not found");

                return;
            }
//  public static final String STRUTS_ACTION_TAG_INVOCATION= "struts.actiontag.invocation";
            //if we are inside an action tag, we always need to do an include
            Boolean insideActionTag = (Boolean) ObjectUtils.defaultIfNull(request.getAttribute("struts.actiontag.invocation"), Boolean.FALSE);

            // If we're included, then include the view
            // Otherwise do forward
            // This allow the page to, for example, set content type
            if (!insideActionTag && !response.isCommitted() && (request.getAttribute("javax.servlet.include.servlet_path") == null)) {
                request.setAttribute("struts.view_uri", finalLocation);
                request.setAttribute("struts.request_uri", request.getRequestURI());

                dispatcher.forward(request, response);
            } else {
                dispatcher.include(request, response);
            }
        }
    }
}

在这里finalLocation值:/gaokao/WEB-INF/index.jsp

request.getRequestURI()值:/WEB-INF/index.jsp

2、然后在struts.xml文件中配置一下

<package name="default" extends="struts-default">
		<result-types>
			<result-type name="myDispatcher" class="test.dispatcher.ServletDispatcherResult" default="false"/>
		</result-types>
3、使用自定义的type类型myDispatcher,例如:
<action name="login" class="com.xing.action.UserAction" method="login">
			<result name="success" type="myDispatcher">/WEB-INF/index.jsp</result>
			<result name="error" >/WEB-INF/login.jsp</result>
		</action>

到这里,主要的代码已经写完了。

转载于:https://my.oschina.net/winHerson/blog/107761

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值