If I am at http://foo.bar?fooId=123 and the validation fails (validate method in action class) I get redirected to http://foo.bar without the fooId param. This causes form fields to lose their values. Any ideas why this is happening?
You are using GET to send your parameters, server on validation failure is sending you the same input page, instead of GET use POST and everything should work fine.
Your configuration would look like this :
<result name="input">inputPage.jsp</result> which discards any parameter that you submitted.
Also note as per HTML specification GET shouldn't be called if operation will result in any updatation, instead POST should be used.
Related
I want to add internationalization support to Spring project that I'm working on. It works when I add "lang" parameter at the end of the url like;
localhost:8080/someurl?lang=en
However, I can not generate the parameter at the end of the url, I need to make a parameter request from the controller.
I don't think it is a good idea to request lang parameter in each controller. I believe there is a better way to implement but I don't know what it is.
Do you have any suggestion where should I look for it?
Edit: I have a langKey field for the entity User so, I want to generate lang parameter by using the field langKey of the User.
I realized that applying the lang parameter only once is sufficient for the rest of the session so, there is no need to add lang parameter in each controller. It was enough for me to only add the lang parameter for the redirection after login according to users langKey. Then the rest of the session displayed according to language choice of the user stored in database.
I hope that helps for the others.
EDIT: changed file name to 'follow-up.jsp' for clarity.
This is a shorter, and hopefully better version of a longer question I posted earlier today (link: Java web development: transfer control from one servlet to another while passing the request object).
I have a form that is POST-ed upon submit. In the corresponding servlet to which the POST action is done, I want to show either the form page again or a follow-up page, depending on whether validation succeeds. BOTH the form page AND the follow-up page need the form data submitted by the user; the form page needs it to re-populate the form fields, and the follow-up page needs it to show some of this data (for example, to have the user double-check that it is correct). Therefore, I want the request object containing the submitted data to be available to BOTH pages.
Forwarding back to the form is not a problem:
RequestDispatcher rd =
getServletContext().getRequestDispatcher("/form.jsp");
rd.forward(request, response);
But how do I reach the follow-up page (say, follow-up.jsp) in such a way that the request object containing the submitted form data is still accessible in that follow-up page?
The following doesn’t seem to work:
response.sendRedirect("/MyProject/follow-up.jsp");
This is because if “name” is a form parameter that was assigned to an attribute of the request object in the form handling servlet, with this attribute conveniently also being called "name", then the following line in follow-up.jsp:
Your name is: ${name}.
does NOT print the user-submitted name (instead it prints nothing, i.e. only “Your name is:”). In other words, the attributes of the original request object are no longer available in follow-up.jsp. This makes sense in that response.sendRedirect triggers a new HTTP-request (with its own request and response objects). But is not what I want.
Another (secondary) concern is that sendRedirect is unable to issue POST-requests (it only uses GET; see [link] response.sendRedirect() from Servlet to JSP does not seem to work).
EDIT: Also, I realise I could actually use:
rd.forward(request, response);
for BOTH requests. However, that would result in both pages having the same URL showing in the address bar (i.e. the name of the routing servlet, like "...../Register"). And that is definitely bad, because the form page and the follow-up are completely different pages; the follow-up page has little to do with the form (except for displaying some of the submitted form data).
Thanks.
In this case I believe you need to store the data not in request, but in a conversation scope....or in the session scope...regarding redirection not supporting post...then ideally all redirections by definition should be get requests only.....if you want to redirect using a a POST request please re-evaluate your approach...
I have a web application written in Java which uses Struts 1.0. Sometimes when a URL is fired, I can see that it one of the request parameters does not have any name and value like the following ...
http://www.aaa.com/test.do?a=1&b=2&=&d=4&e=5
As can be seen, there is a '&=' which is essentially a parameter with no name and value. I'd like to remove this part from the URL before sending the request to the server. How can I achieve this? Should I use a filter or is there an easier way?
I have this in my applcation routes file:
GET /new Tweets.create
POST /new Tweets.save
And in my view I'm creating a form like this:
#{form #save()}
...
#{/form}
But once is submit the form it's sending me to /tweets/save and not to /new. Any ideas how I can fix this? Thanks!
If you have already tried the route below (which is the correct way to use routes)
#{form #Tweets.save()}
and this did not work, I think you may have put your route in the wrong place. Make sure it is at the top of the routes file, and not after the catch-all route. The routes file is processed in order, so if the catch-all is found, this is used first and your other route is ignored. The catch-all looks like
* /{controller}/{action} {controller}.{action}
Try using
#{form #Tweets.save()}
I think it is suggested to use class names with method names.
EDIT:
The way the play framework routing works is you define some route as
GET /clients Clients.index
If a request encountered with URI /clients then it will be intercepted to Clients.index(). If you have another routing such that
GET /clients Clients.save
Then the framework ignores this routing because /clients aready has a mapping. (Most probably it is giving some error in the console or logging stream, check your logs.)
Therefore you can't make it work like that. I see, you request a reverse mapping that will return the same URI for different methods. However the framework aims to intercept requests so that it will simply ignore your second routing.
Try to separate pages. Most probably what you want is to render the same views for two functions. You can do that without redirecting them to the same URI.
I think (if I did not misread) that the issue is you expecting the wrong behavior.
As I understand you expect that the submit will go to Tweet.save() (POST method) and then back to Tweet.create() (GET method) as both share the same path (/new).
In reality Play is calling Tweet.save() and it expects a render at the end of Tweet.save() to display some result. If you want to redirect to Tweet.create() you can do a call to that method at the end of the implementation of Tweet.save(), with either:
create(<params>);
or
render("#create", <params>);
and that should redirect (via 302) to the GET version.
I have a query form that I would like to submit as a GET request so the result page may be bookmarked and otherwise RESTful. It's your classical text field with a submit button. How do I induce Seam/JSF to use GET and include the query expression as a parameter rather than POST, the default?
All you need to do is enable the SeamFilter in web.xml. See Blog Example for an example RESTful application using Seam. The key is to use a Seam page parameter, defined in WEB-INF/pages.xml
you can use a PhaseListener to convert POST requests to GET requests or just to interpret GET requests so that they can be bookmarkable.
This page should explain in more detail:
http://balusc.blogspot.com/2007/03/post-redirect-get-pattern.html
If you are using s:button or s:link, your form will be using GET method.