1

Sorry in advance if this is something trivial

In ASP.NET I remember you could direct your routes to

<a href="~/User/Login">Login</a>

and you didn't have to worry about anything URL related (if your project is on localhost:1234 or localhost:1234/myproject/ or whatever), ASP.NET would do the job for you.

Is there an equivalent for that in Spring. Currently when I start up my project, using GlassFish, it starts up at url localhost:8080/myproject and the resources (css, js...) aren't even being loaded until I add another / in the end (localhost:8080/myproject/).

All my routes are on the first URL parameter and my GET variables aren't even RESTful, simply because if I go one / more, my routing is going to go wrong, ie.

<a href="/home">Home</a> //will go to localhost:8080/home (not the project scope)
<a href="home">Home</a> //is fine until I go to another / in url...
                       //...(/foo/bar), then it goes all the way up 
                      //to localhost:8080/foo/home

I tried to google this, but every time I try to google something equivalent to ASP.NET, I just get a lot of ASP.NET tutorials (nothing Spring related).

So.. is there any way to keep the url consistent, something in the lines of:

<a href="~/user/login">Login</a>

or

<a href="${ projectUrl }/user/login">Login</a>

How is it normally done in (commercial) applications? What's the best practice in this?

Toza
  • 1,348
  • 2
  • 14
  • 35
  • You need to use either JSP or a templating engine - the Spring provides a [taglib](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/tags/UrlTag.html)/[macros](https://svn.apache.org/repos/asf/qpid/tags/pre-M1/java/java/management/core/lib/spring/spring.ftl) to create the correct URL. You cannot do this (easily) with vanilla HTML. How are you creating your webpages? – Boris the Spider Feb 13 '16 at 18:16
  • @BoristheSpider I'm using JSP, yeah. – Toza Feb 13 '16 at 18:19

1 Answers1

1

You need to use the Spring Tag library.

First import the tag library in your JSP:

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>

Now, instead of writing a raw URL, you use <spring:url>, for example:

<a href="<spring:url value="user/login"/>">Login</a>

Or you can assign the value to a variable and reference it:

<spring:url value="user/login" var="login"/>
<a href="${login}">Login</a>

Which you prefer is a matter of preference.

The tag will then work out the web application context, etc, and replace <spring:url ... with the full URL.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • That did the trick! Thanks! Pitty there isn't a shorter way to do that, though. – Toza Feb 14 '16 at 00:15
  • 1
    @NemanjaT The idea with JSP was to create a generic framework with minimal special syntax. A JSP is compiled to Java then compiled again to a servlet class. All a tag does is insert some custom code into the process. Agreed this does make simple things verbose, but it also means that you can very easily write your own tags do reuse bits of complex logic. As with everything - swings and roundabouts. – Boris the Spider Feb 14 '16 at 01:18