Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

java - Changing struts.xml file name for a multi-module Maven web project

I have a Maven project with multiple modules. Out of four modules, two of them are web modules.

Here is the structure.

MyProject
   |
   |__ api
   |
   |__ commons
   |
   |__ web_child
   |
   |__ web_main

web_main module is the main one and it can include web_child module also.

web_child module structure is

 web_child
     |
     |__ src/main/java //java action classes and all
     |__ src/main/resources
     |    |__ struts-config.xml
     |
     |__ WEB-INF
          |__ JSP Pages

web_main module structure is

 web_main
     |
     |__ src/main/java //java action classes and all
     |__ src/main/resources
     |    |__ struts.xml
     |
     |__ WEB-INF
          |__ JSP Pages

Both modules are war.

web-main depends on all the modules, and web-child depends on first two (API & commons) modules.

in web-main's pom.xml, I'm adding the dependency for web-child and it will automatically add other two jars.

The main module web-main is running perfectly on server.

But when I am trying to run the web-child module alone, it is showing error like

There is no Action mapped for namespace [/] and action name [childMenu] associated with context path ...  

Because it is not taking the struts-config.xml

When I am renaming the file to struts.xml, web-child is working fine. But that time main module is not running.

So I want to rename the struts.xml in child module to struts-config.xml and I need to specify it explicitly.

I am using Struts2. But I have tried the following in web.xml and obviously it didn't work

<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>../resources/struts-config.xml</param-value>
    </init-param>
  </servlet>

How to solve this problem?

UPDATE 1

Hi, I added following in my web-child web.xml

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>struts-config.xml</param-value>
    </init-param>
</filter>

And I am getting following exception. Do I need to add any dependency?

SEVERE: Exception starting filter struts2
Unable to load configuration. - [unknown location]
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:483)
    at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:281)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:262)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:107)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4775)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5452)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:976)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1653)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
Caused by: Unable to load configuration. - [unknown location]
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:71)
    at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:429)
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:471)
    ... 19 more
Caused by: Cannot locate the chosen ObjectFactory implementation: spring - [unknown location]
    at org.apache.struts2.config.BeanSelectionProvider.alias(BeanSelectionProvider.java:391)
    at org.apache.struts2.config.BeanSelectionProvider.alias(BeanSelectionProvider.java:362)
    at org.apache.struts2.config.BeanSelectionProvider.register(BeanSelectionProvider.java:288)
    at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:215)
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:68)
    ... 21 more
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Specify explicitly config file in the struts filter initialization parameter.

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>struts.xml,struts-config.xml,struts-default.xml,struts-plugin.xml</param-value>
    </init-param>
</filter>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...