How to get hostName in Freemarker?

There is a project for SpringMVC. As a templating engine is Freemarker. On one of the pages you want to insert the full link, that is, with a domain.

In jsp it can be solved by inserting ${request.getServerName()}. In freemarker the same code is not working.

You can still calculate the host name in the controller and pass it in the view, but it violates the principle of MVC.

How to know hostname in the freemarker template?
October 8th 19 at 01:12
1 answer
October 8th 19 at 01:14
Here it says that if the templating engine is run using freemarker.ext.servlet.FreemarkerServlet, then the entire query in the template are three objects — Request, Session, Application. There are also hash RequestParameters, which contains all request parameters.

As far as I know, may be wrong, but in the Spring context in Freemarker templates is an object url. Then the server name can be retrieved by:
It seems that the url involved here Spring Surf.
You should try to get the necessary parameters from the RequestParameters or Request. - Josie_Little commented on October 8th 19 at 01:17
Yes, most likely they are contained in the Request. Because RequestParameters is the GET request parameters. How to get from the Request is unclear. The idea is that this is the HttpServletRequest object, but Request.getContextPath() gives the following error:
Request processing failed; nested exception is freemarker.core.InvalidReferenceException: Expression Request.getContextPath is undefined
What's wrong here? - kamron.Macejkovic93 commented on October 8th 19 at 01:20
not so here that in freemarker fails to execute java code, only to pull properties. Therefore, it is impossible to take contextPath and get the address.
Or is there a way to pull directly from class methods as freemarker and jsp? - kamron.Macejkovic93 commented on October 8th 19 at 01:23
Generally forums are filled with code in the style of ${Request.getServerName()} or ${Request.setAttribute("test", "test")}but I failed to do such.
The second option, which somehow, again, someone works is to write ${Request.serverName}. It is argued that method will be called getServerName(). Also did not work. - Josie_Little commented on October 8th 19 at 01:26

Find more questions by tags JavaSpring