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.
Just put index. html in src/main/resources/static/ folder and static html is done!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With