How to create view resolver for html in Spring?

2019-04-14 18:09发布

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).

2条回答
We Are One
2楼-- · 2019-04-14 19:00

i dont know why you want to do that ..... as putting pages under web-inf is a wrong practice.......

also the container cant access the static contents that are under web-inf folder. I have faced exactly same problem see resource problem post.

what i have found from googling is you can access dynamic resources under web-inf folder but not the static. I have tried even regestring all the static contents (ie. css, js,html, etc) in xml at the first place but nothing worked. finally i moved my pages out and it worked with no configuration ....

so try moving the resources out of web-inf and under webcontent

tell me if you got some extras on this.

thanks.

查看更多
Fickle 薄情
3楼-- · 2019-04-14 19:05

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.

查看更多
登录 后发表回答