dynamically set target of form to iframe conditionally - java

I am developing a web project using spring mvc and dojo. I have a unique problem. This is what I am doing :
Submit a form to the controller and generate a pdf in the controller. Set the pdf in the response
The target of the form is an iframe so that the pdf shows in the form after submit.
This works perfect.
Now, my problem is :
I am doing validations on the form using spring mvc validator and if there is an error I must return to the same page with the error message(s).
The problem is to combine these two approaches. If I find an error and return with the return view, since the target of the form is the iframe, the response gets shown in the iframe which obviously I don't want (I want to show the new page) and if everything is good, I must set the pdf in the response and show it in the iframe using the target. How can I achieve this?

You didn't mention whether you are using ajax to submit the form. If its ajax then it would be straight forward, I think you are not.
If you are doing a normal form submit, then
Don't target the form submit to an iframe.
That solves your problem but it introduces a new one, handling the PDF response.
When your form submission passes the validation instead of returning the PDF return the same view but with some javascript code, dojo in your case, at the bottom of the page to popup an iframe whose url points to the pdf stream.
If you are using Spring MVC 3.1 you can make use of Spring MVC Flash Attributes to share any data between your form processing method and PDF serving method, If you are using previous versions of Spring you can use Session or capture the information in the redirect url to share any information between those two methods.

If you can use AJAX:
Submit the form using AJAX and return an URI for the PDF. Then set the source of the iframe to the returned URI if the validation was ok.
If can't or don't want to use AJAX:
Return the same page again but this time with the source of the iframe already set to the PDF, if the validation was ok.

Related

Scrape a Dynamic Website using Java with Selenium?

I'm trying to scrape https://www.rspca.org.uk/findapet#onSubmitSetHere to get a list of all pets for adoption.
I've built web scrapers before using crawler4j but the websites were static.
Since https://www.rspca.org.uk/findapet#onSubmitSetHere is not a static website, how can I scrape it? Is it possible? What technologies should I use and how?
Update:
When you fill in the search form (Select type of pet and Enter postcode/town or county) in the UI, the results are then displayed below the search box.
The red is highlighted as the search bar and the black is highlighted as results.
I'm trying to scrape the results and also the content of each result.
I've had a look at the request the browser makes to retrieve results, but from Chrome dev tools it isn't obvious what the request is being made.
You could use Selenium to extract information from the DOM once a browser has rendered it, but I think a simpler solution is to use "developer tools" to find the request that the browser makes when the "search" button is clicked, and try to reproduce that.
In this case that makes a POST to https://www.rspca.org.uk/findapet?p_p_id=petSearch2016_WAR_ptlPetRehomingPortlets&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&_petSearch2016_WAR_ptlPetRehomingPortlets_action=search
The body of the POST request contains a lot of parameters, including animalType and location. The content-type of the request is application/x-www-form-urlencoded.
To see these parameters, go to the "Network" tab in chrome dev tools, click on the "findapet" request (it's the first one in the list when I do this), and click on the "payload" tab to see the query string parameters and the form parameters (which contains animalType and location)
The response contains HTML.
I would try making a request to that endpoint and then parsing the HTML in the response.

How to automatically reload a jsp based on other page action

I have a situation that i can't handle and thats why need your help.
I have a jsp page (mention as A in below pic) where there are many rows and each of them can be edited.
At the end of the A jsp page, there is an option to print the page data.
Now, if some body clicks on the edit link/button, another page will open contain the data for that particular row and user can modify the data in the second page(i,e B).
Now, i want, as soon as the user save the B page, A page should be refreshed automatically to provid the updated data for printing.
Please guide me on how to acheive that . I'm using Spring MVC framework for the java application.
The Spring MVC way to meet your requirement is:
the Edit buttons in page A should be links calling page B with the id of the line to edit, something like Edit
the SaveAndClose button in page B should be a submit button that posts the edited values to a controller method. After server side processing, the controller method should redirect to page A. As a side effect, you use the PostRedirectGet pattern which avoids the ugly do you want to send again ... ?"
#RequestMapping(path = "...", method = RequestMethod.POST)
public String saveAndClose(...) {
...
return "redirect:/path/to/pageA";
}
Of course this will display all pages in same window.
If you want to redisplay the window containing pageA on the Save & Close from page B, still allowing the save to be known to the server, you should redirect to a special page (say pageC) that just contains javascript code asking the browser to redisplay pageA. You can either pass the name of the window containing pageA in a hidden field, or you can decide that as the programmer of the web application you know where it should be.
It can be achieved like this. Follow the steps mentioned
1] When you click on edit button in Page A, pass the id of the row to Page B as request parameter.
2]In Page B JSP receive the id of the row and store it in a hidden element.
3]Create a JavaScript function in Page A which should receive row Id and Modified data as parameter. Lets name it this function as updateRows(rowId,modifiedData). In this function write code to update the with id 'rowId' with modified data using javascript
4]Now When you click on 'Save & Close' Button in Page B. Save the data using call to server. If save succeeds then invoke the function updateRows passing it rowId stored in hidden element and modified data as parameters. This function will update the DOM with latest data.
This way you will avoid making server call to refresh the data
There is one more way if you don't want to use ajax.
In Page A define a javascript function refreshPageA(). In this function add page refreshing logic.
When you click on 'Save & Close' button in Page B save the data in server and forward to a plain jsp. In this JSP declare a onload handler. Inside onload handler add following code
opener.refreshPageA();
window.close()
This will refresh pageA and close page B window

Wicket - redirecting to the same page when browser back button is clicked

I have a web application that uses the Wicket framework. It has a form that takes user input and when submitted, redirects to another page. Once the form is submitted and the browser's back button is clicked, the previous form is retrieved from the cache with the values entered. I need to override this behavior and redirect to the latter page (keep staying on the same page) when the browser back button is pressed. Is this possible in Wicket? If so, please guide me on how to achieve this. Thank you.
Wicket handles URLs so if a previous page is requested by it's recognized by page ID that is by default added to the URL, e.g. http:/dummyexample.com?1
What I use to solve that problem is a flow engine that holds the state of model object within it. E.g. you have a model object of type MyFlow containing a field 'String currentState'. In this case if you required a wrong page, that is not belong the stage where your flow is, you can redirect to the relevant page from a controcutor or onIntialize() throwing 'RestartResponseException' that causes redirect to the new page.
Just use an Ajax submit, this way the user cannot return to the input form via back-button.

How can I reload only a part of code - Java

I have a JSP page that at the end comes a pop-up window from a javascript, I want after the users click to reload only a specific part of the JSP page more specific one if - loop want to be reloaded one more time ...can this happened or the idea is totally wrong ?
What you are looking for is called AJAX.
AJAX is the art of exchanging data with a server, and updating parts
of a web page - without reloading the whole page.
jQuery has a very nice ajax api.
To load html pages, you could use jQuery.load like this:
$("#result").load( "ajax/test.html" );
You can also generate dynamic html by calling a REST function of a php page using a different url. Example:
var data = { limit: 25, otherProp: "val" }
$("#result").load( "getHtmldata.php", data);

Spring MVC Controller Changing URL or Issuing incorrect URL

I have a SpringMVC application i am not sure if its working properly. I have a registration form the url reads http://localhost:8080/myapp/registration.htm when i click on the query button the application is posted to the controller and the user is posted to a second page getList.htm which displays a list of results however when posted to the second page the url still reads http://localhost:8080/myapp/registration.htm.
When the second page is posted the user is suppose to be taken back to the first page http://localhost:8080/myapp/registration.htm to display the record selected form the getList.htm (this displays multiple records).
When the user is posted back to http://localhost:8080/myapp/registration.htm the url reads http://localhost:8080/myapp/getList/1985121244.htm where 1985121244 is the record number. Should the url be displaying http://localhost:8080/myapp/registration.htm once the user is posted back to the first page?
Also if the user tries to POST the first page after being returned form the getList.htm POST you get a HTTP 400 and the url reads http://localhost:8080/myapp/getList/registration.htm. The second page is appended to the url and this is not an appropriate mapping in the controller. Can someone explain what is happening here and how is it fixed.
Edited
Also when i first enter the application the main menu is http://localhost:8080/myapp/hello.htm when i click on a a href i am taken to the registration page however the page shows but the url does not change. When the href tage is clicked form the hello.htm page the Controller makes a reguest to get the registration page and returns it using return new ModelAndView("registration"); i saw some sites that say i should use return new ModelAndView("redirect:/registration"); However when that is used i get 404 Not Found - http://localhost:8080/myapp/registration". Any ideas anyone on what i can look at before i post code my code is alot
For me it looks like you just return view name after login. Of course that doesn't change your url. Instead of returning view name use "redirect:/getList.htm" pointing the url you need.
Then controller which processes the getList.htm checks whether user is logged in and return proper view name.

Categories

Resources