0

I am writing spring mvc application.

I had asked this question should I make two different servlet entry for rest and normal html in web.xml, and it got resolved by the answers given by knowledgeable people present over stackoverflow (Answer: should I make two different servlet entry for rest and normal html in web.xml)

Now my web.xml include below code

<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring_myapp-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>myapp</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

But after making changes to web.xml as mentioned in the answer, I am getting error No mapping found for HTTP request with URI [/myapp/img/logout.png].

I am not able to load any of the css, js as well as images.

I searched for this issue and got the answer https://stackoverflow.com/a/17946825/3898076. If you find this question duplicate then please share the solution for it.

I am not able to figure out my mistake. Can you please help me out with this issue.

Note: I am including files as <script type="text/javascript" src="js/jquery.js"></script>.

Thanks.

Community
  • 1
  • 1
Naman Gala
  • 4,670
  • 1
  • 21
  • 55

3 Answers3

0

Maybe you are looking for something like this

<servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>0</param-value>
    </init-param>
    <init-param>
        <param-name>listings</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/css/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/images/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/js/*</url-pattern>
</servlet-mapping>
user
  • 735
  • 1
  • 6
  • 21
  • Thanks for replying, but it did not do my work. As I am using `@RequestMapping("/htmlUrl")`, that's why I have kept /. And I guess DispatcherServlet should bypass js, css and images, but it is not doing in my case. – Naman Gala Oct 15 '14 at 11:44
0

I have found the answer to my question. https://stackoverflow.com/a/4556267/3898076

For reference, code snippet mentioned in the answer to be included in servlet-context.xml for Spring 3.0.4+ ONLY.

<!-- resources exclusions from servlet mapping -->
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/js/**" location="/js/" />
Community
  • 1
  • 1
Naman Gala
  • 4,670
  • 1
  • 21
  • 55
0

Using this fixes the issue for me:

<!-- resources exclusions from servlet mapping --> 
<mvc:resources mapping="/css/**" location="/css/" /> 
<mvc:resources mapping="/images/**" location="/images/" /> 
<mvc:resources mapping="/js/**" location="/js/" /> 
Zoe
  • 27,060
  • 21
  • 118
  • 148