How to create view resolver for html in Spring?

12,719

Finally, I found the solution. Maybe, it will be interesting to anybody. The main servlet mapping that I had, had url-pattern: /** And that was my problem. As I understood the main servlet in someway intercepted viewResolver even if it had such configuration: <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".html"/> </bean>

When I make the configuration of servlet make as the next one:

  <servlet-mapping>
    <servlet-name>main</servlet-name>
    <url-pattern>/app/*</url-pattern>
</servlet-mapping>

everything became ok.

Share:
12,719
Viktoriia
Author by

Viktoriia

Updated on June 04, 2022

Comments

  • Viktoriia
    Viktoriia almost 2 years

    I have faced the problem when decided to create a web-app without JSPs, but using only HTML-pages which are under directory WEB-INF/pages.

    I've made the view resolver:

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

    Also I've imported all the resources in WEB-INF/pages:

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

    My controller have the following view:

    @PreAuthorize("isAuthenticated()")
    @RequestMapping("/")
    public String indexPage() {
        return "redirect:/index.html";
    }
    

    It works fine for mapping "/" (redirects to login page if not authenticated), but it is not secured for url "/index.html" due to importing of this page as static resource (but it will not work at all if not import it).