java 如何使用 Spring MVC 控制器返回一个 html 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13165800/
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-10-31 11:43:06  来源:igfitidea点击:

How to return an html file using Spring MVC controller

javaspringspring-mvc

提问by tribbloid

I'm working on a website using both WaveMaker and Springsource MVC.

我正在使用 WaveMaker 和 Springsource MVC 开发一个网站。

The entry generated by WaveMaker is named 'index.html'. I import all browser-side code into the /view directroy of an MVC project. And try to configure ContextLoadListener to map it into an uri. Using:

WaveMaker 生成的条目名为“index.html”。我将所有浏览器端代码导入到 MVC 项目的 /view 目录中。并尝试配置 ContextLoadListener 以将其映射到 uri。使用:

@RequestMapping(value = "/index", method = RequestMethod.GET)
public String testIndex() {

    return "index.html";
}

Then I got the following error testing it:

然后我在测试它时遇到以下错误:

SEVERE: PWC6117: File "C:\glassfish3\glassfish\domains\domain1\eclipseApps\TribblesDashboard\WEB-INF\views\index.html.jsp" not found

How do I fix it?

我如何解决它?

回答by Usha

This can be due to two reasons.

这可能是由于两个原因。

  1. The declaration of view resolver in application context. This should be some thing like this:
  1. 应用程序上下文中视图解析器的声明。这应该是这样的:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/jsp/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

This indicates that the views should be placed in the WEB-INF/jspfolders.

这表示应将视图放置在WEB-INF/jsp文件夹中。

  1. The second is to check the Dispatcher Servlet configuration in the web.xml.
  1. 二是检查.dat文件中的Dispatcher Servlet配置web.xml
<servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

Check if the mappings are OK.

检查映射是否正常。