如何使用 Spring 提供 .html 文件 [英] How to serve .html files with Spring
问题描述
我正在使用 Spring 开发一个网站,并尝试提供非 .jsp 文件(例如 .html)的资源
I am developing a website with Spring, and am trying to serve resources that are not .jsp files (.html for example)
现在我已经注释掉了我的 servlet 配置的这一部分
right now i have commented out this part of my servlet configuration
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
并试图从控制器返回资源的完整路径.
And tried to return fromthe controller the full path to the resource.
@Controller
public class LandingPageController {
protected static Logger logger = Logger.getLogger(LandingPageController.class);
@RequestMapping({"/","/home"})
public String showHomePage(Map<String, Object> model) {
return "/WEB-INF/jsp/index.html";
}
}
index.html 文件存在于该文件夹中.
the index.html file exists in that folder.
注意:当我将 index.html 更改为 index.jsp 时,我的服务器现在可以正确地提供页面.
NOTE: when i change the index.html to index.jsp my server now serves the page correctly.
谢谢.
推荐答案
最初的问题是配置指定了一个属性suffix=".jsp"
所以ViewResolver实现类会添加.jsp
到从您的方法返回的视图名称的末尾.
The initial problem is that the the configuration specifies a property suffix=".jsp"
so the ViewResolver implementing class will add .jsp
to the end of the view name being returned from your method.
然而,自从您注释掉 InternalResourceViewResolver
之后,根据您的应用程序配置的其余部分,可能没有注册任何其他 ViewResolver.您可能会发现现在没有任何效果.
However since you commented out the InternalResourceViewResolver
then, depending on the rest of your application configuration, there might not be any other ViewResolver registered. You might find that nothing is working now.
由于 .html
文件是静态并且不需要由 servlet 处理,因此使用
.这需要 Spring 3.0.4+.
Since .html
files are static and do not require processing by a servlet then it is more efficient, and simpler, to use an <mvc:resources/>
mapping. This requires Spring 3.0.4+.
例如:
<mvc:resources mapping="/static/**" location="/static/" />
这将传递所有以 /static/
开头的请求到 webapp/static/
目录.
which would pass through all requests starting with /static/
to the webapp/static/
directory.
因此,通过将 index.html
放入 webapp/static/
并使用 return "static/index.html";
从您的方法中,Spring 应该找到视图.
So by putting index.html
in webapp/static/
and using return "static/index.html";
from your method, Spring should find the view.
这篇关于如何使用 Spring 提供 .html 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!