Struts 2和Conventional插件的全局结果 [英] Global results with Struts 2 and convention plugin

查看:71
本文介绍了Struts 2和Conventional插件的全局结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在我的应用程序中获得一些全局结果.在良好的旧XML配置中,它看起来像:

i would like to have some global results in my application. In good old XML configuration it would look like:

<global-results>
  <result name="error" type="redirectAction">
    <param name="actionName">index</param>
    <param name="namespace">/</param>
  </result>
</global-results>

但是当我使用约定插件时,XML中的全局结果似乎被忽略了,那么如何使用约定插件来实现呢?我不想让我所有的动作类都扩展一个定义了全局结果的自定义类.我认为package-info.java应该是我的朋友,但我只能定义与结果有关的就是@org.apache.struts2.convention.annotation.ResultPath.

But as I'm using the convention plugin the global results in the XML seem to be ignored so how could I implement this using the convention plugin? I don't want to have all my action classes extend a custom class that has those global results defined. I think the package-info.java should be my friend but all i can define there having something to do with results is @org.apache.struts2.convention.annotation.ResultPath.

只需说明一下:我不想避免使用struts.xml配置-我只想拥有一些有效的全局转发,因此,如果任何操作出错,我都希望将用户转发到中央错误页面.目前这不适用于我的配置.如果您在我的struts.xml或我的操作中看到了问题,并且可以帮助我解决该问题,那就很好了.

Just to make clear: I don't want to avoid struts.xml configuration - I just want to have some working global forwards so in case of an error in any action i want to forward the user to a central error page. This is currently not working with my configuration. If you see the problem in my struts.xml or my action and can help me to fix it it would be perfectly fine.

也许struts.xml中的顺序很重要?这是我的struts.xml的结构:

Maybe the order in the struts.xml matters? Here's the structure of my 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.3.dtd">
<struts>
  <constant name="struts.devMode" value="false" />
  <constant name="struts.convention.result.path" value="/content/"/>
  <constant name="struts.convention.default.parent.package" value="my-package"/>
  <constant name="struts.convention.package.locators.disable" value="true"/>
  <constant name="struts.convention.action.packages" value="..."/>
  <constant name="struts.custom.i18n.resources" value="global" />
  <constant name="struts.multipart.maxSize" value="10485760" />
  <package name="my-package" extends="struts-default,json-default" namespace="/">
    <result-types>
      <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
    </result-types>

    <interceptors>
      <interceptor name="login" class="loginInterceptor" />
      <interceptor name="pagetitle" class="pagetitleInterceptor"></interceptor>

      <interceptor-stack name="secureStack">
        ...
      </interceptor-stack>

      <interceptor-stack name="insecureStack">
        ...
      </interceptor-stack>
    </interceptors>

    <default-interceptor-ref name="secureStack" />

    <global-results>
      <result name="error" type="redirectAction">
        <param name="actionName">index</param>
        <param name="namespace">/</param>
      </result>
    </global-results>
  </package>
</struts>

在我的行动中,我有:

public class MyActionClass extends ActionSupport {
  @Actions({ @Action(value = "my-action", results = { @Result(name = "success", type = "tiles", location = "my.location") }) })
  public final String myAction() throws Exception {
    return ERROR;
  }
}

当然myAction具有更多功能-这仅用于说明.执行该动作时,它不使用图块就转发到my-action.jsp.但是我希望它转发到/index.action.

of course myAction has more functionality - this is just for illustration. When the action is executed it forwards to the my-action.jsp without using tiles but I expected it to forward to /index.action.

推荐答案

您正在中定义global-result.这些结果(全局)仅对于定义它们的包是全局的.因此,只有那些也在同一个包中声明的actions才能访问这些global-result.您面前有两个选择:

You are defining a global-result in a package. These kind of results(global) are global only to the package they are defined in. So only those actions which are also declared in the same package can access these global-results. You have two options in front of you:

很明显,如何在XML配置中做到这一点(只需在同一包中定义它们):

It's very obvious how to do it in XML configuration(you just define them in the same package):

<package name="my-package" extends="struts-default,json-default" namespace="/">

    <!-- This result is local to this action -->
    <action name="someAction"class="com.example.SomeAction" method="execute">
        <result name="someLocalResult">/localResult.jsp</result>
    </action>
    .
    .
    .
    <global-results>
        <!--This result is global **to the actions in my-package** -->
        <result name="error" type="redirectAction">
            <param name="actionName">index</param>
            <param name="namespace">/</param>
        </result>
    </global-results>

</package>


公约插件:

因此,如果您使用约定插件仅将Java类标记为actions,则不会为它们定义要驻留的包(它们将属于默认包).为此,您可以使用注释@ParentPackage来告诉框架此action属于此程序包,并且可以使用它的global-result. 为了实现这一点,您的java类应如下所示:


Convention Plugin:

So if you're using convention plugin to only mark your java classes as actions, you are not defining a package for them to rest in(They will belong to the default package). To do so, you can use the annotation @ParentPackage to tell the framework that this action belongs to this package and can use it's global-results. In order to achieve this, your java class should look like this:

@ParentPackage(value="my-pacakge")
public class MyActionClass extends ActionSupport {
    @Actions({ @Action(value = "my-action", results = { @Result(name = "success", type = "tiles", location = "my.location") }) })
    public final String myAction() throws Exception {
        return ERROR;
    }
}

您的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.3.dtd">
<struts>
    <package name="my-package" extends="struts-default,json-default" namespace="/"> 
        .
        .
        . 
        <global-results> 
             <result name="error" type="redirectAction"> 
                 <param name="actionName">index</param> 
                 <param name="namespace">/</param> 
             </result> 
         </global-results> 
     </package> 
 </struts>

摆脱设置每个动作的@ParentPackage的一种类似的替代解决方案是将动作默认pacakge设置为您喜欢的程序包(此处为包含global-result的程序包).只需将此常量元素添加到您的struts.xml中(在<struts>内部),就可以了:

A similar alternative solution to get rid of setting every action's @ParentPackage is to set the actions default pacakge to your favorite package(Here, the package containing the global-result). Just add this constant element to your struts.xml (inside of <struts>) and you'll be fine:

<constant name="struts.convention.default.parent.package" value="my-package"/>


以下是有关@ParentPackage的有用链接:


Here is a useful link about @ParentPackage:

  1. @Apache的@ParentPackage注释

这篇关于Struts 2和Conventional插件的全局结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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