How to navigate/browse on next/previous page in aem? - java

I'm developing pages in AEM 6.3. Right now I have a situation to develop a page containing 2 links that browse to next and previous page with respective to current page. Let's say I have 3 pages under my project site /content/project/en.
Page A
Page B
Page C
Let's assume, if I'm on Page B, then my next page should be Page C and previous page should be Page A. Those two links(next/previous) should redirect me to Page A and Page B.
There might be many approaches to do that but what I have in mind is that, I'll be making an ajax get request which will return me next and previous pages in key/value format. To do this I'll be using sling servlet object to get the page path and query builder to make query on that path to get the next and previous page. This approach works fine if there is very limited number of pages but would be time consuming if pages increases.
I want simple solution to implement this. Or is there any plugin in aem to do this? I also tried to do that with currentPage.nextSibling() in sightly but there is no such thing exist in aem.

One possible solution is to create an end point that -
can be called upon a page
returns page paths (use page.listChildren())as JSON array
Two ways to do that -
In your app’s base page, add a jsp listChildren.jsp that renders json
Create a resourceType based servlet with selector listChildren and extension json. ResourceType being page’s resourceType myapp/page/testpage
Call this endpoint on parent page of the page being rendered, thus you get list of all siblings page path and current page’s path. You can get this endpoint to be catchable so as to avoid multiple publish hit.
Once you get JSON, use it in JS to get the index of current page path (use find(‘path/to/current/page)). once you have index, you could use index-1 & index+1 to get previous and next page path.
Note - in case you are using short path for website url make sure JSON also return short path

As addition with new requirement which was page on next/previous link should be sorted in created date with respective of current page. So I came up with following solution.
I have created a component name 'Next Previous Pagination' and used a WCMUsePojo model in htl language. This model is responsible to get next and previous page of the current page. Following are the jcr queries to get next and previous page.
For next Page,
select page.* from [cq:Page] AS page WHERE
ISDESCENDANTNODE(page,'/content/project/myCurrentPage')
AND page.[jcr:content/jcr:created] >= CAST('${createdDateOfCurrentPage}' AS
DATE) AND NAME(page) <>'${currentPageName}' ORDER BY page.
[jcr:content/jcr:created] asc Limit 1
For previous page,
select page.* from [cq:Page] AS page WHERE
ISDESCENDANTNODE(page,'/content/project/myCurrentPage')
AND page.[jcr:content/jcr:created] <= CAST('${createdDateOfCurrentPage}' AS
DATE) AND NAME(page) <>'${currentPageName}' ORDER BY page.
[jcr:content/jcr:created] desc Limit 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.

Check for valid links if a dead URL redirects to a costum error page

I need to test(Selenium) if the links in a given page is valid or not. I found a good post about it here
http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/
But the problem is, what if a error page redirects to a custom error page? Then i would get a 200 or a 302 instead of a 404. How should I go about checking the validity of URLs for webpages that redirect their 404s.
You should utilize an assertion of an element on the page with a known specific test. Use a specific reusable function for this.
Then when you hit a page call the function as a check. If you find the specific element present then click the browser back button after recording the URL. If not you can continue your test as desired. There is another post in regards to recursively finding all links and testing them. How to browse a whole website using selenium?
if (checkError()) //calls specific check for the error on the custom error page
{
//Log URL
string badURL = driver.Url();
//Save somewhere in a list for output later...
//navigate to previous page
driver.navigate().Back();
}

how use RequestDispatcher foward keeping url requested?

I have a JSP page to search customers. This page calls the controller, which execute a method to return a list of customers and after forward to the origin URL;
I used to forward : request.getRequestDispatcher(urlOrigin).forward(request, response);
(note 1: request.getHeader("Referer") was used to get complete origin URL )
(note 2: There a method to split the complete origin URL and get name page )
Since it, I have the following url in the browsear :
(http://domain/ProjetoT/mvc)
Its the url of my controller
If I search a customer again won't work, because the controller url will be recognized as origin url.
I Tried use : response.Sendredirect(urlOrigin);
But I lost my object and the list of customers didn't rendered.
Anyone can help me please?
Thanks!
Instead of intially accessing the JSP page directly in the browser, you could access it through the same controller used to process the search. To do that you would have to program your controller to detect if you are in initial display mode or if you are in "submit" mode. This is typically done by checking the presence of a parameter that is sent in the submit.
So in initial display mode, your controller would just forward to the JSP without any further processing, while in submit mode it would do what it is currently doing. This way you would be using the same URL for both the initial display and the submit and the problem you described should go away (that is, if I understand your question correctly).

Fetching the website with Jsoup - page view source and Jsoup shows different content

I use Jsoup to scrap the website:
doc = Jsoup.connect(String.valueOf(urls[0])).userAgent("Mozilla").get();
Here is the link:
http://www.yelp.com/search?find_desc=restaurant&find_loc=willowbrook%2C+IL&ns=1#l=p:IL:Willowbrook::&sortby=rating&rpp=40
I have added rpp=40 parameter to the link in the command line to display 40 results per page. I'm able to see all the results in page view source.
I know that Jsoup is for the static content only and cannot fetch the websites that use AJAX/JS Libraries technique to generate content. However why Jsoup cannot retrieve the same content as I can see in the browser via page view source? Page view source shows 40 results whereas Jsoup is able to retrieve elements from only 10 results? How can I obtain every elements visible via page view source.
Short answer Jsoup can't execute the Javascript.
Long answer
http://www.yelp.com/search?find_desc=restaurant&find_loc=willowbrook%2C+IL&ns=1#l=p:IL:Willowbrook::&sortby=rating&rpp=40
The webpage your are looking for accepts the Http Get with the parameters. In the normal browser it accepts the params and loads the page . But Not with willowbrook checked(in your example). It loads the JS after it loads the page and the Javascript does the check box for Fliters the serach results. Therefore when you use Jsoup you are getting more results because it loads 'state=IL' without 'willowbrook' filtered.

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.

Categories