如何在Spring中为html创建视图解析器? [英] How to create view resolver for html in Spring?

查看:520
本文介绍了如何在Spring中为html创建视图解析器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定在没有JSP的情况下创建一个web-app,但只使用位于WEB-INF / pages目录下的HTML页面,我遇到了这个问题。

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>

此外,我已导入WEB-INF / pages中的所有资源:

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

它适用于映射/(如果未经过身份验证,则重定向到登录页面),但由于将此页面作为静态资源导入,因此对于URL/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).

推荐答案

最后,我找到了解决方案。也许,任何人都会感兴趣。我有的主要servlet映射,有url-pattern:/ **
这就是我的问题。据我所知,主要的servlet在某种程度上拦截了viewResolver,即使它有这样的配置:< bean id =viewResolver
class =org.springframework.web.servlet.view.InternalResourceViewResolver >
< property name =prefixvalue =/ WEB-INF / pages //>
< property name =suffixvalue =。html/>
< / bean>

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>

当我将servlet make的配置作为下一个时:

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>

一切都变好了。

这篇关于如何在Spring中为html创建视图解析器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆