1

I am doing project on Spring MVC with html file as viewResolver. Already my project done in ftl (freemarker template) files as viewResolver perfectly. But I want to do conversion of all ftl files into html files. Hence I have tried hard for two days and do some configuration changes in view resolver like below, but nothing worked.

My original code in applicationContext.xml

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="cache" value="true" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".ftl" />
    <property name="exposeSpringMacroHelpers" value="true" />
    <property name="exposeRequestAttributes" value="true" />
    <property name="allowRequestOverride" value="false" />
    <property name="exposeSessionAttributes" value="true" />
    <property name="allowSessionOverride" value="false" />
    <property name="exposePathVariables" value="true" />
    <property name="requestContextAttribute" value="rc" />
</bean>

I have made some changes like below

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

My DispatcherServlet configuration

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></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>

My html files are in "/WEB-INF/views/" folder. What changes I need to do to run the html files. Any alternate way also warmly welcome. Thanks in advance guys...

Srini
  • 119
  • 3
  • 13

2 Answers2

1

HTML pages meant for implementing static views, we have great feature in spring to render the static resources like html, css and images i.e.

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

Similar question asked here SpringViewResolver for HTML views

Community
  • 1
  • 1
Lovababu Padala
  • 2,415
  • 2
  • 20
  • 28
0

================================ SOLUTION ========================================

Newly created Folder structure:

 WEB-INF
   `-static
          |-html
                `-index.html

Spring config:

<resources mapping="/static/**" location="/WEB-INF/static/" />

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

My web.xml

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></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>

Controller method:

@RequestMapping(value = "/", method = RequestMethod.GET)
  public String homePage(ModelMap model, HttpServletRequest servletRequest, HttpServletResponse response) {
    String returnText = "static/html/index";
    return returnText;
}

I have searched and implemented various ways. Its now working like a boss. Below link helped me and stiched my time a lot. Also need to work various html files with this solution.

Spring MVC ViewResolver not mapping to HTML files

Community
  • 1
  • 1
Srini
  • 119
  • 3
  • 13