0

I had sample poc which I did with jsp and spring mvc and working fine, I configured DispatcherServlet and InternalResourceViewResolver like this

<servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

and in my servlet-context.xml I configured InternalResourceViewResolver like this

<beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp"  />
</beans:bean>

My request and response are working good. Now I am trying to start a new sample project with html rather than jsp I changed InternalResourceViewResolver like this

class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <beans:property name="prefix" value="/WEB-INF/views/" />
            <beans:property name="suffix" value=".html"  />
</beans:bean>

but I am getting an exception that

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

I want to start a new sample application with html and spring mvc. can any one please suggest me in this regard.

Nithin
  • 61
  • 1
  • 9

1 Answers1

0

Solution 1: In the InternalResourceViewResolver you can leave the Suffix part empty and the InternalResourceViewResolver will resolve both .jsp as well as .html files.

But make sure that in your controller you have have methods that return html views and methods that return jsp views based on the suffix. For example, if index.html and index.jsp both exist in WEB-INF/pages you can do:

@RequestMapping("/htmlView")
   public String renderHtmlView() {
      return "index.html";
}

@RequestMapping("/jspView")
   public String renderJspView() {
      return "index.jsp";
}

Solution 2: Since .html files are static and do not require processing by a servlet then it is more efficient, and simpler, to use an 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.

Tahir Hussain Mir
  • 2,506
  • 2
  • 19
  • 26
  • 1
    It is not recommeded. Resources are generally allowed to all users specially when you use spring security resource configuration is for ANONYMOUS users. Putting views in resources will lead to OPEN get to all. – ScanQR Nov 24 '16 at 06:59
  • @TechBreak i had also that thing in my mind. But i then thought that putting simple html files in resource folder which just serve static content and has nothing to do with secure scenario can be done. But still, can you please tell me what would be the better way to solve this issue, as i had this issue revolving in my mind for a long time. – Tahir Hussain Mir Nov 24 '16 at 07:22