How set base package to scan subpackages for actio

2019-05-10 22:22发布

问题:

How can I tell to Struts 2 convention plugin to scan all subpackages of a package. I tried with this

<constant name="struts.convention.action.suffix" value="Controller" /> 
<constant name="struts.convention.package.locators.basePackage" value="fi.fpf.mvc" />

and this

<constant name="struts.convention.action.suffix" value="Controller" /> 
<constant name="struts.convention.package.locators.basePackage" value="fi.fpf.mvc.*" />

but they don't work. my actions end with "Controller" suffix. Someone knows how to do it?

here's my struts.xml:

<struts>

    <constant name="struts.convention.exclude.parentClassLoader" value="true"/>
    <constant name="struts.convention.action.fileProtocols" value="jar,vfs,vfsfile,vfszip"/>
<constant name="struts.convention.action.suffix" value="Controller" /> 
    <constant name="struts.convention.package.locators.basePackage" value="fi.fpf.mvc" />


    <package name="fpf-default" extends="struts-default">
        <result-types>
            <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
        </result-types>
    </package>
</struts>

and one action:

@Action("indexController")
public class IndexController extends ActionSupport{

    private static final long serialVersionUID = -2613425890762568273L;


    @Action(value="loadIndex", results={
            @Result(name="indexView", location = "indexView", type="tiles")
    })
    public String loadIndex() {
        return "indexView";     
    }    
}

回答1:

Try

<constant name="struts.convention.action.packages" value="fi.fpf.mvc.*"/>

if you are using convention plugin, then you should follow the class and package name conventions. Why not just name a base package to "struts" or "struts2" and with the default package locators it will be located. Also the classes should have names that matches "Action" suffix.

You can tell the Convention plugin to ignore certain packages using the property struts.convention.exclude.packages. You can also tell the plugin to use different strings to locate root packages using the property struts.convention.package.locators. Finally, you can tell the plugin to search specific root packages using the property struts.convention.action.packages.

See the docs.

Alternatively, you could set the base package and locator that match this package and any package under the base

<constant name="struts.convention.package.locators.basePackage" value="fi.fpf.mvc"/>
<constant name="struts.convention.package.locators" value="fi,fpf,mvc"/>