chubbsondubs chubbsondubs - 2 months ago 16
Java Question

Return a static html without redirect in Spring

So I'd like to return an HTML page without sending a redirect. The reason being is using a redirect changes the URL in the browser, and I can't redirect someone to the login if they aren't logged in. What's the most straight forward way to do this? Seems like it should be simple without using jsp or other server side view technologies.

ug_ ug_
Answer

You can use forward.

Example:

say /static/myWebpage.html is your static html page

This code will return the content of myWebpage.html without changing the url

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

Again your url would be "localhost/path/page" but you will be viewing "localhost/static/myWebPage.html"