Get url minus current filename in JSP or CQ5 - java

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

Related

URL removing for anchor tags

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.

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

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.

How to jump to specific page number using display tag in Spring framework

I want to jump to a specific page number using display tag with a textbox and a "go" button.
On the click of GO button calls a javascript in which it should go to that specific page through that .htm which is not happening.
please suggest an argument for this particular way of getting a specific page or else alternate suggestions are always welcome
Below are the arguments in displaytag.properties which i know so far
enter code here
{0}: numbered pages list
{1}: link to the first page
{2}: link to the previous page
{3}: link to the next page
{4}: link to the last page
{5}: current page
{6}: total number of pages
Below is the javascript function which is being called on click of GO button
function selectPage(){
alert("pageNo:" +document.portalDisplayform.selPageNo[0].value);
alert("pageNo:" +document.portalDisplayform.selPageNo[1].value);
var pageNo = document.portalDisplayform.selPageNo[0].value;
var pageNo = document.portalDisplayform.selPageNo[1].value;
document.portalDisplayform.action = '<%=request.getContextPath()% >'+"/portalAccessdisplay.htm?tokenId="+'<%=cachetoken%>'+pageNo;
document.portalDisplayform.submit();
}
It's not so easy, because the displaytag encodes parameters (to avoid naming conflicts with functional parameters), and uses a unique ID per table, in case several tables are on the same page. I suggest you download the source code of displaytag, have a look at the ParamEncoder and Pagination classes (and their callers) to discover how a link to a specific page is constructed by the displaytag. You'll have to use similar code to generate the URL, and you'll have to modify the value of the appropriate (encoded) parameter in your JavaScript code.
The very easiest way, you can modify the TableTag.java which is in the display-tag jar file. In this file modify the initParameters() method. Inside the method
place the below 4 line code.
After this line which is in initParameters() method.
this.pageNumber = (pageNumberParameter == null) ? 1 : pageNumberParameter.intValue();
Place the below code
if((request.getParameter("pageno") != null) && (request.getParameter("pageno") != ""))
{
this.pageNumber=Integer.parseInt(request.getParameter("pageno"))
}
And use a TextBox with the name pageno in your DisplayTag page. Also include the name in your DisplayTag property excludedparam.

jsp to servlet communication with parameters from URL, not a form

I'm using netbeans to create a web application that allows a cell phone user to scan a QR code and retrieve a journal's previous/next title information. The take home point is that there's no form that the user will input data into. The QR code will contain the search string at the end of the URL and I want the search to start on page load and output a page with the search results. For now (due to simplicity), my model is simply parsing an XML file of a small sample of MARC records. Oh, to top it off...I'm brand new to programming in java and using netbeans. I have no clue about what javabeans are, or any of the more advance techniques. So, with that background explanation here's my question.
I have created a java class (main method) that parses my xml and retrieves the results correctly. I have an index.jsp with my html in it. Within the index.jsp, I have no problem using get to get the title information from my URL. But I have no clue how I would get those parameters to a servlet that contains my java code. If I manage to get the search string to the servlet, then I have no idea how to send that data back to the index.jsp to display in the browser.
So far every example I've seen has assumed you're getting form data, but I need parameters found, processed and returned on page load...IOW, with no user input.
Thanks for any advice.
Ceci
Just put the necessary preprocessing code in doGet() method of the servlet:
String foo = request.getParameter("foo");
// ...
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
And call the URL of the servlet instead of the JSP one e.g. http://example.com/page?foo=bar instead of http://example.com/page.jsp?foo=bar in combination with a servlet mapping on an URL pattern of /page/*.
You can get the url parameter in servlet using request.getParameter("paramName");
and you can pass the attribute to page from servlet using forwarding request from servlet to jsp.
See Also
Servlet wiki page
Bear in mind that your JSP page will compile internally to a servlet. So you can retrieve the string and print it back in the same JSP. For instance assuming you get the string in a parameter called param you would have something like this in your JSP:
<%
String param = request.getParameter( "param" );
out.println( "String passed in was " + param );
%>
One last thing, you mentioned the main method -- that only gets executed for stand alone applications -- whereas you're talking web/JSP :O

Categories