1

I have the following controller:

@Controller
public class MyErrorController implements ErrorController {

    @RequestMapping("/error")
    public String handleError(HttpServletRequest request, Model model) {
        model.addAttribute("request", request);
        return "error";
    }

}

And the following template:

URL: <span th:text="${#request.getRequestURL()}">url</span><br/>

If I go to the url http://localhost:8080/this-is-a-404-url, here is what the template shows:

URL: http://localhost:8080/error

However, this is not the correct url (it's only the requestMapping). Is there a way to get the client url from the request object in the template?

Note, if I had to do this in the controller, I would do: request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);. Is there a way to do that in thymeleaf?

David542
  • 104,438
  • 178
  • 489
  • 842
  • Maybe [this](https://stackoverflow.com/questions/1490821/whats-the-best-way-to-get-the-current-url-in-spring-mvc) post will solve your problem: – Dennis Jul 26 '18 at 22:22
  • @Dennis no the issue here is that it's getting "error" instead of "error12232321". "error" is the specified url of the RequestMapping. Does that make sense? – David542 Jul 26 '18 at 22:33
  • @David542 no it does not. Your template seems to indicate that you want to have the `url` and the `method`? In any case both are easily attainable from the request. – akortex Jul 26 '18 at 22:35
  • @Aris please see updated question. I've tried to clarify. – David542 Jul 26 '18 at 22:37

1 Answers1

1

You can do the above by getting the forwarded request uri:

URL: <span th:text="${#request.getAttribute('javax.servlet.forward.request_uri')}">url</span><br/>

This will return your "/this-is-a-404-url"

David542
  • 104,438
  • 178
  • 489
  • 842