0

I have an issue with mvc:resources

My main-servlet.xml

    **
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="50000000" />
    </bean>

    <mvc:resources mapping="/images/**" location="/images/" />
    <mvc:resources mapping="/css/**" location="/css/" />
    **

When I comment the mvc:resources lines everything (else) starts working magically. But when I uncomment the lines, ONLY the resources work. While compiling, log says that ONLY css/** and images/** were mapped and that there is no other mapping.

Web.xml File :

<servlet-mapping>
    <servlet-name>main</servlet-name>
    <url-pattern>/css/**</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>main</servlet-name>
    <url-pattern>/images/**</url-pattern>
</servlet-mapping>
     <servlet-mapping>
    <servlet-name>main</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

I tried putting 2 dispatcher servlets but that doesn't working either. The other servlet gets read and does get mapped but the request defaults to the main servlet.

Compilation log AFTER commenting /css/** and /images/** from web.xml :

    **
    org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
    INFO: Mapped URL path [/images/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
    Nov 26, 2013 11:40:40 AM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
    INFO: Mapped URL path [/css/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#1'
    Nov 26, 2013 11:40:40 AM org.springframework.web.servlet.FrameworkServlet initServletBean
    INFO: FrameworkServlet 'resources': initialization completed in 220 ms
    Nov 26, 2013 11:40:40 AM org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring FrameworkServlet 'main'
    Nov 26, 2013 11:40:40 AM org.springframework.web.servlet.FrameworkServlet initServletBean
   **
    INFO: FrameworkServlet 'main': initialization completed in 234 ms
    Nov 26, 2013 11:40:40 AM org.apache.coyote.http11.Http11Protocol init
    INFO: Initializing Coyote HTTP/1.1 on http-8080
    Nov 26, 2013 11:40:40 AM org.apache.coyote.http11.Http11Protocol start
    INFO: Starting Coyote HTTP/1.1 on http-8080
    Nov 26, 2013 11:40:53 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
    WARNING: No mapping found for HTTP request with URI [/KT/] in DispatcherServlet with name 'main'
4
  • Just tried it. Doesn't work :( . Putting up the compilation log now Commented Nov 26, 2013 at 16:41
  • Note that you shouldn't call it compilation. It's not compilation. It's startup/bootstrap logs. Commented Nov 26, 2013 at 16:41
  • What is the everything (else) that starts working? Commented Nov 26, 2013 at 16:45
  • basically the other mappings in the web.xml file. When I try to access myapp/add with mvc:resources commented I can access it whereas i cannot access it when it's uncommented. Apache give me a 404 error Commented Nov 26, 2013 at 16:50

2 Answers 2

3

Note that none of your controllers will be registered with your current setup. You need to add

<mvc:annotation-driven />

to your context so that Spring registers them with the DispatcherServlet.

Your main DispatcherServlet should also simply be mapped to

<servlet-mapping>
    <servlet-name>main</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

The others are redundant.

Sign up to request clarification or add additional context in comments.

3 Comments

I believe you're right. I added that line and tried to compile. I get this error in the startup logs. "Line 19 in XML document from ServletContext resource [/WEB-INF/main-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'context:component-scan'. One of '{"springframework.org/schema/mvc":message-converters, "springframework.org/schema/mvc":argument-resolvers, ..., is expected." I'm a newbie to Spring so thanks for bearing with me
I moved mvc:anootation-driven one line down (below the context:component scan). Now I get the same error that the "bean" (next line after mvc-annotation driven) has an issue
I suppose it needed an end tag </mvc:annotation-driven>. It removed that error but got me another error. I'll post the log
0

The question has already been answered but I ran into a bunch of issues while solving it so I wanna leave a few resolution steps just in case someone comes looking with similar problems :

1st and initial issue : mvc:resources what misbehaving (details in question) Solution : Solved exactly as the accepted answer suggested. (

2nd Issue (see 1st stack trace) : Occurs when you don't have a closing tag for ) Solution : add " / " at the end or add right after (also the accepted answer has it correct)

3rd Issue (2nd (last) stack trace) : Occurrs when you don't have the following dependency :

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.3.1.Final</version>
        </dependency>

Also. If you're using Tika & Solrj togather, select the sl4j-api with the higher version. Selecting the lower version causes a whole bunch of other problems (sorry no stack trace).

My versions : Solr 3.6.2, Solrj 4.4.0, Tika 1.4.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.