如何使用Spring提供.html文件 [英] How to serve .html files with Spring

查看:116
本文介绍了如何使用Spring提供.html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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进行处理,因此使用

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屋!

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