我希望在我的应用程序中获得一些全局结果。在良好的旧 XML 配置中,它看起来像:
<global-results>
<result name="error" type="redirectAction">
<param name="actionName">index</param>
<param name="namespace">/</param>
</result>
</global-results>
但是当我使用约定插件时,XML 中的全局结果似乎被忽略,那么我如何使用约定插件来实现它呢?我不想让我的所有操作类都扩展定义了这些全局结果的自定义类。我认为 package-info.java 应该是我的 friend ,但我所能定义的与结果有关的就是 @org.apache.struts2.convention.annotation.ResultPath >.
只是为了说明:我不想避免 struts.xml
配置 - 我只想有一些工作全局转发,以便在我想要的任何操作中出现错误时将用户转发到中央错误页面。目前这不适用于我的配置。如果您在我的 struts.xml
或我的操作中看到问题并且可以帮助我修复它,那就太好了。
也许struts.xml
中的顺序很重要?这是我的 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
而不使用图 block ,但我希望它转发到 /index.action
。
最佳答案
不幸的是,您无法使用约定插件在包上定义 Result
或 Results
注释。您必须在 xml 配置中定义全局结果,并且它们不会被忽略,因为无论您使用哪个配置提供程序,都会定义运行时配置。解决方法是在基类上定义 Result
或 Results
。
关于java - 使用 Struts 2 和约定插件的全局结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19034052/