I'm using Spring 3 and Eclipse. The weirdest thing happened, when I was trying to make a form on index page: all I got is neither plain target or binding result for bean name (commandname) error...but when I copy EXACTLY THE SAME form to other .jsp-file and THE SAME controller method, it works fine! Can anyone tell what it is?
The request from your "index page" is obviously bypass somehow your spring front controller.
Related
Im able to add validation to page with single form and it works great. Now, on my page i have 2 forms.
Filling first one and submitting redirects us to one page, filling seconds redirects to another page. When i add validation only for one form it works, when i do same for another, now both are throwing exception.
Thats my controller class:
https://i.stack.imgur.com/A8jWe.png
Thats my form code:
https://i.stack.imgur.com/d9CQP.png
the second form is just same but instead of th:object="${planPreferencesDTO}" it has planPreferences
The error im getting while subbmitting first form:
Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'preferencesDTO' available as request attribute
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'preferencesDTO' available as request attribute
Submitting second form throws same exception bit with planPreferencesDTO instead of preferencesDTO in stack trace
The dtos have #Data annotation from lombok.
It looks like a problem with model and binding result, but i wasn't able to find the solution for many hours, does anyone know what is wrong here?
I want to get parameter from POST method in Spring with name that starts with "value". And it can be like "value_345465". Any ideas?
There is nothing Spring MVC specific. You can use ServletRequest.getParameterNames(), ServletRequest.getParameterMap(), and ServletRequest.getParameter() methods to accomplish this task.
In an enterprise application, I'm trying to set some attributes in an EJB3 interceptor and then show them on a jsp. Is it possible? How can I achieve that? The use case is as follows- Some values are read from the database which are required to be shown on the error page. This error page is the default error page, no controller for it. I couldn't find anything in the code regarding this error page, nothing in web.xml. Need a solution for this asap.
I couldn't find a way to use an interceptor. I ended up using a filter, which was already there in the application. I set an application scope variable map in the init method of the Filter. That ways, I had to get and set the values only once, i.e., at the server startup. Then I used this map in the JSP to read the values. Like, ${applicationScope.appParams.someKey}.
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.