Search code examples
javajspstruts2annotationstiles

Annotation based configuration alternative for tag result-type of struts.xml


I'm trying to create new Struts 2 application (annotation based configuration) with common header, footer and body where body change on menu click.

So I referred to this link which says If you are using annotations alone and no struts.xml, then you have to create a minimal one for this because there's no annotation available to define a custom result type.

Do i still have to create struts.xml for result-type tag?

I am trying to use Tiles 3 and getting this error:

Jun 17, 2017 3:26:20 PM org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter struts2
Unable to load configuration. - [unknown location]
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:499)
    at org.apache.struts2.dispatcher.InitOperations.initDispatcher(InitOperations.java:75)
    at org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:63)
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4590)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5233)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: Unable to load configuration. - [unknown location]
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:70)
    at org.apache.struts2.dispatcher.Dispatcher.getContainer(Dispatcher.java:906)
    at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:445)
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:486)
    ... 14 more
Caused by: The Result type [tiles] which is defined in the Result annotation on the class [class org.iibf.crf.action.LoginAction] or determined by the file extension or is the default result type for the PackageConfig of the action, could not be found as a result-type defined for the Struts/XWork package [org.iibf.crf.action#convention-default#/] - [unknown location]
    at org.apache.struts2.convention.DefaultResultMapBuilder.createResultConfig(DefaultResultMapBuilder.java:443)
    at org.apache.struts2.convention.DefaultResultMapBuilder.createFromAnnotations(DefaultResultMapBuilder.java:414)
    at org.apache.struts2.convention.DefaultResultMapBuilder.createResultsFromAnnotations(DefaultResultMapBuilder.java:222)
    at org.apache.struts2.convention.DefaultResultMapBuilder.build(DefaultResultMapBuilder.java:192)
    at org.apache.struts2.convention.PackageBasedActionConfigBuilder.createActionConfig(PackageBasedActionConfigBuilder.java:948)
    at org.apache.struts2.convention.PackageBasedActionConfigBuilder.buildConfiguration(PackageBasedActionConfigBuilder.java:700)
    at org.apache.struts2.convention.PackageBasedActionConfigBuilder.buildActionConfigs(PackageBasedActionConfigBuilder.java:336)
    at org.apache.struts2.convention.ClasspathPackageProvider.loadPackages(ClasspathPackageProvider.java:53)
    at com.

Solution

  • The default parent package should be not convention-default but tiles-default. You can distinguish this if you add a tiles plugin to the classpath and set the @ParentPackage("tiles-default") annotation to the action class. You can put this annotation on the class or package where it's applied by the retention policy.

    Also note, that you can define the parent package globally, not only the custom action class from the previous example. I.e., if you have a package default in your configuration that extends tiles-default and you want to configure it globally use

    <constant name="struts.convention.default.parent.package" value="default"/>