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?
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 want to create simple form which will display errors if input is not proper means if validation fails. I am using spring 3.0 annotations.
I did following things
1 : Created JSP
2 : Created Controller
3 : Created DTO
4 : Created org.springframework.validation.Validator
(write an implementation the necessary methods)
int error = bindingResult.getErrorCount() returning the error count and even my page is not being submitted which is expected but my JSP is not showing error messages
I have write on JSP.
Please guide me how to do this.
If i miss on something please let me know i will paste it.
Have a look at this answer for the structure of the controller. The important think is to have a paramter BindingResult and if this binding result contains an error you must return the same view (not redirect) again.
In the jsp code you can use the spring errors tag.
#see Spring Reference chapter 16.2.4.14 The errors tag -- there is an example
I have solved this i have just mentioned dto object name in #ModelAttribute
public void myMethod(#Valid #ModelAttribute**("myDto")** MyDTO myDTO,
BindingResult bindingResult, ActionResponse response,
SessionStatus sessionStatus)
I have ajax requests that come into my controller and my validation is working great. In the controller I call a failure jsp page if there is a failure. The only problem is that I have no idea how I can output the errors to the user on the failure.jsp page. I don't have access to the form tags of spring obviously. What should you do in this scenario?
Edit: All I really want to know is how I can access the binding errors on a JSP page when I'm using an AbstractCommandController.
What I've done in the past is use HTTP headers to send back messages to the AJAX requester (the XMLHTTPRequest object). You will not get a full binding and validation support this way, but it's a simple way to pass messages.
Another option that will give you the full power of Spring binding and validation is as follows. I'm assuming you're submitting a form via AJAX. You could do the standard spring binding and validation, and in the case of an error, send back and replace the form with the exception messages next to the problem input. This way you can leverage the full power of Spring binding and validation while getting the AJAX goodness that you want. This would require you to separate your form into a separate JSP page, so you could just return that form on AJAX submission and error.
In response the comment
My issue is just how to access the
BindingErrors from a JSP if I'm using
an AbstractCommandController. Ajax
isn't really that important in the
equation. I just didn't want to use a
formController because it didn't make
sense.
I think you can simply set a variable in your model like this:
ModelAndView.addObject(this.getCommandName(), errors)
This would be done in AbstractCommandController's
protected abstract ModelAndView handle(
HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors)
throws Exception
method. Be sure the name of the model attribute is the name of your command (set in the setCommandName method).
This is untested and from memory.
You can check the BindException object for errors (and also catch and handle exceptions), and return information about them in your Ajax response. If you're using JSON, you could pair a list of error information with an "errors" key. The front-end would then need to check for and display these errors.
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.
I got this exception in time of running a web application in java. What does this mean?
exception.name = javax.servlet.ServletException: BeanUtils.populate
I guess you are using something which utilizes Jakarta BeanUtils (like Struts) and some method is throwing an exception.
Following may be reasons for same :
The action attribute of an tag must
match exactly the path attribute of
the action definition in the
struts-config.xml file. This is how
Struts associates the ActionForm
bean with the action.
This error usually occurs when you
have specified a form name that does
not exist in your tag. For example,
you specifiec and 'myForm' is not
the name of a form associated with
myAction in the struts-config file
You get this message when Struts is
unable to map the data in the HTML
form to the properties in your
ActionForm bean. Make sure each of
the properties on your bean is
either a String or a boolean. Do you
have any properties of type
java.util.Date or other objects?
That might cause this error. Also
check to see that you have public
getters and setter for each of your
properties.
Check:
http://www.coderanch.com/t/53114/Struts/ServletException-BeanUtils-populate
http://forums.sun.com/thread.jspa?threadID=632599
http://javaexceptions1.blogspot.com/2009/08/javaxservletservletexception.html
A short call to google's famous www-indexer (with:"ServletException: BeanUtils.populate") provided this result:
ServletException BeanUtils populate
The answer to that question over there at coderanch could help to solve your problem
Since this is a Struts related exception (and seeing that we don't know the cause of the exception), here are some possible reasons why you're getting the exception.
No Bean Specified. This means that there is no ActionForm defined in your Action.
Your bean properties you're copying from doesn't match the bean properties you're matching to.
Unless we know the cause of the exception, you'll just have to debug your code and see what is the fault.