Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Struts forms and validation with webflow

Hybrid View

  1. #1
    Join Date
    Apr 2005
    Location
    Edinburgh
    Posts
    18

    Default Struts forms and validation with webflow

    I have numerous Struts forms with validation rules in both validation.xml and in in the validate method of the forms. I would like to reuse these forms and their validation with Web Flow. Is this possible? or do you need to use the binding of objects like in in the birthdate sample? If it is possible please can someone point me in the direction of documentation for configuring it or an example.

    Thanks
    Daniel

  2. #2
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    Hmmmm... I guess I see two options:

    - Use a generic BindingActionForm adapter, like BirthDate does, but have your existing ActionForms be managed by the Spring Web Flow FormAction as a POJO.

    So in that case I could see the "FormAction" being responsible for creating instances of your existing ActionForms you want to use in Spring Web Flows. Binding should work with no change, but you'll likely need to tweak the validate hooks in FormAction to invoke the proper validate method on your Struts action form classes, as by default it'll use a Spring validator if one is configured.

    - Keep your existing ActionForm definition as is, and reference it from your FlowAction definition in struts-config.xml, instead of using the generic "bindingActionForm".

    In that case your action code will need to pull your ActionForm form out of the request. Also, the RequestProcessor will do binding/validation instead of deferring that to the Web Flow system.

    The limitation with this approach is you'll need to define a single FlowAction def per flow (limited to a single form object per flow), where with the first approach you could still have a single FlowAction for the whole app (with perhaps multiple form objects in play over the life of the flow).

    Overall, I'd say currently web flow's Struts integration is most appropriate with Spring-powered data binding and validation, as Spring's solution is just more feature rich, so it's best for use in completely new code you want to write as flows. But I see no reason why we can't make it easier to adapt existing Struts code to the web flow system, particularly if there is a lot of demand for it.
    Keith Donald
    Core Spring Development Team

  3. #3
    Join Date
    Apr 2005
    Location
    Edinburgh
    Posts
    18

    Default

    In this case, I think the second option would be most suitable since I am trying to make use of Web Flow with minimum impact. There will be 2 workflows, both of which will be using the same form throughout, and in many states using the same JSPs (which I also hope will be largely unchanged apart from state tracking fields).

    I am however a little unsure on how to configure this. If I have a form, myForm which extends the Struts ValidatorForm, and an action defined as:
    Code:
    <action path="/MyWorkflow"
            type="org.springframework.web.flow.struts.FlowAction"
            name="myForm" scope="request" 
            validate="false"
            className="org.springframework.web.flow.struts.FlowActionMapping">
            <set-property property="flowId" value="workflow" />
    </action>
    Do I still need to define the following?
    Code:
    <controller processorClass="org.springframework.web.struts.BindingRequestProcessor"/> 
    <plug-in className="org.springframework.web.struts.BindingPlugin"/>
    Daniel

  4. #4
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    No, you don't need it -- if you don't use a BindingActionForm, the BindingRequestProcessor will act just like the standard RequestProcessor. It only acts differently for BindingActionForm, defining form binding and validation. It shouldn't hurt to leave it in though.

    I'm considering adding a StrutsEvent subclass of HttpServletRequestEvent that makes it easier to get the ActionForm for the current request. In that case, to get it in your actions, you can just do this:
    Code:
    &#40;&#40;StrutsEvent&#41;context.getOriginatingEvent&#40;&#41;&#41;.getActionForm&#40;&#41;;
    Keith
    Keith Donald
    Core Spring Development Team

  5. #5
    Join Date
    Apr 2005
    Location
    Edinburgh
    Posts
    18

    Default

    Would anything need to be added to the JSPs if I was to follow this approach? or would the be left intact? (Asking because the struts sample uses the spring:bind tag). The JSPs and validation (using struts forms) are my biggest concern since I use a custom library to generate the forms, validation rules and JSPs from a single xml file, and changing the library is unfortunately not an option.

    The addition of a StrutsEvent would certainly simplify things.

  6. #6
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    No, you should be able to work with your custom tag lib or struts tags with no problems. Birthdate really shouldn't be using spring:bind--I'll change it to be a full struts solution in the JSPs.

    I just committed a StrutsEvent, providing access to the ActionForm and ActionMapping (in addition to all the HttpServletRequestEvent stuff.)
    Keith Donald
    Core Spring Development Team

  7. #7
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    I've just updated BirthDate to be a complete struts solution from the JSP point of view, using the standard struts html tags. It still uses Spring's data binding and validation infrastructure with a BindingActionForm adapter, but it should be easily possibly now to obtain handles to your own ActionForm instances using the StrutsEvent class like:

    Code:
    RequestContext context = ...
    ActionForm myActionForm = &#40;&#40;StrutsEvent&#41;context.getOriginatingEvent&#40;&#41;&#41;.getActionForm&#40;&#41;;
    We'll release PR3 next week to officialy include these fixes and enhancements, but feel free to try in CVS now. In fact, I'd encourage that--we could use the feedback before PR3.

    Cheers,

    Keith
    Keith Donald
    Core Spring Development Team

  8. #8
    Join Date
    Apr 2005
    Location
    Edinburgh
    Posts
    18

    Default

    Thanks for the help.

    I will hopefully get some time this afternoon to try out the code from CVS.

    Daniel

  9. #9
    Join Date
    Apr 2005
    Location
    Edinburgh
    Posts
    18

    Default

    I could well have missed something, but is the following really required in my code? Is there a way to get the normal struts validation to work for action states?
    Code:
     StrutsEvent strutsEvent = &#40;StrutsEvent&#41;context.getOriginatingEvent&#40;&#41;;
            MyForm form = &#40;MyForm&#41;strutsEvent.getActionForm&#40;&#41;;
            ActionMessages errors = form.validate&#40;strutsEvent.getActionMapping&#40;&#41;,strutsEvent.getRequest&#40;&#41;&#41;;
            if&#40;!errors.isEmpty&#40;&#41;&#41;&#123;
                context.getRequestScope&#40;&#41;.setAttribute&#40;Globals.ERROR_KEY,errors&#41;;
                return error&#40;&#41;;
            &#125;
            
            return success&#40;&#41;;
    The action mapping which is a flow action has the validate attribute set to false, since having it true requires an input attribute to be specified. This would be fine if it was at the start of the flow, but the validation is required half way through, so the input is no longer correct.

  10. #10
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    Well, not sure what to say, I guess that's just problamatic with Struts -- as there is one ActionMapping per Action, and only one FlowAction per flow (where a flow has multiple states, any of which might trigger validation.) I don't think you can rely on a flag in the ActionMapping to detrmine whether validation should be performed...
    Keith Donald
    Core Spring Development Team

Similar Threads

  1. Replies: 2
    Last Post: Jul 25th, 2005, 04:25 PM
  2. Struts adapter
    By jocsch in forum Web Flow
    Replies: 1
    Last Post: Apr 17th, 2005, 11:35 AM
  3. Help on 2 forms sharing 1 formModel
    By hszetu in forum Swing
    Replies: 8
    Last Post: Mar 9th, 2005, 03:49 PM
  4. Replies: 4
    Last Post: Jan 21st, 2005, 08:43 AM
  5. Replies: 1
    Last Post: Jan 6th, 2005, 11:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •