How to print list of absolute URLs in JSP - java

I want to print a list of absolute URLs in a Spring JSP, for an internal user to pick from. However, the page is rendered with the current URL prepended.
For Example I want to a link to www.anothersite.com, but the links comes out as http://localhost:8080/myapp/www.anothersite.com on the page
What am I doing wrong? Both lines below have the same result.
<c:forEach items="${listAppURLForm}" var="nextURL">
<li>
</c:out>>${nextURL.link}
<a href=${nextURL.link}>${nextURL.link}</a>
</li>
</c:forEach>

There's a misconception. An URL like www.example.com is definitely not an absolute URL. The URI scheme is completely missing. You need to prepend the URL with the desired scheme to make it really an absolute URL, like so http://www.example.com.
If you can't edit the URLs directly in the list, then you'd need to prefix it in HTML instead.
${nextURL.link}
You might want to perform a ${fn:startsWith()} check beforehand to prevent duplicate schemes.

Related

how to do <a href/> properly with thymeleaf?

I am struggling with my thymeleaf template, as follows.
So, I have an Arraylist of urls of the same name, which I want to display on a page.
<a th:each="u:${urls}" th:href="${u}" th:value="${u}">[[${u}]]<br></a>
The problem is that, when I click on one of the rendered links. It simply appends my url to the current url. e.g.:
http://localhost:8080/www.google.com
What's going on here? and how should I achieve what I'm attempting to? I have tried "base href", to no avail.
The urls need to have http:// or https:// in front of them. (If they don't they are considered relative urls and the browser correctly appends http://localhost:8080/ to them.) You can add them in like this if you want:
<a th:each="u: ${urls}" th:href="|https://${u}|" th:text="${u}" />

Navigating in spring URL

I have a question regarding navigation.
Currently this is my URL
http://localhost:8080/spring/Account/ump
I would like to navigate to
**http://localhost:8080/spring/Scheduler/test**
this is the URL i am trying to hit but i cannot find the correct ahref code to achieve
My Code
<li>test page</li>
I have tried alternatives such as
<li>test page</li>
The result would be
http://localhost:8080/Scheduler/test
and for the redirect result it will be
redirect:/Scheduler/test
I use a InternalResourceViewResolver if it matters.
If you are able to use JSTL on your pages, use the c:url tag which is defined in the core JSTL. The url tag always appends the actual context path to the start of the url.
example:
<li>
test page
</li>

Print only absolute URLs

I wrote a simple Java Web Crawler that lets the user type in any web page and it will search through the page and pull out the links as Strings. I am not using a package like Jsoup. My question is, how do I only print the absolute URLs rather than both relative and absolute URLs?
Inspect the src or href attribute to see if it's absolute, relative, or protocol-relative (//stackoverflow.com/file). Parse the page's URL. If the tag was protocol-relative, use the protocol from the parsed page URL, then append the content of the attribute. If it's relative, strip the query string and fragment IF from the original URL, and "append" the relative portion. Be aware that a relative URL can look like /foo, foo, foo/bar, or ./../../bar/../foo, so you might want to resolve path traversals before printing.
Edit:
Take a look at URL and the Commons URL Builder. They'll both be helpful.

How to configure s:url to return the absolute path, so that it doesn't get affected by html base?

Using <base href="<s:url value="/"/>" target="_blank"> resolves all images & stylesheets properly, when there are many namespaces like /, /admin etc.
But the action urls also get interrupted by base tag.
Suppose the current browser url is http://context/admin/dashboard
<s:url value="clients" namespace="admin"/> returns clients which in the browser gets resolved to http://context/clients instead of http://context/admin/clients
Is there a way to tell s:url to render absolute URLs instead of relative ?
http://struts.apache.org/development/2.x/docs/url.html
You have wrong value to the tag attribute namespace. The namespace value should correspond to the package attribute and use the path value calculated from the web content root. So, if you have declared the namespace="/admin" this value should be used to the corresponding url tag attribute.
<s:url action="clients" namespace="/admin"/>
The result outputs to HTML, and you could see what value is rendered.

How to check the URL pattern inside JSP?

How I can match the current page URL with a certain pattern. For example I want to make if statement that makes one region on the page appears or disappears depends on the URL pattern of the current JSP page.
What I know , I have to use the tag :
<c:if test="the conditional test">Region</c:if>
for example I want the region appears if the end of URL is matching /me/*
?
You can use
HttpServletRequest#getRequestURI()
to obtain the request URI. The
getServletPath()
as suggested by the other answer is not necessarily helpful as it represents the servlet path (the matching part in the JSP/Servlet URL pattern), not the request URI (as the enduser sees in the browser address bar). If the JSP was been forwarded by some front controller servlet, you would get the JSP's own path instead of the virtual path as in the browser address bar.
Assuming that you have a menu which is represented by a List in the application scope where the Page class has url and name properties
you can use this code to find current page url, and then do your task
<c:set var="active" value="${fn:endsWith(pageContext.request.requestURI, page.url)}" />
Something along the lines of:
<c:set var="url" value="${pageContext.request.requestURL}" />
<c:set var="pathinfo" value="${fn:split(url, '/')}" />
<c:set var="pathnode" value="${pathinfo[pathinfo.length - 1]}" />
<c:if test="${pathnode == 'me'}">
<p>Show this region</p>
</c:if>

Categories

Resources