Java 从控制器转发到静态 html 页面

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

Forward to a static html page from Controller

javarestspring-mvc

提问by Alexey Buistov

My spring mvc application has one single ContentNegotiatingViewResolver that defines JsonView for rendering json resonses:

我的 spring mvc 应用程序有一个 ContentNegotiatingViewResolver,它定义了用于呈现 json 响应的 JsonView:

<mvc:annotation-driven/>

<context:component-scan base-package="world.domination.test"/>

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json"/>
        </map>
    </property>
    <property name="defaultViews">
        <list>
            <bean class="com.secondmarket.connector.springmvc.MappingHymansonJsonViewEx"/>
        </list>
    </property>
</bean>

The whole application sits on root url "myapp". Everything works as I need.

整个应用程序位于根网址“myapp”上。一切都按我的需要工作。

The first questionis: how to return a static html page when accessing a certain url? Say, when accessing Spring uri /myapp/test I would like to render an html page /TestStuff.html that resides in root webapp folder.

一个问题是:访问某个url时如何返回一个静态的html页面?说,在访问 Spring uri /myapp/test 时,我想呈现一个位于根 webapp 文件夹中的 html 页面 /TestStuff.html。

I went ahead and wrote a simple controller:

我继续写了一个简单的控制器:

@Controller
@RequestMapping("test")
public class TestConnector {

    @Autowired
    private RestTemplate tpl;

    @RequestMapping(method = RequestMethod.GET)
    public String get() {
        return "/TestStuff.html";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String post(@RequestParam("url") String url, @RequestParam("data") String data) {
        return tpl.postForObject(url, data, String.class, new HashMap<String, Object>());
    }
}

The get() method is supposed to tell Spring to render a TestStuff.html, but instead I get an error saying that the view with name "/TestStuff.html" is missing.

get() 方法应该告诉 Spring 呈现一个 TestStuff.html,但我收到一个错误,说缺少名称为“/TestStuff.html”的视图。

The second questionis how to avoid the necessity to put extension to the URL. In my example, when I use /myapp/testinstead of /myapp/test.htmlmy ContentNegotiatingViewResolver uses a json view that renders {} (empty curly braces)

第二个问题是如何避免的必要性,把扩展到的URL。在我的示例中,当我使用/myapp/test而不是/myapp/test.html我的 ContentNegotiatingViewResolver 使用呈现 {}(空花括号)的 json 视图

Any pointers are highly appreciated.

任何指针都受到高度赞赏。

回答by DwB

Instead of returning "/TestStuff.html" from your controller, try returning "redirect:/TestStuff.html".

不要从控制器返回“/TestStuff.html”,而是尝试返回“redirect:/TestStuff.html”。

Another option is to create and register a view resolver for your static pages. Perhaps something like this:

另一种选择是为您的静态页面创建和注册视图解析器。也许是这样的:

<bean id="staticViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="prefix" value="/WEB-INF/static/"/>
    <property name="suffix" value=".html"/>
</bean>

回答by John Mikic

The best way to do this is to use InternalResourceViewResolver combined with mvc:view-controller tag (see appropriate Spring Reference Manual for details). Just include the following into your application context XML file (in this case your static file TestStuff.html will be located in /WEB-INF/static-pages directory):

最好的方法是将 InternalResourceViewResolver 与 mvc:view-controller 标签结合使用(有关详细信息,请参阅相应的 Spring 参考手册)。只需将以下内容包含到您的应用程序上下文 XML 文件中(在这种情况下,您的静态文件 TestStuff.html 将位于 /WEB-INF/static-pages 目录中):

<bean>
    <bean id="staticPagesViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/static-pages/"/>
    <property name="suffix" value=".html"/>
</bean>

<mvc:view-controller path="/test" view-name="TestStuff"/>

回答by Bruce Edge

tutorialspoint has a good complete example of this: http://www.tutorialspoint.com/spring/spring_static_pages_example.htm

tutorialspoint 有一个很好的完整示例:http: //www.tutorialspoint.com/spring/spring_static_pages_example.htm