Java 如何在 Spring 中为 html 创建视图解析器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21039711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:09:14  来源:igfitidea点击:

How to create view resolver for html in Spring?

javaspringspring-mvcmodel-view-controllerspring-security

提问by Viktoriia

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.

当我决定创建一个没有 JSP 的 web 应用程序,但只使用目录 WEB-INF/pages 下的 HTML 页面时,我遇到了这个问题。

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:

我还导入了 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).

它适用于映射“/”(如果未通过身份验证,则重定向到登录页面),但由于将此页面作为静态资源导入,因此它不适用于 url“/index.html”(但如果没有,它将根本无法工作导入)。

采纳答案by Viktoriia

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 映射具有 url-pattern: /** 这就是我的问题。正如我所理解的,主 servlet 以某种方式拦截了 viewResolver,即使它有这样的配置:<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 make 配置为下一个时:

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

everything became ok.

一切都好起来了。

回答by Mayur Gupta

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

我不知道你为什么要这样做......因为将页面放在 web-inf 下是一种错误的做法......

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

容器也无法访问 web-inf 文件夹下的静态内容。我遇到了完全相同的问题,请参阅资源问题帖子

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

我从谷歌搜索中发现,您可以访问 web-inf 文件夹下的动态资源,但不能访问静态资源。我什至尝试过将 xml 中的所有静态内容(即 css、js、html 等)重新字符串化,但没有任何效果。最后我把我的页面移了出来,它在没有配置的情况下工作......

so try moving the resources out of web-infand under webcontent

所以尝试将资源从web-infwebcontent下移出

tell me if you got some extras on this.

告诉我你是否对此有所了解。

thanks.

谢谢。