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.
Related
In my application, the http://localhost:8080/TestApplication/subCategories/2 will display subcategories of my table with id 2.
Click Here
When I click on the link rendered by the HTML above, my server is redirecting to http://localhost:8080/SecondOpinion/subCategories/hello
I want it to redirect to
http://localhost:8080/SecondOpinion/hello
How do I achieve that?
First of all, this is nothing to do with "anchor tags". An anchor tag is an HTML element of the form <a name="here">, and it defines a location within the HTML that another URL can link to.
What you have is an ordinary HTML link, and what you are seeing is standard HTML behavior for a relative link.
A relative link is resolved related to the "parent" URL for the page containing the link.
If you want a link to go somewhere else, you can:
Use an absolute URL
Use path in the relative link; e.g. `Click Here
Put a <base href="..."> element into your document's <head> section.
In your case, you seem to be1 combining relative URLs with some unspecified server-side redirection. In this case, you could either:
change as above, so that the URL that is sent to the server (before redirection) goes to a better place, or
change the redirection logic in your server.
I can't tell which would be more appropriate.
1 - I am inferring this because you said "my server is redirecting to". It is possible that you actually mean that the browser is sending that URL to the server, and there is no redirection happening at all.
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
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
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).
To remove the language toggle from the page view(Comfirmation Page)
I found this code but it doesn't work in Spring MVC
<c:if test="${!fn:contains(pageContext.request.servletPath,'/comfirmation')}">
//Other Code
</c:if>
My actual url is (ShoppingCart.jsp).
It is used when /viewCart.htm,/updateCart.htm,/Confirmation.htm,etc.
So, the user go to the /Confirmation.htm, it also redirect to the ShoppingCart.jsp but the url path in the browser is /Confirmation.htm.
I want to remove the language toggle when call the /Confirmation.htm in the above mention.
Finally, I got it. Here we go
<%
String url=request.getAttribute("javax.servlet.forward.servlet_path").toString();
if(url.equals("/Confirmation.htm")){
%>
//Language Toggle code
<% } %>
I decided to use this. Another way is that storing url path in session since front controller.
the pageContext.request.servletPath will give you the path of the jsp (and not the url your browser shows).
The request is forwarded to a controller, which returns a path to a view. The view ist called using a second internal request