dev-null dev-null - 1 year ago 98
Java Question

Best way to use absolute/global paths in view templates in Spring MVC

What is the best way to reference absolute path (relative to the context root of course) in templates in Spring MVC ?

For example I have a page A managed by controller C-A which contains link to a page B with URL

managed by controller C-B.

If this was link to an A page location I would use relative path. But what when I want want to refer C-B's view (

What is a best approach here ?
Should I have all the paths stored in a global model attribute and used by the view templates?
Should this be defined statically (as constants) or dynamically?

All this while preventing to have one path at multiple locations.

Answer Source

What I decided to finally use was custom wrapper bean around MvcUriComponentsBuilder. I use it from Thymeleaf views like this ${@router.url('SC#someMethod'} where 'SC' are capitals of the controller name in this case SomeController. This couples view to the name of the controller and method but is easy to use and does not centralize apps URLs as static variables and allows dynamic building of URLs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download