Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make .html default view spring mvc

Tags:

spring-mvc

Have a sample app and created a

view/HelloWorld.html

page. From my controller, I return the following

public String home(Locale locale, Model model) {
    return "HelloWorld";
}

In debug mode I get this warning/error:

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/HelloWorld/WEB-INF/views/HelloWorld.html] in DispatcherServlet with name 'appServlet'

contents of my src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".html" />
</beans:bean>

If I rename the .html to .jsp and change above to .jsp then things work fine.

like image 228
user1361914 Avatar asked Feb 04 '26 10:02

user1361914


1 Answers

The flow that the servlet container goes through for this request is the following:

  1. First the DispatcherServlet is invoked by the Servlet Container.
  2. The DispatcherServlet finds a mapping which maps to the home method of your Controller and the home method returns a view name "HelloWorld"
  3. Now the DispatcherServlet uses a View Resolver (your InternalResourceViewResolver) to find the View to render the model through, since the name is "HelloWorld", this maps to the /WEB-INF/view/HelloWorld.html view.
  4. Now essentially a call is made to RequestDispatcher.forward("/WEB-INF/views/HelloWorld.html",....
  5. The Servlet container at this point tries to find the servlet which can handle /WEB-INF/views/HellowWorld.html uri - if it had been a .jsp there is a JSPServlet registered which can handle rendering the jsp, however for *.html there is no servlet registered, so the call ends up with the "default servlet", which is registered with a servlet-mapping of / which probably your DispatcherServlet is.
  6. Now the Dispatcher servlet does not find a controller to handle request for /WEB-INF/views/HelloWorld.html and hence the message that you are seeing

If you want this kind of a extension to be handled by the servlet container, say tomcat, you can register *.html extension to be handled by JSPServlet and then it should work cleanly. Or return forward:/resources/HelloWorld.html which will be considered a static file relative to your resources folder.

like image 117
Biju Kunjummen Avatar answered Feb 09 '26 14:02

Biju Kunjummen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!