I like to pass values from a spring controller to a filter without using session. suggestions please..
From the first controller I set the values to the request and showing a page. Some jsp pages included with this view (using tiles) is using this attributes. When I try to access this values from this controllers, it is null .
Add an attribute to the request in your controller (using request.setAttribute(...)), then fetch it in the filter (using getAttribute(...)).
(Answer is as lacking in detail as the question...)
Related
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 have a page with part of a URL that is dynamic e.g.
http://localhost:8080/myApp/myPageList.htm?recNo=107&recNo=-96&recNo=-24&recNo=-9&recNo=38&recNo=-21&recNo=-50&crimeRecNo=-110
The last part of the page recNo is a parameter passed to the page. However, the parameter value was encrypted. I would like to know how I can set the urlMapping in the controller. I will be posting the data and i will require a urlMapping to process this form.
So based on your comments your request mapping should be
#RequestMapping("/myPageList.htm")
And the signature of your method something like
public WhateverType myPageList(#RequestParam("recNo") List<Integer> recNo, ...
assuming that recNo are integer values.
Using SimpleUrlHandlerMapping (doc sample)might help.
Have a look at ControllerClassNameHandlerMapping here, as well
Sorry friends if this question is very easy but i am confuse i unable to find out solution.
As we all know in spring MVC framework we create controller which will handle multiple request from same page using #requestmapping annotation.
but same thing i want to do in servlet how can i do ?
Suppose i have a jsp which which will contain a jqgrid,and two forms i want to use only one servlet to load the data into jqgrid and that servlet only will handle request from both the form . Since we have only doGet and doPost in servlet how one servlet fulfill all three request. Hope you understand my question if you have and link where i get sample or and tutorial link plz reply me
Well, the only easy way to do this would be to use a request parameter to control how the processing happens.
In a very basic example, you may have something like a requestType value that gets passed as either part of the query string or the request body. You would assign values of 1-3 (or 0-2) with each value indicating a different type of request. Your servlet would then parse the request accordingly.
This actually is how the DispatcherServlet in SpringMVC works. There's only one servlet class instance and when a request comes in, it examines the query string along with other parts of the request to determine which controller should handle the request.
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.
My websites pages are broken down into 3 parts:
Header
Main Content
Footer
The header needs to access the user object to display things like the username, sign/signout links depending if the user is logged in or not.
I want my velocity templates to be like this:
inject header template, that will need access to the user object.
main content will be a spring mvc action page
footer
I know that in my spring mvc action, I will simple load my velocity template with the attributes/objects that I will need to display in the 'main content' area.
Is there a way for me to provide the 'header template' access to the attributes/objects it needs from a place OTHER than the spring mvc action?
I am new to java/spring, is there a request level collection that I can load my user object into, so I can reference it from there?
I think that the way to do this in the Spring MVC framework is to use a custom Handler Interceptor that attaches the necessary information to the request (or response) object and then returns true to allow the request to proceed to the designated controller.
(Warning: I've never done this, YMMV, etc ...)