Get an absolute URL in Spring MVC - spring

In a JSP page, I need to have an absolute URL of on of my page in order to do some integration with twitter.
This is what I have
<spring:url value="/blog/article1" var="articleUrl" />
<spring:url value="https://twitter.com/share" var="twitter" >
<spring:param name="url" value="${articleUrl}"/>
</spring:url>
Tweet
My problem is that it generates :
Tweet
And of course, twitter do not like relative URL...
Is there a simple way to have the absolute URL (with spring:url or c:url or other...)?

Try to explore more about context attribute in <spring:url/> tag. I think it would be helpful to you.
Hope this helps you. Cheers.

I found my response on StackOverflow :
<c:set var="baseURL" value="${fn:replace(pageContext.request.requestURL, pageContext.request.requestURI, pageContext.request.contextPath)}" />

Related

How to mix href within jstl code

When I use the below jstl code
<a href="http://mysite.com?id="<c:out value="${myid}"/>/><c:out value="${myid}"/></a>
the output is :
"1234"
The value 1234 corresponds to the variable value of myid but the url being generated is
"http://mysite.com?id=" so no value for myid is being generated as part of the href.
How can I amend the href so that entire href is displayed :
"http://mysite.com?id=1234"
instead of :
"http://mysite.com?id="
Ultimately, JSP/JSTL generates HTML. You're familiar with basic HTML, right?
Look closer at the generated HTML output by rightclick, View Source in browser. You'll see:
<a href="http://mysite.com?id="1234/>1234</a>
Is that valid HTML? No, you're closing the attribute value too soon with " at wrong place and you're closing the tag too soon with />. Look, the Stack Overflow HTML syntax highlighter also got confused. Instead, it should have been:
1234
Fix the HTML generator (i.e. the JSP/JSTL code) accordingly so that it generates the desired HTML:
<c:out value="${myid}"/>
Unrelated to the concrete problem, the <c:out> is only helpful in preventing XSS attack holes when redisplaying user-controlled input and actually the wrong tool to inline URL parameters. If you can guarantee that ${myid} is always a number (because it's a Long or Integer), you can even just leave it entirely out, making the code prettier to read:
${myid}
If the ${myid} is however not a guaranteed to be a number (because it's a String), then you should use <c:url> and <c:param> to properly URL-encode it:
<c:url value="http://mysite.com" var="myURL">
<c:param name="id" value="${myid}" />
</c:url>
<c:out value="${myid}" />
<c:url> tag is used to create an url. It is helpful in the case when cookies is turned off by the client, and you would be required to rewrite URLs that will be returned from a jsp page.
<c:param> tag may used as a subtag of to add the parameters in the returned URL. Using these parameters encodes the URL.
<c:url value="http://mysite.com" var="myURL">
<c:param name="id" value="${myid}" />
</c:url>
<a href="${myURL}" />${myURL}</a>
Read more from here.

Spring url tags with jsessionid

I have a jsp with the following (relevant) setup:
<s:url value="/res" var="res_url" />
<link href="${res_url}/less/bootstrap.less" rel="stylesheet/less">
<link href="${res_url}/less/responsive.less" rel="stylesheet/less">
...
Ive noticed a problem with using this technique, in that on the first page load of a new session my res_url variable will have ";jsessionid=xxxxxxxxx" appended. In this case that means the id appears in the middle of my stylesheet URL and therefore the stylesheets are not loaded.
I realize that I'm probably not using the URL tag in the way its intended, and that you can include param tags inside the URL tag to get around this, but I don't like the idea of it and think the way i did it was much cleaner. Is it possible to somehow tell it to ignore the jsessionid? Or is there any other way of doing this?
I don't see the benefit of using Spring's URL tag over the standard JSTL tag. What about
<c:url value="/res/less/bootstrap.less" var="lessBootstrap" />
<link href="${lessBootstrap}" rel="stylesheet/less">
If you want to define the /res/less path in a variable instead of repeating it you may do this like this:
<c:set var="resDir" value="/res/less" scope="request" />
The right way to do it is
<link href="<s:url value="/res/less/bootstrap.less"/>" rel="stylesheet/less">
<link href="<s:url value="/res/less/responsive.less"/>" rel="stylesheet/less">
I don't see what any simpler way to do it.

How to create an image map on ASP.NET MVC 3?

I've been looking for a way to create an image map in ASP.NET MVC 3 and so far I didn't found. What I want is quite simple:
<img src="#Url.Content("~/Content/images/minasmapa.png")" usemap="minasMap"/>
<map name="minasMap">
<area shape="circle" href="" coords="249,346,9" />
<area shape="circle" href="http://" coords="433,392,8" />
</map>
The problem is: how to put an action link inside the href? What I want is to put something like an action link instead of creating the url myself, because this way I have no problem with servers.
I found this solution, but it used Url.RouteUrl, and I couldn't understand how this worked.
Just found it ... you just have to insert #Url.Action("PlantInfo","PowerPlant",new {id = "1"}) inside the href.
You can also try the example provided in this article. This allows you to construct an imagemap using c#, both in the view / controller / elsewhere.
http://www.avantprime.com/articles/view-article/9/asp.net-mvc-image-map-helper

How to use <spring:url /> with an <a> tag?

Can I use <spring:url value="/something" /> inside of an <a> tag?
<spring:url value="/something" var="url" htmlEscape="true"/>
...
But you an also use c:url
<c:url value="/something" var="url"/>
...
The one important difference between c:url and spring:url is, that c:url does not html encode the created url. But for a valid url the & between the url parameters must be a &. So you need the c:out to escape it. -- In spring:url you have this functionality already included (if I understand the documentation correct).
Namespaces:
xmlns:spring="http://www.springframework.org/tags"
xmlns:c="http://java.sun.com/jsp/jstl/core"
http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/spring.tld.html#spring.tld.url

How to create hyperlink in Spring + JSP

What's the proper way to create a hyperlink in Spring+JSP? There must be a better way than just coding in the <a href="..."> tag. Take for example a page that displays people. The URL is people.htm. The corresponding controller gets people from the database and performs optional column sorting. The JSP might look like:
<table>
<tr>
<td>Name</td>
<td>Age</td>
<td>Address</td>
</tr>
...
This seems bad as the URL people.htm is hardcoded in the JSP. There should be a way to have Spring automatically build the <a> tag using the URL defined in servlet.xml.
Edit: Maybe I should be using a Spring form.
The only thing that comes to mind is the JSTL standard tag <c:url>. For example:
<c:url var="thisURL" value="homer.jsp">
<c:param name="iq" value="${homer.iq}"/>
<c:param name="checkAgainst" value="marge simpson"/>
</c:url>
Next
Now this won't get you servlet mapping or the like but nothing will. It's not something you could really do programmatically (after all, a servlet can and usually does map to a range of URLs). But this will take care of escaping for you.
I haven't seen this kind of functionality in pure spring (although grails offers things like that).
For your specific case you might consider removing the file part and only using the query string as the href attribute:
<td>Name</td>
<td>Age</td>
<td>Address</td>
These links append the query string to the path component of the current url.
In Spring MVC in jsp:
You can use:
General Hyperlink:
Click Here
If passing from controller:
Click Here
Jsp tags
<c:url var="URL" value="login">
<c:param name="param" value="${parameter}"/>
</c:url>
Click Here
Hope it Helps.. :)
Better way to create link is:
Name
<%=request.getContextPath() %> makes sure that correct URI will be taken into account.
"sort" parameter you can get over with hidden field and change a value with a little bit of javascript:
<input type="hidden" name="sort" id="sort" value="name">
And controller method should look like this:
#RequestMapping("/people")
public String createUser(String sort) {
...
}
Import this package in your jsp file
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
when you want to redirect new page or url then use for eg.
<a href='<c:url value="url of next page" />'>Home</a>

Resources