Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve .html files with Spring

I am developing a website with Spring, and am trying to serve resources that are not .jsp files (.html for example)

right now i have commented out this part of my servlet configuration

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"          p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> 

And tried to return fromthe controller the full path to the resource.

@Controller public class LandingPageController {  protected static Logger logger = Logger.getLogger(LandingPageController.class);  @RequestMapping({"/","/home"}) public String showHomePage(Map<String, Object> model) {     return "/WEB-INF/jsp/index.html";       } } 

the index.html file exists in that folder.

NOTE: when i change the index.html to index.jsp my server now serves the page correctly.

Thank you.

like image 836
Gleeb Avatar asked Mar 18 '13 14:03

Gleeb


People also ask

How do I serve html in spring boot?

Just put index. html in src/main/resources/static/ folder and static html is done!

What is static folder in spring boot?

Using Spring Boot Spring Boot comes with a pre-configured implementation of ResourceHttpRequestHandler to facilitate serving static resources. By default, this handler serves static content from any of the /static, /public, /resources, and /META-INF/resources directories that are on the classpath.


1 Answers

The initial problem is that the the configuration specifies a property suffix=".jsp" so the ViewResolver implementing class will add .jsp to the end of the view name being returned from your method.

However since you commented out the InternalResourceViewResolver then, depending on the rest of your application configuration, there might not be any other ViewResolver registered. You might find that nothing is working now.

Since .html files are static and do not require processing by a servlet then it is more efficient, and simpler, to use an <mvc:resources/> mapping. This requires Spring 3.0.4+.

For example:

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

which would pass through all requests starting with /static/ to the webapp/static/ directory.

So by putting index.html in webapp/static/ and using return "static/index.html"; from your method, Spring should find the view.

like image 79
andyb Avatar answered Sep 22 '22 14:09

andyb