0

I am trying to figure out how i am building a restFull Web server that will:

1: Serve a single index.html file when going to /

2: all other URLs will get caught by the controllers in a restful manner for example: /invoke1 -> will reach the request mapping of "/invoke1"

The problem:

First of all spring dispatcher has some weird fallout with serving html pages (i am not talking about resources and the <mvc:resources mapping ) i am talking about serving an html page and not JSP.

i solved this issue by using the default catalina servlet like so:

<servlet>
 <servlet-name>default</servlet-name>
    <servlet-class>
      org.apache.catalina.servlets.DefaultServlet
    </servlet-class>
   <load-on-startup>2</load-on-startup>       
</servlet>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/docs/*</url-pattern>
</servlet-mapping> 

With this code i can serve HTML pages (Even behind security, as long as they are under the /docs/* path), So from here i can do:

@RequestMapping(method = RequestMethod.GET)
public String main() {
     return "redirect:/docs/index.html";
}

But that's not what i want. from here on out the URL on the browser will be displayed as /docs/

What i want to happen is that when the user goes to / he will get the index.html but when he goes to /invoke1 he will reach the spring controller.

This is something i am trying to figure out for some time now. hope you can guide me to the right solution. Thank you.

Gleeb
  • 10,773
  • 26
  • 92
  • 135

4 Answers4

0

1) The easiest solution would be to map Spring to something like /rest/ and leave / for your index.html.

2) Another solution, though you did not want it, to wrap your index.html up in a JSP and serve it on /index.html and define your controller as intended.

3) Or use <mvc:resources>. You can serve .html from a predefined static directory and simply return in your index controller "static/index.html". cf. this StackOverflow Question.

Community
  • 1
  • 1
Thomas Junk
  • 5,588
  • 2
  • 30
  • 43
0

In your configuration file you can define one mvc:view-controller to handle the redirect URL:

<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
 .....
 xsi:schemaLocation="...
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 ....>

<mvc:view-controller path="/index" view-name="/docs/index.html" />
 ....
</beans>

And you can change your main method to redirect to /index:

@RequestMapping(method = RequestMethod.GET)
public String main() {
     return "redirect:/index";
}

Now when a request is made to /, it will come to the main method, which will send a redirect request to /index. The above mapping will forward a request for /index to the static view index.html. And /docs/index.html will not be present in the browser URL.

Debojit Saikia
  • 10,532
  • 3
  • 35
  • 46
0

With Spring 3+ and its resource handling you can just do:

  • map servlet to / - all requests (excluding *.jsp) will be handled by Spring
  • configure Spring to serve your resources - (thus the index.html will be served by Spring)
  • configure index.html as your <welcome-file> in web.xml
Pavel Horal
  • 17,782
  • 3
  • 65
  • 89
0

The simple answer is you need to use forward ("forward:/") and not redirect ("redirect:/").

Adam Gent
  • 47,843
  • 23
  • 153
  • 203