I would think that doing validation in your action would be contra-indicated
as a design matter, if you are doing Struts struts rather than one of the
other struts.

On 12/18/05, Rick R <[EMAIL PROTECTED]> wrote:
>
> On 12/18/05, Josh McDonald <[EMAIL PROTECTED]> wrote:
> >
> > What's the best place to put input validation and possible repair?
>
>
>
> I actually prefer to do the validation checks in my DispatchAction. The
> reason being is that, inevitably, you'll end up with a case where you'll
> have some Lists you'll want to populate on your input JSP that are setup
> before you get to the JSP and if validation fails and you use the struts
> built in approach calling validate="true" in your action mapping, you'll
> end
> up with all kinds of problems when you are brought back to the JSP after
> validation fails (request scoped lists won't be in scope). Unless you use
> some other plans to keep the lists around (Session, reset,etc... all of
> which I don't like much and I talk more about here:
> http://www.reumann.net/struts/articles/request_lists.jsp). I find it saves
> you from headaches down the line if you just do your validation calls from
> your Action class. You can still use the validation framework (commons
> validator stuff that Paul mentions) to set up validation checks if that's
> your pleasure... it's just that the call to invoke those checks ends up
> being initiated from the Action. As an example:
>
> //notice the prep method which will put stuff back in request scope when
> validation fails and would also be used by other dispatch methods in this
> action class
>
> public ActionForward yourActionMethod(ActionMapping mapping, ActionForm
> form, HttpServletRequest request, HttpServletResponse response) throws
> Exception {
>     YourForm yourForm = (YourForm)form;
>     if (validationSuccessful(request, yourForm)) {
>            //do stuff.
>           return mapping.findForward(Constants.SUCCESS);
>       } else {
>           prep(request);
>           return mapping.findForward(Constants.FAILURE);
>       }
> }
>
> private void prep(HttpServletRequest request) {
>       request.setAttribute(Constants.SOME_LIST_NAME,
> service.getSomeKindOfList());
>   }
>
> private boolean validationSuccessful(HttpServletRequest request,
> EmployeeForm form) {
>     boolean isOk = true;
>     ActionMessages errors = new ActionMessages();
>     //do your validation checks...
>     //ie...
>     if (form.getFirstName() == null || form.getFirstName().trim().length()
> == 0) {
>         errors.add("firstName", new ActionMessage("errors.required",
> "First
> Name"));
>     }
>     //or you could even call the form's validate...
>     //errors = form.validate( mapping, request );
>
>     //continue with other checks
>
>     if (!errors.isEmpty()) {
>         saveErrors(request, errors);
>         isOk = false;
>     }
>     return isOk;
>   }
>
> --
> Rick
>
>


--
"You can lead a horse to water but you cannot make it float on its back."
~Dakota Jack~

Reply via email to