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

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

Related

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

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

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

Liferay Page redirection

I have developed a theme in liferay 6.1. I have a page named "localhost:8080/home" but now i want that on clicking this link of the page, it should be redirected to localhost:8080
Any suggestions are welcomed.
Thanks in Advance.
I think you are confused a little bit, so just some things you should know:
You can't (normally and without hacks) have a page named "localhost:8080". Every Page (or 'Layout' in Liferay) has a short name, that takes it's part of the url. This is often called "friendly url" but it's often confused with the "friendly url feature", which is a way to shorten your url request data.
So you're always going to have urls like 'localhost:8080/something'. The same holds for the 'home' page
You can partially shorten the Url by using 'virtual host'. It removes the part of the url before your page's name (like removing the web/guest or user/username ) suffix
You can use the 'friendly url' feature to shorten the part of the url that goes after the page's name, and contains request information like lifecycle state info or custom request parameters

Spring MVC navigation path

I have a problem when trying to make some simple navigation in spring mvc. I have a navigation controller:
#Controller
#RequestMapping("/secure")
public class NavigationController {
#RequestMapping("/operation")
public String processOperationPage() {
//Some logic goes here
return "corpus/operation";
}
#RequestMapping("/configuration")
public String processConfigurationPage() {
//Some logic goes here
return "corpus/configuration";
}
}
and there is my links to reach that controller:
Operation
Configuration
When the first time the link is clicked everything is OK. In the browser I see the normal path as I am expecting. For e.g: http://localhost/obia/secure/configuration.htm. But if I am at this page, and from this page I want to reach operation.htm when I click the operation link the path becomes like this: http://localhost/obia/secure/secure/operation.htm.
The secure appears two times. How can I solve this problem?
Your links are relative. Adding a slash in front of them will fix it.
If you are using JSP, use JSTL instead:
<c:url value="/secure/operation.htm" />
Remember include taglib in JSP file:
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
By using JSTL, you can avoid to change the URL once the app is deploy to different context such as http://host/ and http://host/myapp
The first one will generate http://host/secure/operation.htm and second one will generate http://host/myapp/secure/operation.htm for you.
Change your URL from relative or calculate relative URL dinamically depending on current page. E.g. you can change your URL to host-based:
Operation
Configuration
Responder above has one correct answer in using c:url to generate an absolute URL. However, there are situations where the JSTL doesn't know the URL base correctly because of a firewall configuration. In that case, you can use a relative URL, but it has to know where you're starting from. i.e. on the page obia/secure/operation.htm, the url would be ../secure/configuration.htm, or just configuration.htm. The dot dot means to traverse up one level.

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).

Categories