Nikita Vlasenko
Nikita Vlasenko

Reputation: 4352

Spring, Serving static views from controllers

I am getting WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/fomoapp/resources/lib/login.html] in DispatcherServlet with name 'fomo' error. The following thread solutions are not working for me:

org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI Spring 3

What I see is that the application finds the right controller, then tries to find the .html file that I am returning from the method, but somehow fails to do this. Basically, I am trying to serve static resources from resources/lib/ folder. The directory structure is the following:

enter image description here

web.xml:

enter image description here

WEB-INF/applicationContext:

enter image description here

WEB-INF/spring/fomo-config.xml:

enter image description here

and the controller looks like the following:

enter image description here

Upvotes: 0

Views: 750

Answers (2)

Nikita Vlasenko
Nikita Vlasenko

Reputation: 4352

Basically, what I was trying to do is to start serving .html files as views, and it was not working due to the specified extension - .html. After changing it to .jsp, things start working good. So, I guess InternalResourceViewResolver needs .jsps.

This was the main change needed (besides renaming the file to .jsp), in fomo-config.xml:

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />  

</bean>

As can be seen I have also separated static resources from views the way Martin Frey recommended. Maybe this was important too.

applicationContext.xml was changed to the following:

<!-- Root Context: defines shared resources visible to all other web components -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:default-servlet-handler/>

Then, even though the login page loaded, static files inside of it were not, so I needed to do 2 things:

  1. create another resources folder inside the webapp folder and move there all of my resources

  2. Specify the mapping for files extensions inside of my web.xml the following way:

    <servlet-mapping>
      <servlet-name>default</servlet-name>
      <url-pattern>*.css</url-pattern>
    </servlet-mapping>
    

The directory structure looks like this now:

enter image description here

It is working fine now, but I am still not sure why at all we need main/resources folder if I can not access it other than by specifying the absolute path the way it is explained here:

http://akshaybobde.blogspot.com/2014/07/spring-mvc-serving-images-from-physical.html

I can not delete or move around it: the project just won't compile.

Upvotes: 0

Martin Frey
Martin Frey

Reputation: 10075

Your mvc:resources maps all requests to resources/lib. You should only map /resources/**, else your controllers will never be called.

Mvc resources "is" one controller specifically for serving static resources to the client (js/css/...), so you dont need your own.

Don't mix these resources with templates to be rendered by your controllers. Put them into another location, else they will be also available to the clients in their raw form.

Upvotes: 1

Related Questions