0

我想保留网址“ http://www.mywebsite.com/hello/1111 ”,但我想显示另一个外部页面,假设我想显示 google.com。

这是我的控制器的代码:

@RequestMapping(value = {"hello/{id}"}, method = RequestMethod.GET)
public String helloGet(Model model, @PathVariable Integer id) {

    final String url = "http://www.google.com"
    return url;

}

如何在地址栏中保留“ http://www.mywebsite.com/hello/1111 ”,并显示“www.google.com”?

4

2 回答 2

1

一种方法是在您为用户生成的视图中使用iframe 。

@RequestMapping(value = {"hello/{id}"}, method = RequestMethod.GET)
public String helloGet(Model model, @PathVariable Integer id) {

    final String url = "http://www.google.com"
    model.addAttribute("externalUrl", url);

    return "hello";
}

然后在 hello.jsp 文件中可能如下所示:

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <iframe src="${externalUrl}"></iframe>
    </body>
</html>

另一种方法是向外部网站发出请求并将响应流式传输给用户。

于 2013-09-03T17:05:15.523 回答
1

您将无法通过视图转发来做到这一点。您必须使用 HTTP 客户端从目标 url 获取响应并将该内容写入当前请求的响应正文。

@RequestMapping(value = {"hello/{id}"}, method = RequestMethod.GET)
public void helloGet(Model model, @PathVariable Integer id, HttpServletResponse response) {

    final String url = "http://www.google.com"
    // use url to get response with an HTTP client
    String responseBody = ... // get url response body 
    response.getWriter().write(responseBody);
}

或与@ResponseBody

@RequestMapping(value = {"hello/{id}"}, method = RequestMethod.GET)
public @ResponseBody String helloGet(Model model, @PathVariable Integer id) {

    final String url = "http://www.google.com"
    // use url to get response with an HTTP client
    String responseBody = ... // get url response body 
    return responseBody;
}
于 2013-09-03T17:05:23.987 回答