aspose file tools*
The moose likes Struts and the fly likes validation annotation in struts-2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "validation annotation in struts-2" Watch "validation annotation in struts-2" New topic
Author

validation annotation in struts-2

Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
In my jsp,I have button...

On click of it I am generating a form with 2 textfields,1select ,OK,Cancel buttons...On OK,Cancel the form dissappears and this is since have enclosed all this in <div> tag....





in Action class...



But the problem is I am unable to validate these text fields in the div tag...what could be the reason? As seen above,I have declared variables with the same name as in div tag in my action class still the problem persists... How does the framework appproach something like this?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What's being sent to the server on submit? Have you checked the actual request?
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
I am not submitting anything as you could see, it's input type="button" !!!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

So you have a form that's not submitting anything? That makes no sense.
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
It's like this...

I have a table with 3 fields...I have "add" button below the table...

on click off Add i show up a form with 3 fields with OK,Cancal buttons...

On 'OK' I am just setting the 3 values entered in that form onto the table row...

I need to validate this form when there is a case the user clicks 'OK' without entering anyting... Hope it's clear now...


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No, it's not clear. If the form is being submitted, it'll be validated: that's why I asked what's actually being sent from the browser to the server.
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
When the form is being submitted an AJax call is being made with the URL containing the 3 values in the form to Action class.From there the values are added to the DB.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

And the problem is that they're not being validated? What makes you say that?
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
I tried adding the form without entering any values in the fields and it's setting null values to the table row instead of validating with the message I had put in the first message of this thread...what could be the reason??
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

How are you trying to display the validation messages?
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Like this...



They would be displayed just below the field . i.e if it is skipped it would display the message in the annotation like "conceptName Field is Required" as shown above...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No, I mean how are you trying to display the errors to the user.
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
As a text below the field that is to be validated.... ie I have 3 fields one below the other.
I entered last 2 fields and 1st is empty then, on button click for form ,a message should be displayed below the 1st field saying that is empty...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You're not understanding what I'm asking: you don't have anything to display any error messages.

If this is an Ajax submission you need to do that manually. How are you doing it?
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
you don't have anything to display any error messages.





In the above one 'message' parameter is my error message... It gets displayed below the field attached to that...

Yes it is an Ajax submission.what should be done thn?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You're saying you *want* it to be displayed below each submission, because right now, it's not.

You'd need to return an object containing any validation messages--they're available in the response JSP, but since you're doing all the Ajax/JavaScript outside of the Struts 2 framework you'd have to build up that object in some way. You haven't even said how you're doing the Ajax submission.

Why not take a look at how "stock" Struts 2 does its validation, learn where the error messages are stored, and figure out how to craft an Ajax response containing those messages?
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
but since you're doing all the Ajax/JavaScript outside of the Struts 2 framework you'd have to build up that object in some way. You haven't even said how you're doing the Ajax submission.


My Ajax call is in my jsp which in struts-2 framework.

You haven't even said how you're doing the Ajax submission.


As described earlier, I have a button 'add' on click produces a form(which is to be validated) containing 3 fields with buttons 'OK' and "Cancel".
On click of 'OK' button this form is submitted using an Ajax call and the 3 values are added to the DB and also to the table on the UI and the form is then hidden.


Why not take a look at how "stock" Struts 2 does its validation, learn where the error messages are stored, and figure out how to craft an Ajax response containing those messages?


------> Any links for this? I have never heard "stock" struts-2 ...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

With WHAT are you making your Ajax call? Magic? Dojo? jQuery? Prototype?

"Stock" Struts 2 means default Struts 2. Struts 2 has Ajax validation built in. You're not using it. So you have to tell us how you are doing it, otherwise I don't see how anybody can help.
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
With WHAT are you making your Ajax call?


Iam not using any Javascript framework... I am making the call in a normal Javascript function.

Struts 2 has Ajax validation built in


I would check this one out...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Okay, so in your "normal" JavaScript function, you'd have to deal with the data returned by the Ajax submit. And in your action's result, you'd have to craft a meaningful response. Since you want error messages to be associated with fields, it'd probably be easiest to return a JSON object containing field names mapped to their associated errors. Then in your Ajax response method you'd need to display the error messages near their associated fields.

All that would be a *lot* easier if you'd use a JavaScript framework.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: validation annotation in struts-2