aspose file tools*
The moose likes Struts and the fly likes Validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Validation" Watch "Validation" New topic
Author

Validation

A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Hi again all. I have been able to incorporate most of the error logging that I wanted for my application except for the login page. As it stands right now if the person logging in does not include a username or a password the application works fine. (Giving an error stating the missing field is required). The problem for me is when they supply both, I don't know how to display an error message saying that username and password are incorrect. Any thoughts or suggestions ? The ugly kludge that I could get to work is to redirect to a login page with the error message hard coded in, but that can't be the only way to accomplish this.

For the username and password I am using a validation file that has both the username and password values listed as required. I have also created a third field which I was trying to use as a control starting it out as null and then if the username and password check out setting that value to anything, which I thought would allow it to pass. Any better suggestions of how to do this ?
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
How are you validating that the username and password are correct? Is this something you are doing in your code (maybe by checking a database table)? What I do for business layer validation is to throw a custom exception (ValidationFailedException) from the business layer. In my custom base action class I catch this exception, create an ActionError and then forward to the input defined on the mapping.

- Brent
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Yes I am checking it against a database table. I will look into seeing what you have suggested.

thanks again for the input.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
If it helps, here is a code example. I tweaked it a little to get rid of some project specific code, but I think it should work as posted.

At the end of the execute method in my base class I have the following catch block:


And here is the code in my processError method:



- Brent
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
I am finally in a position where I can work on this again, and I have a few questions in regard to what brent was kind enough to post

1) Will what brent has posted interfere with using the validator on my
LoginForm ?

2) There isn't any possible manner to do this using the LoginForm ? (It seems to me it would be better to have all of the error handling in one space, as I already have a check for username and password being null. (Please inform me if this is a bad way of thinking).
I was thinking of having a variable in the LoginForm that is only set if the username and password are valid, but I can't seem to get the form to validate after this has been set.
After doing more reading would it not be possible to use valid when to check to see if both username and password are not null,and have a value stored in a variable in the form that would be able to tell me if I had the correct username and password ?



3) I am doing reading on the base class, but I am not sure where that is supposed to be placed.
4) I am guessing processforward is placed within the LoginAction page, and is used only in this case ?


Thanks again for all the help to this point. Time for me to go back to the struts documentation and figure out where everything should be placed.

*edited to add another thought.
[ February 27, 2007: Message edited by: A knibbs ]
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I have been involved with 3 or 4 Struts based projects and all of them have interjected a common base class for actions, though I am sure there are plenty of projects that do not use this approach. The basic idea is that you create a base class that extends Action. Your base class implements the execute method (I have it declared final). In your base class' execute method you perform common processing (stuff like checking that the user is valid, retrieving data needed for common tiles, logging, and exception handling). Your execute method would then call an abstract method with a name like doExecute (you can pick whatever name you want). Now all your action classes would extend your base class instead of Action.

Now to your questions:
1) The action would only be called if the initial validation performed by the validator framework passed, so I don't see how it would interfere.
2) I am not sure what you are asking. You could probably perform this validation in the form's validate method but I would rather not call business method or make database calls from my form.
3) see above
4) Do you mean processError? This method exists in my common action base class.

- Brent
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Originally posted by Brent Sterling:
I have been involved with 3 or 4 Struts based projects and all of them have interjected a common base class for actions, though I am sure there are plenty of projects that do not use this approach. The basic idea is that you create a base class that extends Action. Your base class implements the execute method (I have it declared final). In your base class' execute method you perform common processing (stuff like checking that the user is valid, retrieving data needed for common tiles, logging, and exception handling). Your execute method would then call an abstract method with a name like doExecute (you can pick whatever name you want). Now all your action classes would extend your base class instead of Action.

Now to your questions:
1) The action would only be called if the initial validation performed by the validator framework passed, so I don't see how it would interfere.
2) I am not sure what you are asking. You could probably perform this validation in the form's validate method but I would rather not call business method or make database calls from my form.
3) see above
4) Do you mean processError? This method exists in my common action base class.

- Brent

1) That makes sense, just wanted to make sure that I wasn't going to have to change all of the code that I had to this point in order to implement this.

2) I can understand that, I am trying to see what all of the options available to me are. As it stands right now the call would be to a DAO that does the database call seperately. (Hopefully I am using the right terms here).

3 I vaguely understand but at least now I have a few search terms that I can use to fill in the gaps that I have.
4) Yes sorry that was a case of writing while trying to read code, and accidentally writing what I saw.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validation