2

I created a simple spring-mvc web app (one controller), and simple spring jar (containing one controller).

I utilized servlet 3 spec using web fragments, what I came across was that, if I was using spring xml configuration (as per the example spring-mvc project created in STS), the controller in the jar was not being called or hit when I entered the url, but if I used java config it worked fine.

And all my java config did was import the xml and set the scanning... I'm really curious as to why this was, anyone got any ideas?

Java config:

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = { "com.webapp.test" })
@ImportResource({"classpath:webapp-context.xml"})
public class TestConfig  extends WebMvcConfigurerAdapter
{

}

webapp-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/, classpath:/META-INF/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>               

</beans:beans>

my web-fragment.xml

   <web-fragment xmlns="http://java.sun.com/xml/ns/javaee"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd"
                  version="3.0">

        <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:/META-INF/web-fragment-context.xml</param-value>
        </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    </web-fragment>

web-fragment-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory--> 
    <resources mapping="/resources/**" location="classpath:/META-INF/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.fragment.tests" />


</beans:beans>

my web.xml in the main webapp

<web-app metadata-complete="false" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- Configure DispatcherServlet to use JavaConfigWebApplicationContext 
            instead of the default XmlWebApplicationContext -->
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
         </param-value>
        </init-param>
        <!-- Again, config locations must consist of one or more comma- or space-delimited 
            and fully-qualified @Configuration classes -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>com.fragments.test.ContextConfiguration</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
user1555190
  • 2,803
  • 8
  • 47
  • 80
  • There is too little information here. Post the fragment, any `WebApplicationInitializer` you might have etc. – M. Deinum Jun 10 '15 at 14:38
  • @M.Deinum just updated with fragment and web xml – user1555190 Jun 10 '15 at 15:17
  • And the content of your `web-fragment-context.xml` and I assume that is the same path as the one used in `@ImportResource`. – M. Deinum Jun 11 '15 at 05:44
  • @M.Deinum i have updated the code above with clearer names. the ImportResource refers tot he webapp-context.xml. During scanning, the web-fragmetn is peicked up and the then the web-fragment-context.xml should be loaded. Which i know it does as i have set post constructors in the beans int he web-fragment jar, and they do get loaded. A little confused.. – user1555190 Jun 11 '15 at 08:49
  • The beans will get loaded however they aren't accessed by the `DispatcherServlet`. Controllers aren't detected in the parent config, so for the `DispatcherServlet` there are no controllers. If you now load the configuration again (that is what you are doing) in the `DispatcherServlet` they will be available. Also I highly doubt this will work as you end up with duplicate mappings for `/resources/` I suspect one will override the other, having mutliple `InternalViewResolver`s can also be problematic (especially if the paths differ). – M. Deinum Jun 11 '15 at 08:56
  • Why aren't you simply using a `WebApplicationInitializer` which scans for `@Configuration` classes and ditch the whole xml approach entirely? If you are on servlet 3 there is no requirement or need to have a web.xml or web-fragment.xml. – M. Deinum Jun 11 '15 at 08:57
  • Yes the controllers are not available in the parent config, since i have no component scan in the root xml. But what i dont understand is by just changing to java config and moving the component scan into java class rather than webapp context xml, it starts to pick up the controllers. While a complete xml configured app fails to do so... – user1555190 Jun 11 '15 at 09:40
  • @M.Deinum agree with the comment that multiple resources is not good thing, and yes they do override each other, and multiple InternalViewResolvers also bad idea, which i have removed. thanks – user1555190 Jun 11 '15 at 09:42
  • It doesn't fail they are their but the components that need one another are in different contexts and as such don't see each other. If you have `` in xml in the root and only have the controllers in the child that isn't going to work, the other way around also not (by default you can switch that). It is all a matter of which is loaded by which part of your application. – M. Deinum Jun 11 '15 at 10:17
  • @M.Deinum thanks. I think i will move to removing xml and use the WebApplicationInitializer – user1555190 Jun 11 '15 at 10:54

0 Answers0