Struts 2 Convention Plugin 定义多个父包

时间:2023-04-05
本文介绍了Struts 2 Convention Plugin 定义多个父包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在开发 Struts 2 Web 应用程序,使用 Convention Plugin 通过注释来配置所有内容.我的一些动作类遇到了障碍,我想在其中使用多个父包.我确实在 Apache 的网站 上找到了一种方法 - 但是这似乎已被弃用.它不起作用,抛出 Type mismatch: cannot convert from String[] to String 错误.

I'm working on Struts 2 web application, using the Convention Plugin to configure everything via annotations. I'm hitting a snag with some of my action classes, where I'd like to use multiple parent packages. I did find a means of doing it on Apache's site - but that appears to be deprecated. It does not work, throwing back a Type mismatch: cannot convert from String[] to String error.

@ParentPackage({"my-parent-package1, my-parent-package2"})
package com.mycompany.myapp.actions

import org.apache.struts2.config.ParentPackage;

我正在使用 Struts 2.2.1.是否可以通过注释授予多个父包的操作?他们会将此作为一项功能删除,这似乎很奇怪.

I am using Struts 2.2.1. Is it possible to grant an action multiple parent packages via annotations? Seems odd that they would have removed this as a feature.

推荐答案

你不能用注解来做到这一点,但它仍然可以通过 XML 配置来实现.javadoc 说

You cannot do it with annotations, but it's still possible via XML config. The javadoc says

这个注解允许actions修改他们的父包正在使用.由于 XWork 包是由 Convention 插件创建的通过动作存在的 Java 包,有一些棘手的问题处理 XWork 父包和 XWork 包的命名空间用于发现的动作,以便同一个包中的两个动作可以指定不同的父级和命名空间而不会发生冲突.

This annotation allows actions to modify the parent package that they are using. Since XWork packages are created by the Convention plugin via the Java packages that the actions exist in, there is some tricky handling of XWork parent packages and namespaces of the XWork packages for discovered actions so that two actions in the same package can specify different parents and namespaces without collision.

为了正确处理这个问题,XWork 包的名称使用以下格式构建操作:

In order to handle this correctly, the name of the XWork package that actions are placed into is built using this format:

<java-package>##

<java-package>#<parent-xwork-package>#<namespace>

这意味着java包中可能有多个父包.但是您不能在同一个类或包上放置两个或多个 @ParentPackage 注释.正如您注意到的那样,父包不能有数组.但是您至少可以有两个父包,一个在包定义中,另一个在动作类中.不幸的是,配置构建器为此注解构建了两个配置,但操作配置仅针对具有注解的配置创建.

This means that is possible to have multiple parent packages in the java package. But you cannot place two or more @ParentPackage annotations on the same class or package. And as you notices cannot have array for parent packages. But you can at least have two parent packages one at the package definition and other at the action class. And unfortunate the config builder builds two configs for this annotations but the action config is only created for the one that has an annotation.

这篇关于Struts 2 Convention Plugin 定义多个父包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:java注释中的私有字段 下一篇:外行术语中的 Spring 传播示例

相关文章