Struts 2 Convention Plugin Define Multiple Parent Packages
Asked Answered
K

2

1

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;

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.

Kraal answered 8/1, 2013 at 2:22 Comment(0)
B
2

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

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.

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>

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.

Bushranger answered 8/1, 2013 at 11:17 Comment(1)
Long story short, what roman said is correct... but of course where there is a will there is a way, however this would be a bit of work. Conventions did not integrate well with tiles (That is: find tiles views if they existed in the same way as with jsps/vm/fmt) it is possible to extend/build upon conventions. I needed a custom ResultProvider if you look in the plugins (struts-convention-plugin) xml file you'll see many definitions for different beans (extension points), including a configuration provider. I would start there. This way you can provide you own implementation.Indigotin
C
1

I checked org.apache.struts2.convention.annotation.ParentPackage for you, it is not allowed to define multi-parent-package. here is the snippet:

@Target({ElementType.TYPE, ElementType.PACKAGE})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface ParentPackage {
    /**
     * @return  The parent package.
     */
    String value();
}
Cookbook answered 8/1, 2013 at 8:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.