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}" />
Related
I hope you get my problem.
out.println("<li class='has-sub'><a href='#'>" + k1.getKName() + "</a>\n");
I have a JSP and inside this java code. The result is a navigation on the left side with several categories and subcategories. So this is one category element. As you can see, I didn't put anything in the href. What I want to do is, that when I click on this category, I will get the articles of this category in the content space on the right side.
So, what do I have to do with servlets or JSPs in order to give a result to the content space. I can't just call a servlet there of course, because that means that I get the result of the servlet inside the href obviously.
I am sorry if this is a silly question, but I really don't know how to solve this :(
Further to previous comments you do not need web services. You can do this using ajax and a normal Servlet. You might want to look at using JQuery to help with the Ajax part. Here's some JQuery documentation around the load() function which will:
Load data from the server and place the returned HTML into the matched
element.
https://api.jquery.com/load/
Your link will look something like (if k1 is a bean in some scope then you can use EL rather than scriptlets):
<a href='javascript:loadData(${k1.id});'>${k1.name}</a>
Your Javascript will look something like:
function loadData(id){
var url = "/pathToMyServlet?id=" + id;
$( "#result" ).load( url );
}
which will call your Servlet and insert the HTML returned to an element on your page with the ID 'result'.
Your Servlet then needs to generate the data and forward to a simple JSP which returns the results (and only the results) i.e. it does not need to be a fully formed HTML page but should only contain the table of results or whatever.
And stop using scriptlets:
How to avoid Java code in JSP files?
What is the best way of obtaining context-root on a jsp page. If I hardcode my css/image to "/myapp/images/foo.gif" then it will be a broken link if the context root / app name is changed. Using relative path is not always preferrable because a context root can be multi-path (eg: /mysite/myapp)
So far I've tried using <c:url var="root" value="/"/> which works alright (${root} will give the context-root /myapp/), however if this is the very first time user is visiting the site (or if cookie is cleaned on the browser), the value assigned to ${root} became something like /myapp/;jsessionid=019762g1hk3781kh98uihilho and it breaks the images/css reference. Is there any other better way than this?
So far I've tried using <c:url var="root" value="/"/> which works alright (${root} will give the context-root /myapp/)
This is not the right way. The <c:url> should be applied on every single URL individually.
You'd better use
<c:set var="root" value="${pageContext.request.contextPath}" />
See also:
Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
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.
I have two different divisions in a JSP page. One contains a menu of links, when clicked the div2 (id-content) loads different pages accordingly. I am doing something like -
<div id="menu">
<ul class="navbar">
<li><a name="login" href="Login.jsp" onclick="changeContent()">Login</a>
</li></div>
and in the script I have something as -
<script language="JavaScript">
function changeContent() {
document.getElementById('content').load('Login.jsp');
}
</script>
I also tried -
document.getElementById('content').innerHTML=
"<jsp:include page="Login.jsp">";
None of the ways worked. Please suggest how should I
Try jquery..
function changeContent() {
$('#content').load('Login.jsp');
}
The solution is to use Ajax, which will asynchronously retrieve your page content that can be pasted in with the innerHTML method. See my answer to a similar question of how an Ajax call works and some introductory links.
As to why your examples in your answer don't work, in the first case there is no load() method on an Element object (unless you've defined one yourself and not shown it). In the second example, as one of the question comments says, there is probably something causing a syntax error in the javascript.
As an FYI, when there is a syntax error in some javascript in a web page, the current expression being parsed and the rest of the <script></script> block will be ignored. Since this is inside a function declaration, that function will never get defined.
For instance, an embedded quote in the included page will end the string for the innerHTML assignment. Then the javascript parser will try to parse the remainder of the HTML causing a syntax error as the HTML will not be valid javascript.
We use jquery. Add a click event handler to the anchor elements. In the click handler call $('#content').load(your_url);. You might want to use the load(url, function() { ...}) version. More info here http://api.jquery.com/load/
Your initial page comes down from the server. It's displayed by the browser. When you click on a link (or a button) in the browser, you want to fill the second div with new HTML. This is is a perfect job for an AJAX request. What the AJAX object in the browser does, is to send a POST (or whatever) string to the server. And then the Ajax object receives the HTML response back from the server. And then you can display that response data which the AJAX object contains, anywhere you want.
I'm developing a liferay theme on which I have to place some links to other pages. Now I wanted to put those links like ${httpUtil.encodeUrl("\myPage"")}. However when I disable cookies and visit the page I still see the page without the jsessionid appended to the myPage url. Does anyone know why this happens and a possible sollution?
I also tried some velocity functions without any success.
If you use jsp to render your page, than you have to use <c:url> to print the url.
Added:
If you can not use <c:url> and you need to do it by hand, then have a look at the Implmentation of <c:url>.
Added:
In JSPs you have to "wapp" <c:url> by <c:out> to get the enhanced (by session id) url correct formated.
<c:url var='urlWithSession' value='\myPage' />
my page