I have a spring applicalation that illustate a list of reports.
Everything works fine. Now i want to call a controller that "generates" all these reports and store them locally. That means all jsp-file should be 100% equally renedered like a browser-response but not returned to the browser but stored locally as an html file.
My idea is:
user call a url "generate all reports"
the suiteable controller render all reports in the same why the would call every report individually
the controller would store each rendered jsp to defined place as an html
everything will be zipped (works already) and the user get the zip-file with all reports as return
So the concrete question is:
How can i render a jsp file and store this "rendered return" locally as HTML
Thanks a lot for your respone and help
So final question is:
You want to set up controller and call that controller to save rendered result in local store without returning to the browser.
In this case you can #Autowire the controller class and call controller function. When controller is called in another method response will be returned to that calling method not to the browser. You can process returned multipart (html, jsp...) in calling method.
Related
Can I create a response object in java without creating a Servlet? I have a regular java class right now called Menu. It contains not only my menus for my application but it also contains my CRUD functionality that is tied to the database. Which works fine on the console. What I want to do now is to display the result set on a webpage. I want to use AJAX (no frameworks like jQuery etc.) to capture the information from the result set. In the Menu class I am going to convert the extracted information into a Json string with Jackson databinding (object mapper). I want to use a PrintWriter to send this information back to AJAX. I am assuming I need a response object. Do I have to re-write my Menu class as a Servlet or is there another way to accomplish this?
For querying data from server you need to have a resource which has
http url
can accept a form of request as input
can give response based on business logic in menu crud class.
For this querying as you said you can do with ajax call to this servlet with specified url pattern from your frontend webpage.
In short servlet will be essential. It can handle all http method types like get post and so on.
So you can map your crud methods to your servlet methods
eg :
doGet - querying items.
doPost- creation of item.
doDelete- delete an item.
and so on.
further you will call your crud methods from these servlet do* methods.
for example refer: https://www.geeksforgeeks.org/servlet-crud-operation-with-example/
Hope this helps.
I have a simple route '/firstlast' and I want to display web page 'firstlast.scala.html' with 2 fields and submit button.
How to call that 'firstlast.scala.html' file from a route?
And how to transfer values entered in a form to some controler (some method)?
To display the file, you make a controller and call the controller from the routes file. The methods in the controller will call the template.
You'll want to make two controller methods one to handle a GET request and one to handle a POST request.
Routing GET and POST requests to controllers
How to inject a template into a controller and call it
The GET request is called first. It shows the form with empty values. For the GET the controller makes an empty form model object and passes the form to the template.
How to put default values into the form object on the GET request
When the user submits the form it sends a POST request. The controller for the POST request validates the request body and either process it or, if there's an error, passes it to the template.
How to validate the form on a POST request
I have been trying this for a while with no luck.
I want to know if there is a way to include response of a server request (i.e. a Spring controller) inside a JSP.
I am trying to build an independent module which will rely on some specific objects and will print the HTML based on these objects.
So I want to create a Controller method which will take care of dependencies and return this JSP as response.
I can then include a call in the parent JSP so that it hits the controller method and injects the response returned by this method in the parent JSP.
I read somewhere that jsp:include can be used for this purpose as follows:
<jsp:include page="/test-url" flush="true"/>
Where /test-url will map to a Spring controller method.
But when I run this, i get the following exception:
Servlet.service() for servlet dispatcher threw exception: java.lang.IllegalStateException: Cannot forward after response has been committed
Please provide your valuable inputs if you have some idea about this?
use this way
String pPath = yoururl
<jsp:include page="<%=pPath%>" flush="true">
it work fine for me
I'm been search for a solution for this problem for a while and didn't found any!!
To explain the problem I will give and example:
Let's imagine that I have a search page X with results (x1....x10) and a form to give feedback. This form will call a link for a controller (java spring controller) defined as '/feedback.html'. After the submit the feedback, the controller should return again to X with the same results. And here is the problem, how can I do this? because this feedback controller can go to X or to any other page depending where the form is!
In summary: How can I do the javascript history(-1) in the controller (java spring controller)??
Thanks
If you access the search page like this:
http://domain.com/search/query
or
http://domain.com/search?query=text
Then you can just pass this ulr along with the feedback form (by adding a hidden input with its value the URL)
<% request.setAttribute("redirectURL",
request.getAttribute("javax.servlet.forward.request_uri"));%>
<form:hidden path="redirectURL" value="${redirectURL}"/>
And then in the controller simply access the redirectURL property and redirect to the search page with the same query showing the same results.
The "redirect" Spring capabilities is usually used within a PRG pattern. Given your title and your use case, I'll assume you're trying to get redirected to the search page or another page after submitting your form (form action seems to be '/feedback.html').
So basically you have your feedback controller which should have a #RequestMapping annotated method like #RequestMapping(value = "/feedback.html", method = RequestMethod.POST). From there and within this method, you can redirect the request anywhere you want by returning a String matching an existing mapping in you Spring app (for example, if you want to redirect to the search page, given your search page is mapped with #RequestMapping(value = "/search.html", method = RequestMethod.GET), simply return "redirect:/search.html".
Note that the whole "search page" logic will have to be re-run (the redirect issuing a new GET request) so if you don't want that to happen, you will indeed have to store the search results in session (not sure what sense does that make... but it's possible).
EDIT : If your URL mapping permits it, you can also redirect the request to the search page with search parameters included, something like : "redirect:/search.html?myParam=10".
I think, in the search controller, you can store X in session and at the end of your feedback controler send a redirect to an URL that call the search controller (same methode or another one) that load the search result page using the X held in session.
You can also pass the X parameter with hiden field (if you dont want to use session).
I've one spring controller which is setting some values to request and shows a jsp page. For the view part we use tiles. The result page has 3 parts, header , content and footer jsp's.
This header jsp use a java file and i want to access the attributes created by the first spring controller from this file. Is there any way to do that without using session?
When I tried request.getAttribute, it gives null. I think it's because it's not an immediate file after the request values setting.
As long as everything runs in the same request and the controller code is executed before the view part, setAttribute() should work. To debug issues like that, use a Filter which dumps the request URL and attributes to the console or the log.
If those calls are in different requests, you have two options: The session and a Spring bean (use a session bean or your own implementation). I prefer beans since they are type safe and they allow me to separate my code from the Servlet API which is complex to test.
You'll really need to put some code to get a code answer but unless you're using JSP scriptlets I'm guessing this is a Java bean that you're using in the header. This of course cannot access the request (hence the session) nor should it really. What you probably want to do is convert it to a tag library if you want it to have access to the request/session.