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).
Related
The first step, in SpringMVC application, may be mapping a URL to one Controller (not a Jsp page) to display the HomePage using GET method, isn't it? After the form, in my spicific app, is filled, the same controller get the information, do some verifications and the return a logical view name to InternalViewResolver to show the second page.
My question is: when user click a icon or button in the second page, how does the next controller be selected to run? Is still a URL mapping? or any way else?
thanks!
kenneth
Short answer is, Yes. Whether you use ajax request or form submission, you will need to have a controller with appropriate mapping url that will process your request further depending on the code your controller contains.
when user click a icon or button in the second page, how does the next controller be selected to run? Is still a URL mapping? or any way else?
yes, because Spring MVC uses url pattern which itself is a concise and simple way for mapping set of urls.
When you click image or link in ur view page, you allow Controller and DispatcherServlet to receive user's request and after that dispatch request to controller. Then MappingHandler will choose which one is right url request that client requesting by following HttpServletRequest mechanism.
public interface HandlerMapping {
HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception;
}
After this Controller will do real work of processing request/view page.
Note that: URL pattern itself follow simple ways to run the correct url by analyzing every character in a pattern which must match the corresponding character in the URL path exactly with two exceptions.
In Spring MVC, you can do either this
Other site
or that
<spring:url value="/othersite"/>
then URL pattern will be matched by controller itself.
I have an Spring-mvc application and in each controller I add a form to SessionAttributes to preserve properties when save, delete or do another get request. Main problem becomes when I try to open some link in another browser tab and try to submit the first one. I tried this solution but when I do a redirect (in controller I only have 1 return for view and the other methods do a redirect) it creates a new conversation and can't find previous one.
I have another question about this triying to use spring-session, question It's here but I don't know if this will work too.
Did you look into Spring's RedirectAttributes? I haven't used it myself but it sounds like it should do what you would like. RedirectAttributes is typically used for GET/redirect/POST patterns and at least one user seems to think passing session attributes this way is bad practice, however they go on to mention there doesn't seem to be a better solution. Anyway, the example shown in the documentation:
#RequestMapping(value = "/accounts", method = RequestMethod.POST)
public String handle(Account account, BindingResult result, RedirectAttributes redirectAttrs) {
if (result.hasErrors()) {
return "accounts/new";
}
// Save account ...
redirectAttrs.addAttribute("id", account.getId()).addFlashAttribute("message", "Account created!");
return "redirect:/accounts/{id}";
}
would add the "message" attribute to a RedirectModel, and if your controller redirects, then whatever method handles the redirect can access that data like so:
#RequestMapping(value = "/accounts", method = RequestMethod.POST)
public String handleRedirect(Model model) {
String message = (String) model.asMap().get("message");
return new ModelAndView();
}
So adding session attributes should be possible in the same way. Another reference here.
EDIT
I was looking through the Spring documentation and they also mention this annotation #SessionAttributes. From the documentation:
The type-level #SessionAttributes annotation declares session attributes used by a specific handler. This will typically list the names of model attributes or types of model attributes which should be transparently stored in the session or some conversational storage, serving as form-backing beans between subsequent requests.
Could this be what you need?
And also a link to documentation on flash attributes.
This is the solution we have come up with, nothing to do with Spring:
On each html form of your application you will have to include a hidden field. Let's name this field CSRF_TOKEN. This field should have a randomly generated value. This value is placed both in the session and the hidden field. The name of the session attribute is SESSION_CSRF_TOKEN
When the form is submitted to the server, you check whether the value in the session (SESSION_CSRF_TOKEN) equals the value sent in the HTTP request parameter CSRF_TOKEN. If not, you show some kind of error message and you stop processing. If they are equal, proceed.
If the user opens a new tab or duplicates a tab, the server will re-render the page and a new CSRF_TOKEN will be generated. So the user will only be able to submit the form from the newly opened tab , and not from the original.
This solution offers an additional bonus: It protects from CSRF attacks.
I have my MVC architecture in my webapplication.Now the flow is as follows:
starter.jsp -> SERVLET ->view.jsp
Now view.jsp expects cetrtain request parameters to be present in request object.
When view.jsp is called using above workflow everything works fine.But if view.jsp is called directly then it does not get expected attributes in request and forwards to error.jsp
Now Will search engine directly call view.jsp? or will it travel from starter.jsp to view.jsp?
Detailed example:
main page has a href to a article.
href ="servlet?id=xyz"
Now servlet gets the id from request.
Servlet gets details for id from DB , puts the Object obtained from DB as request attribute
idDetails
And forwards to view.jsp
View.jsp gets request attribute idDetails
So view.jsp expects idDetails to be present as request attribute
There is no correlation between SEO and MVC. MVC manages the internals of your application while SEO is some external tweak. If they correlate somehow then there is something wrong with your application in my opinion.
If some files are public so the search engine can see them it is likely that they will be indexed. If in doubt you can always check your pages in google for example using the site: prefix.
So the point is that if you can see it google can see it.
I agree with Adam's reply. SEO and MVC architecture are not related at all.
If you have fear like, what happen If "CRAWLERS" will directly access your view.jsp page, then I suggest you to use Filters and Interceptors in your application.
So here your interceptor/filter will intercept the incoming request and filter it out if they are directly made by "Crawler Algorithm" OR "Suspect User".
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 have a Spring Annonted Controller that is used to capture the information from a form and get a list of search results from the database.
Here is the definition of the method
#RequestMapping(method = RequestMethod.POST, params = {SUBMIT_BTN })
public ModelAndView processForm(#ModelAttribute(COMMAND_NAME){
// 1. Load search results using search parameters from form (Contained in a collection of some sort)
// 2. Create the ModelAndView
// 3. Redirect with RequestView or redirect: to generate a GET.
}
I think I need to redirect with redirect: since i have a list of items in a collection store in the session. Cannot add that as a url request param.
Basically I'm trying to prevent problems whith the back button where it says that the page is expired. I want to implement the PRG pattern in strings.
I'm having a hard time wrapping my head around converting the POST into a GET. Can I just redirect or do I need two methods? Thanks for any help you can provide.
The standard pattern is to have a controller method to handle the GET,and which shows the form (or whatever) to the user, and one to handle the POST, which is the form submission. The POST method sends a redirect after it has finished processing the submission, which comes back in to the GET method.
#RequestMapping(value="/myapp", method=GET)
public String showForm(#ModelAttribute(COMMAND_NAME){
return "form.jsp";
}
#RequestMapping(value="/myapp", method=POST)
public String processForm(#ModelAttribute(COMMAND_NAME){
// do stuff to process for submission
return "redirect:/myapp";
}
Returning a view name with the "redirect:" prefix forces Spring to send an HTTP direct rather than an internal request forward.
This is the same pattern that Spring 2.0 implemented with SimpleFormController, but the new way is far more transparent.