How do I add a anchor to a HREF in JSTL? - java

I was wondering how I go about adding a anchor to a HREF in JSTL. I've tried googling it and am no further forward really, I tried adding it as a PARAM but to no avail. My current code is like this;
My URL
But my code fails to validate and my server Error 500's. Could someone explain to me how to successfully an anchor to my HREF please?

I figured it out, but if there is a better way of doing it, please let me know ;)
I have this code in place, the dynamic bit is inside of a Mustache template;
<c:url value="/myURL#" var="myUrl"/>
My URL

Related

How to handle URL and #RequestMapping in Spring MVC

I have a doubt about the best way to handle the #RequestMapping.
For example:
with this url http://localhost:8080/SpringExample/admin/personaListForm/1 I reach a form, controlled by this #RequestMapping:#RequestMapping("/admin/personaListForm/{tipoPersona}")
As you can see "1" is a variable.
This is my form:<form:form action="personaListFormSent">
As you can see, If I submit the form, I'll be sent to this url http://localhost:8080/SpringExample/admin/personaListForm/personaListFormSent (because of the "/1").
The problem is that i don't want to go there, I want to go to http://localhost:8080/SpringExample/admin/personaListFormSent
I may solve the problem editing the form this way <form:form action="../personaListFormSent"> but it doesn't seem a professional way to handle this problem, since if tomorrow I need to add more variable I'll have to add more "../" to the form tag.
thank you
You can use ${pageContext.request.contextPath}/personaListFormSent.
<form:form action="${pageContext.request.contextPath}/personaListFormSent">
So you will go to http://localhost:8080/SpringExample/personaListFormSent when you post the form.
Send them to action="/personaListFormSent".
'/' is the root of your app, so it doesn't matter which is your context path.
Regards,
Jorge

Links not working if current url doesn't end with "/" (slash)

I am currently experiencing very strange behavior.
If my url doesn't end with slash, all links are broken. Specifically, if my url is "http://localhost:8080/SharedTodoListDemo/todolist", then this code
<spring:message code="todolist.button.delete"/>
redirects to "http://localhost:8080/SharedTodoListDemo/delete"
If my url is instead "http://localhost:8080/SharedTodoListDemo/todolist/"
then the same code redirects (correctly) to "http://localhost:8080/SharedTodoListDemo/todolist/delete"
Do you have any ideas why this might be happening and what to do about it? I am using Spring + Spring MVC and need to link uniformly in order to be able to map to controller methods.
Thanks.
EDIT:
I've also tried href="/delete" and href="./delete". The first redirects even more strangely to "http://localhost:8080/delete" both with and without the backslash in url, the second works the same as above.
Ok, after a while I've solved it myself.
This does the trick:
<spring:url value="/product/new" var="springLink" />
spring link
The above works in both cases (the original url ends or doesn't with slash).
Compare to these, which don't work:
my link
my link
my link
my link

How to map server response retrieved in jsp to an iFrame

I'm using struts2 framework(java/js/html/css combo) for my webapp. I am reading a text file from server and I want to write the response to an iFrame present in the same jsp.
Flow:
(1) On click of a link, I pass the relative URL of the text file to jsp.
(2) When the jsp page loads, the java code in the jsp reads the file from server.
(3) Now this response has to be written to an iFrame present in the same jsp file
Can anyone plz help me in writing such response to an iFrame?
Thanks in advance :)
[code not tested, only a demostration of the concept]
here's some very rough idea as to how to fix your code, they definitly not the best but they should be enough to help you understand the concept.
However I'd still recommend going over the whole concept and maybe come up with a more efficent way to do what you need.
if you insist on using iframe, you need to make use of 2 seperate jsp as W3C says in "Implementing HTML Frames":
Any frame that attempts to assign as its SRC a URL used by any of its ancestors is treated as if it has no SRC URL at all (basically a blank frame).
so you'll need 2 jsp, the first one is basically what you have but the the src of the iframe changed to:
<iframe scrolling="yes" width="80%" height="200" src="second.jsp?content=<%=all%>" name="imgbox" id="imgbox">
and the second one will be something like :
<html><body><%= request.getAttribute("content") %></body></html>
From the code you've shown you forced a "content update" on the iframe by using javascript. The proper/usual way to update an iframe is to provide different input parameter to the second jsp and let it update it for you.
Finally, I'd recommend using JSTL as much as possible instead of scriptlets. It is much cleaner.
What you need to do is set the src attribute of the IFRAME to the jsp url when your link is clicked. Another way to do it is doing something like this:
<iframe src="" name="iframe_a"></iframe>
<p>W3Schools.com</p>
with the correct parameters of course

Get url minus current filename in JSP or CQ5

I wish to get the current url minus the file name that is being currently referenced. Whether the solution is in JSP or CQ5 doesn't matter. However, I am trying to use the latter more to get used to it.
I'm using this documentation but it's not helping. CQ5 Docs.
The example I found retrieves the full current path, but I don't know how to strip the file name from it:
<% Page containingPage = pageManager.getContainingPage(resourceResolver.getResource(currentNode.getPath()));
%>
Profile
Assuming you are accessing the following resource URL.
/content/mywebsite/english/mynode
if your current node is "mynode" and you want to get the part of url without your current node.
then the simplest way to do is, call getParent() on currentNode(mynode)
therefore, you can get the path of your parent like this.
currentNode.getParent().getPath() will give you "/content/mywebsite/english/"
full code :
<% Page containingPage = pageManager.getContainingPage(resourceResolver.getResource(currentNode.getParent().getPath()));
%>
Profile
A much simpler approach.
You can use the currentPage object to get the parent Page.
The code looks like this
Profile
In case you are getting an error while using this code, check whether you have included the global.jsp file in the page. The one shown below.
<%#include file="/libs/foundation/global.jsp"%>
I don't know anything about CQ5, but since getPath() returns an ordinary Java string I expect you could just take the prefix up to the last slash, which for a string s can be done with s.substring(0, s.lastIndexOf('/')+1). If you have to make it into a one-liner, you could do containingPage.getPath().substring(0, containingPage.getPath().lastIndexOf('/')+1).

A weird problem happened when parsing a html page using HTMLParser

I was parsing a web page using HTMLParser in Java, I met a weird problem when using class HasAttributeFilter.
The element I wanna parse in the page is <span style="vertical-align: middle;"></span>, so the expression should be HasAttributeFilter filter = new HasAttributeFilter("style", "vertical-align: middle;");, right? Yeah, I used this exp, but it DIDN'T WORK! BUT I am sure there IS the node in the page
After that, I applied some other exp, such as HasAttributeFilter filter = new HasAttributeFilter("class", "singlecolumnminwidth"); to the same page, and also, the node is there, something weird happened, this expression WORKED!
Has anyone met this problem before? Help me ...
Thanks in advance!
The page's link.
what do you get if you fetch the value of this attribue and print it out to the screen?
do you maybe have to escape some chars like space or minus? think it could have problems with the space in between
does vertical-align:middle; work?
or maybe test if its the minus causing an error

Categories