Struts 2 约定插件定义多个父包 [英] Struts 2 Convention Plugin Define Multiple Parent Packages

查看:23
本文介绍了Struts 2 约定插件定义多个父包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Struts 2 Web 应用程序,使用约定插件通过注释配置所有内容.我的一些操作类遇到了障碍,我想在其中使用多个父包.我确实在 Apache 的网站 上找到了一种方法 - 但是这似乎已被弃用.它不起作用,抛出类型不匹配:无法从 String[] 转换为 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

这个注解允许操作修改它们的父包正在使用.由于 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>#<parent-xwork-package>#<namespace>

<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 约定插件定义多个父包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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