Announcement Announcement Module
Collapse
No announcement yet.
Javascript Form Submission - SWF 2 & JSF Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Javascript Form Submission - SWF 2 & JSF

    Hello guys,

    I'm trying to write a page in a flow, that must trigger a form submit at certain events. For each different action, a different view must be presented. So, I`m trying to activate the following javascript code to perform the submission:

    Code:
    function myFormSubmit( eventId ) {
       var formAction = document.myForm.action;
       document.myForm.action = formAction + '&_eventId=' + eventId;
       document.myForm.submit();
    }
    Unfortunatelly, this doesn't triggers the requested transition in my flow. The page doesn't change. Does anyone knows how to deal with this?

    Thanks,
    Alexandre

  • #2
    Does it try and submit the form and just refreshes the same page? The script looks correct to me. Make everything is correct by putting an alert before the last line;

    alert(document.myForm.action);

    Check this is correct. If it is then try typing it into the address bar directly and see if that calls the correct flow.

    Comment


    • #3
      Hi TargetBen,

      I did as you`ve suggested, and the resulting action URL is correct.

      I've tried typing the url, and the result is the same. I did the same to other working transitions in my application, and none of them works when typing the URL. Debuging with firebug, I've seen that when I click on buttons (h:commandButton) that fires transitions, the browser sends a POST request, with 'execution' as it's only parameter. But, the 'Referer' header of the request does contain the '_eventId' parameter.

      I'll try to make a request setting the referer header, and let you know about the results.

      Comment


      • #4
        OK I'm not 100% sure whats going on with it then. Assuming that elements in your form are submitting fine you could try passing it as a input instead of in the url;

        Code:
        function mySubmitThing(eventId)
        {
          var form = document.myform;
          var newInput = document.createElement("input");
          newInput.name = "_eventId";
          newInput.type = "hidden";
          newInput.value = eventId;
          form.addElement(newInput)
          form.submit();
        }
        I haven't tested that code but assuming you know your way around JavaScript you should be able to figure it out!

        Good luck.
        Ben

        Comment


        • #5
          This won't work at the moment, as we don't do anything with the _eventId parameter in the Faces+Web Flow integration. You have to trigger a proper JSF ActionEvent in order to signal an event to Web Flow.

          I think there is a ticket open along these lines, but it will probably be a 3.0.0.M1 feature.

          In the meantime, I've whipped up a quick PhaseListener that will signal an ActionEvent based on the presence of the _eventId parameter. Feel free to try it out:

          Code:
          package org.springframework.faces.support;
          
          import javax.el.ELContext;
          import javax.el.MethodExpression;
          import javax.faces.component.UICommand;
          import javax.faces.context.FacesContext;
          import javax.faces.event.ActionEvent;
          import javax.faces.event.PhaseEvent;
          import javax.faces.event.PhaseId;
          import javax.faces.event.PhaseListener;
          
          import org.springframework.faces.webflow.JsfUtils;
          
          public class EventIdPhaseListener implements PhaseListener {
          
          	public void afterPhase(PhaseEvent event) {
          		if (JsfUtils.isFlowRequest()) {
          			FacesContext context = event.getFacesContext();
          			if (context.getExternalContext().getRequestParameterMap().containsKey("_eventId")) {
          				UICommand eventSource = new UICommand();
          				eventSource.setTransient(true);
          				eventSource.setParent(context.getViewRoot());
          				eventSource.setId("_eventId");
          				String eventId = (String) context.getExternalContext().getRequestParameterMap().get("_eventId");
          				eventSource.setActionExpression(convertEventIdToMethodExpression(context, eventId));
          				context.getViewRoot().queueEvent(new ActionEvent(eventSource));
          			}
          		}
          	}
          
          	public void beforePhase(PhaseEvent event) {
          		// Do Nothing
          	}
          
          	public PhaseId getPhaseId() {
          		return PhaseId.APPLY_REQUEST_VALUES;
          	}
          
          	private MethodExpression convertEventIdToMethodExpression(FacesContext facesContext, String eventId) {
          		ELContext elContext = facesContext.getELContext();
          		return facesContext.getApplication().getExpressionFactory().createMethodExpression(elContext, eventId,
          				String.class, new Class[0]);
          	}
          
          }
          The other option would be to instead just use a hidden <h:commandButton> (you have to hide it via CSS), with some JavaScript to call click on the button.

          Comment


          • #6
            Thanks Jeremy, your solution does work as I wanted.

            The only change I did to your code, was to change the PhaseId of the listener to UPDATE_MODEL_VALUES, so I can work with the values altered in the form. But it does work perfectly now.

            Thank you very much.

            Alexandre

            Comment


            • #7
              For some reason the PhaseListener and _eventId does not work for me, when i use a webflow version > 2.0.7

              Anybody experiencing the same ?

              Comment


              • #8
                Is there another solution in the meantime?

                I use
                • Spring 3.0
                • WebFlow 2.3.0
                • JSF Mojarra 2.0.4 (FCS b09)

                The solution with the EventIdPhaseListener still works fine. I just have to add the "javax.faces.ViewState" parameter to the URL to trigger userevents in JSF 2.

                Comment


                • #9
                  Hi, is the eventID still being ignored in the JSF integration?

                  Comment

                  Working...
                  X