0

I have a spring application which runs on Tomcat server. Now my requirement is that all the requests should redirect to app-servlet.xml.

I have configured it as follows:

<servlet>    <servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param><param-name>listings</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>/</url-pattern> </servlet-mapping>

The application is getting started, but the pages are not properly loaded. Could anyone please help with this?

Jamal
  • 763
  • 7
  • 22
  • 32
vibin
  • 51
  • 1
  • 5
  • Does this question: ["Servlet mapping / vs /*"](http://stackoverflow.com/questions/4140448/servlet-mapping-vs) answer your question? – Kong May 20 '14 at 02:47
  • @kong.. Yes please.. In our application it is configured as *.htm in the servlet mapping so that only the request whcih ends with .htm will be forwared to app-servlet.Now we need to forward all the requests apart from *.htm.For that purpose i have given url pattern as /.. But now application is not loading properly. i mean functioanlity is working but ui is inconsistent – vibin May 20 '14 at 04:14

1 Answers1

0

Doing so, All the requests for static contents (Images and css files) are also being forwarded to your DispatcherServlet.

From Spring 3.0.x, There is a mechanism to serve static resources through DispatcherServlet, what you need is to add the following line in your app-servlet.xml.

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

Then you can use the image or css of your resource folder using url like below in jsp pages:

  <c:url value="/resources/image/someImage.jpg" />

For details:

Edit:

But for lower versions of Spring there is no shortcut or simple solution to do that. More convenient way is to use specific url pattern for your DispatcherServlet, so that the public static resources can be accessed directly and have no mapping.

Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52
  • Thanks for the response. We are using Spring 2.5 in our application – vibin May 20 '14 at 03:48
  • @Rahaman May i know how to map invalid URL pattern..My Requirement is "For an invalid request i should get 404 error page" But in my application we have pre compiled jsps and i used 404 /WEB-INF/classes/org/pache/jsp/jsp/error/error_jsp which is not working for an invalid request. – vibin May 20 '14 at 04:26
  • What is the problem exactly you are facing? – Sazzadur Rahaman May 20 '14 at 08:27
  • @Rahman our app is configured as app *.htm so that for all the requests ends with *.htm will be forwared to app-servlet.xml. But if we change the url in the browser something like this - abc.def we should see custom 404 error page.Unable to configure the same way as mentioned above – vibin May 20 '14 at 13:45