在Spring中返回一个没有重定向的静态html

chu*_*ubs 5 java spring-mvc

所以我想在不发送重定向的情况下返回HTML页面.原因是使用重定向更改了浏览器中的URL,如果他们没有登录,我无法将某人重定向到登录.有什么最直接的方法呢?看起来它应该很简单而不使用jsp或其他服务器端视图技术.

ug_*_*ug_ 15

你可以用forward.

例:

/static/myWebpage.html是你的静态html页面

此代码将返回内容myWebpage.html而不更改URL

@Controller
@RequestMapping("/path")
public class TestingController {
    @RequestMapping("/page")
    public String someOtherPage(HttpServletRequest request, HttpServletResponse response) {
        return "forward:/static/myWebpage.html";
    }
}
Run Code Online (Sandbox Code Playgroud)

您的网址将是"localhost/path/page",但您将查看"localhost/static/myWebPage.html"