在拦截器中获取结果类型 [英] Getting Result type in Interceptor

查看:235
本文介绍了在拦截器中获取结果类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不同(HTML和JSON)结果类型的Struts2操作。他们使用常见的拦截器。

I have Struts2 actions with different (HTML and JSON ) result types. They use common interceptor.

如果需要拦截请求,如何根据给定的操作结果类型返回结果?

If needed to intercept the request, how to return result based on given action result type?

例如,我的 Action.ERROR 转发到JSP页面。如果action是JSON类型,我想改为转发JSON错误。

For example, my Action.ERROR forwards to JSP page. If action is JSON type I want to forward JSON error instead.

推荐答案


我有Struts2动作不同的(HTML和JSON)结果类型。他们使用常见的拦截器。如果需要拦截请求,如何根据给定的动作结果类型返回结果?

I have Struts2 actions with different (HTML and JSON ) result types. They use common interceptor. If need to intercept the request, how to return result based on given action result type?

例如,我的Action.ERROR转发到JSP页面。如果action是JSON类型,我想转发JSON错误。请建议。

For example, my Action.ERROR forwards to JSP page. If action is JSON type I want to forward JSON error instead. Please advice.

虽然一个Action没有类型,但如果确实如此,那么,如果以AJAX方式调用Action,就像返回JSON的操作一样,它的所有结果具有相同的结果类型(在本例中为JSON),除非您使用单个Action执行不同的逻辑动作(ajax和非ajax操作,这是一种反模式);

Although is true that an Action has not a type, it's also true that, if an Action is called in an AJAX way, like an action returning JSON, all of its results should have the same result type (JSON in this case), unless you are using a single Action to perform different logical actions (ajax and non-ajax operations, that is an anti-pattern);

这就是说,如果你想返回正确的 GLOBAL错误结果,来自您的所有动作(每个动作都有结果类型)使用的拦截器,基于 其他 结果类型(让我们说: SUCCESS ,假设每个Action都有一个 SUCCESS 结果),这就是这样做的方法:

That said, if you want to return the proper GLOBAL error result, from inside an Interceptor that is used by all of your Actions (each one with its result type), based on their other result type (let's say: SUCCESS, assuming every Action has a SUCCESS result), this is the way to do it:

public String intercept(ActionInvocation invocation) throws Exception {

    // Get the action configuration defined in struts.xml
    ActionConfig config = invocation.getProxy().getConfig(); 

    // Get the SUCCESS result configured for that Action
    ResultConfig success = config.getResults().get("success");

    // Get the class of the SUCCESS result
    Object clazz = success.getClass(); 

    /* .... oops, some error occurred !! 
       We now need to redirect to the right global error result .... */

    if (clazz instanceof org.apache.struts2.dispatcher.ServletDispatcherResult) {
        log.debug("Struts2 Result type: CLASSIC");
        return "error";
    } else if (clazz instanceof org.apache.struts2.json.JSONResult) {
        log.debug("Struts2 Result type: JSON");
        return "jsonError";
    } else {
        log.debug("Struts2 Result type: SOMETHING ELSE, returning default ");
        return "error";
    }
}

虽然这在技术上是可行的,但我会劝阻它,因为......没有真正的理由去做;

Although this is technically possible, i would discourage it because... there is no real reason to do it;

为了您的目的,请记住每个全局结果的范围是 < package> ;

For your purpose, remember that each global result is scoped in its <package>;

因为你可以(/应该)为Classic提供两个不同的包操作(< package> 扩展 struts-default )和JSON操作(< ; package> 扩展 json-default ),你可以简单地为每个具有相同名称但不同的包定义两个不同的全局错误结果结果类型;这样Interceptor将调用相对于当前Action的包的那个,输出所需类型的结果。

Since you could (/should) have two different packages for Classic Actions (a <package> extending struts-default) and JSON Actions (a <package> extending json-default), you can simply define two different global error result for each package with the same name but different result type; this way the Interceptor will call the one relative to the package of the current Action, outputting the desired kind of result.

这篇关于在拦截器中获取结果类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆