简体   繁体   中英

Html integration with Spring MVC

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.

In the you can leave the part and the will resolve both .jsp as well as .html files. ,可以将部分保留为 ,而将同时解析.jsp和.html文件。

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";
}

: Since .html files are static and do not require processing by a servlet then it is more efficient, and simpler, to use an mapping. :由于.html文件是静态的,不需要servlet进行处理,因此使用映射更加有效和简单。 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM