集成 Flex 4.6 和 Coldfusion

Integrating Flex 4.6 and Coldfusion

我在让我的 Flex 前端表单将其数据传递到 Coldfusion 以及随后的 SQL 服务器方面遇到了一些挑战。下面是我的代码:

MXML:

<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
     xmlns:s="library://ns.adobe.com/flex/spark"
     xmlns:mx="library://ns.adobe.com/flex/mx"
     width="764" height="434"
     width.State1="630"  creationComplete="initApp()">
<fx:Style>
    @namespace s "library://ns.adobe.com/flex/spark";
    s|Form#form1 s|FormItem
    {
        skinClass: ClassReference("spark.skins.spark.FormItemSkin");
    }
    s|Form#form1 s|FormHeading
    {
        skinClass: ClassReference("spark.skins.spark.FormHeadingSkin");
    }
    s|Form#form1
    {
        skinClass: ClassReference("spark.skins.spark.FormSkin");
    }
</fx:Style>
<fx:Script>
    <![CDATA[
        import assets.*;
        import assets.comps.accountManagement.userLogin;
        import assets.comps.accountManagement.userRegistration;
        import model.*;
        import mx.collections.ArrayCollection;
        import mx.controls.Alert;
        import mx.events.FlexEvent;
        import mx.events.ValidationResultEvent;
        import mx.managers.PopUpManager;
        import mx.rpc.events.*;
        import mx.rpc.events.FaultEvent;
        import mx.rpc.events.ResultEvent;
        import mx.utils.ObjectUtil;
        import vo.*;

        //public var cfcResponse:String;
        //this gets called when the application is done being created
        //
        [Bindable]
        private function initApp():void{
            //this calls the confirmCFC method in remoting.cfc
            //cfc_ro.confirmCFC(); }
        //Result Event Method maps RO call to Result Event
        //public function loadConfirm(event:ResultEvent):void{
            //this binds the cfcResponse var to the result of the RO call
            //cfcResponse = event.result as String; 
        }


        //Method submits registration information to the DB via the controller Registration.cfc
        //Upon Successful registration, user receives a confirmation of their successful 
        registration.
        protected function createUser():void
        {
            userRegistrationRO.userRegMethod(
                fName.text, 
                lName.text, 
                uName.text, 
                password.text, 
                city.text,
                state.name,
                zip.text,
                email.text, 
                DOB.name,
                secQuestion1.name,
                secQuestion2.name, 
                ans1.text, 
                ans2.text);
        }
        //Method initiated on the Reset button click and clears form fields
        private function _resetForm():void 
        {
            fName.text = "";
            lName.text = "";
            uName.text = "";
            password.text = ""
            city.text = "";
            state.selectedIndex -1;
            email.text = "";
            confirm_email.text = "";

        }
        //Result event for registration 
        private function registrationResultEvent(event:ResultEvent):void{

        }
        //Fault handler for userRegMethod function
        private function registrationFaultHandler(event:FaultEvent):void{
            Alert.show("Your attempt to register was unsuccessful. Please try again. If problem  
          persists, contact Administrator")

        }
        //Registration RO Event Handler
        private function regROHandler(event):void{
            Alert.show("RO connection successful");
        }
        //Registration RO Fault Handler
        private function regROFaultHandler(event:FaultEvent):void{
            Alert.show("Error 100: Remote object call unsuccessful. Unable to submit data. Please  
        contact Administrator at support@blackiceems.com");
        }
        //variable references user registration popup
        private var userRegForm:userRegistration;

        //Registration Event Handler. Notifies user of successful registration by displaying a 
        confirmation message.       
        private function regHandler(event:Event):void
        { Alert.show("Registration Successful!");
            PopUpManager.removePopUp(userRegForm);  }
        //This funtion calls user reg. form 
        private function showRegForm():void {
            //This variable calls the Login Title Windows
            //var userRegForm:userRegistration = new userRegistration as IFlexDisplayObject =
            userRegForm = new userRegistration();
            userRegForm.addEventListener(Event.CLOSE, closeHandler);
            userRegForm.addEventListener("register", regHandler);
            userRegForm.addEventListener("cancel", closeHandler);
            PopUpManager.addPopUp(userRegForm, this, true);
            PopUpManager.centerPopUp(userRegForm);
            //userRegForm.setInitialFocus();
        }
        //Close Registration Event Handler  
        private function closeHandler(event):void
        {Alert.show("You cancelled the login operation", "Login Cancelled");
            PopUpManager.removePopUp(userRegForm);
        }


    ]]>
</fx:Script>

-->

<!--End of Transitions Code Block-->
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->     
    <!--This remote object passes form data from the form into the CFC DAO. Data Validates on client and server side.-->

    <!-- Defines the data model for the form. -->
    <!--<fx:Model id="formInfo">
        <formData>
            <date>
                <month>{monthInput.text}</month>
                <day>{dayInput.text}</day>
                <year>{yearInput.text}</year>
            </date>
            <name>
                <firstName>{fNameInput.text}</firstName>
                <lastName>{lNameInput.text}</lastName>
            </name>
            <phoneNum>{phoneInput.text}</phoneNum>
        </formData>
    </fx:Model>-->
    <!-- Define the validators. -->
    <mx:StringValidator id="fNameValidator" 
                        required="true"
                        source="{fName}" 
                        property="text"
                        minLength="3"
                        maxLength="10"
                        tooShortError="Your entry is too short to be a valid last name."
                        tooLongError="Your entry is too long to be a valid first name."/>
    <mx:StringValidator id="lNameValidator" 
                        required="true"
                        source="{lName}" 
                        property="text"
                        minLength="2"
                        maxLength="12"
                        tooShortError="Your entry is too short to be a valid last name."
                        tooLongError="Your entry is too long for a last name."/>
    <mx:StringValidator id="cityValidator" 
                        required="true" 
                        source="{city}" 
                        property="text"
                        minLength="4"
                        maxLength="15"
                        tooShortError="Your city name is too short to be valid"/>
    <mx:StringValidator id="stateValidator" 
                        required="true" 
                        source="{state}" 
                        property="text"/>
    <mx:ZipCodeValidator id="zipcodeValidator"
                         required="true"
                         source="{zip}"
                         property="text" />
    <mx:StringValidator id="emailValidator" 
                        required="true" 
                        source="{email}" 
                        property="text" 
                        tooLongError="Your email address exceeded the maximum length allowed."
                        tooShortError="Your email address is too short to be a valid email address."/>
    <mx:StringValidator id="confirm_emailValidator" 
                        required="true"
                        source="{confirm_email}" 
                        property="text"/>

    <mx:StringValidator id="userNameValidator" 
                        required="true" 
                        source="{uName}" 
                        property="text" 
                        minLength="6"
                        maxLength="15"
                        tooShortError="Your username must be a minimum of 6 alphanumeric characters."
                        tooLongError="Your username is too long. Usernames must not exceed 15 alphanumberic characters."/>

</fx:Declarations>
<!--Form Layout Elements and text fields contained below-->
<s:TitleWindow height="100%" close="PopUpManager.removePopUp(this);" controlBarVisible="true"
               title="New Member Registration"
               width.p2="100%"
               width.State1="100%">
    <s:controlBarContent>
        <s:Button label="Register"
                  click.p2="createUser();" enabled.p2="true"
                  click.State1="createUser();" enabled.State1="true"/>
        <s:Button label="Reset"
                  click.State1="_resetForm();showRegForm();"/>
    </s:controlBarContent>
    <s:layout.State1>
        <s:ConstraintLayout/>
    </s:layout.State1>
    <!--Form Starts Here-->
    <s:Form id="RegForm" width="100%" height="90%" skinClass="spark.skins.spark.FormSkin">
        <s:SkinnableContainer width="100%" height="350">
            <s:FormItem id="f_Name" includeIn="State1" label="First Name">
                <s:TextInput id="fName" width="100%" displayAsPassword="false" tabEnabled="true"
                             tabFocusEnabled="true" tabIndex="0"/>
            </s:FormItem>
            <s:FormItem id="l_Name" includeIn="State1" x="230" label="Last Name">
                <s:TextInput id="lName" tabEnabled="true" tabFocusEnabled="true" tabIndex="1"/>
            </s:FormItem>
            <s:FormItem id="frm_city" includeIn="State1" x="0" y="53" label="City">
                <s:TextInput id="city" tabIndex="2"/>
            </s:FormItem>
            <s:FormItem id="frm_zip" includeIn="State1" x="371" y="59" width="165"
                        label="Zip Code">
                <s:TextInput id="zip" width="77"/>
            </s:FormItem>
            <s:FormItem id="frm_state" includeIn="State1" x="206" y="55" width="157" height="41"
                        label="State">
                <s:DropDownList id="state" width="100" prompt="Select Date" selectedIndex="-1">
                <s:dataProvider>
                    <s:ArrayList>
                        <fx:String> Alabama</fx:String>
                        <fx:String> Alaska</fx:String>
                        <fx:String> California</fx:String>
                        <fx:String> Colorado</fx:String>
                        <fx:String> Conneticut</fx:String>
                    </s:ArrayList>
                </s:dataProvider>
                </s:DropDownList>
            </s:FormItem>
            <s:FormItem id="frm_email" includeIn="State1" x="0" y="100" width="214" label="Email">
                <s:TextInput id="email" width="152"/>
            </s:FormItem>
            <s:FormItem id="frm_cfirm_email" includeIn="State1" x="257" y="100" width="279"
                        label="Confirm Email">
                <s:TextInput id="confirm_email" width="152"/>
            </s:FormItem>
            <s:FormItem id="frm_username" includeIn="State1" x="1" y="142" width="213"
                        label="Username">
                <s:TextInput id="uName"/>
            </s:FormItem>
            <s:FormItem id="frm_password" includeIn="State1" x="3" y="191" width="215"
                        label="Password">
                <s:TextInput id="password" displayAsPassword="true"/>
            </s:FormItem>
            <s:Button x="0" y="326" label="&gt;&gt; Next"
                      x.p2="0" y.p2="328" width.p2="96" label.p2="&lt;&lt; Previous"
                      click.p2="currentState='State1'"
                      x.State1="0" y.State1="328" click.State1="currentState='p2';initApp();"/>
            <s:FormItem includeIn="p2" x="1" y="10" width="644" label="Security  Question 1:">
                <s:DropDownList id="secQuestion1" width="409" mouseEnabled="false">
                    <s:dataProvider>
                            <s:ArrayList>
                                <fx:String> What was your high school mascot?</fx:String>
                                <fx:String> What is your mother's maiden Name?</fx:String>
                                <fx:String> What was your first car?</fx:String>
                            </s:ArrayList>
                        </s:dataProvider>
                </s:DropDownList>
            </s:FormItem>
            <s:FormItem includeIn="p2" x="1" y="108" width="644" label="Security  Question 2:">
                <s:DropDownList id="secQuestion2" width="409" mouseEnabled="false">
                    <s:dataProvider>
                    <s:ArrayList>
                        <fx:String> Who was your best friend growing up? </fx:String>
                        <fx:String> What city were you born in? </fx:String>
                        <fx:String> What was your first car?</fx:String>
                    </s:ArrayList>
                    </s:dataProvider>
                    </s:DropDownList>
            </s:FormItem>
            <s:FormItem includeIn="p2" x="64" y="58" width="254" label="Answer 1">
                <s:TextInput id="ans1" width="160"/>
            </s:FormItem>
            <s:FormItem includeIn="p2" x="359" y="59" width="286" label="Confirm Answer">
                <s:TextInput width="160"/>
            </s:FormItem>
            <s:FormItem includeIn="p2" x="64" y="159" width="254" label="Answer 1">
                <s:TextInput id="ans2" width="160"/>
            </s:FormItem>
            <s:FormItem includeIn="p2" x="359" y="160" width="286" label="Confirm Answer">
                <s:TextInput width="160"/>
            </s:FormItem>
            <s:FormItem id="frm_DOB" includeIn="State1" x="257" y="153" label="D.O.B.">
                <mx:DateChooser id="DOB" x="183" y="288" width="189" height="146"/>
            </s:FormItem>
            <!--This label displays value of cfcResponse variable when Front-end to CFC remote object call is successful-->
            <!--<s:Label includeIn="State1" x="0" y="240" width="171" height="40" color="#F6F309"
                     text="{cfcResponse}"/>-->
        </s:SkinnableContainer>
    </s:Form>
</s:TitleWindow>

   Coldfusion CFC:

<cffunction name="init" access="public" output="false" returntype="any">
        <cfargument name="DSN" required="true" type="string" hint="datasource" />
        <cfset variables.DSN = arguments.DSN />
        <cfreturn this />
</cffunction>
<!---Flex app CFC Remoting test function--->
<cffunction name="confirmCFC" access="remote" 
returntype="any" 
description="tests the remote object communication between View the controller code" >
<cfreturn "Your Flex Remoting with this CFC is successful."/>
</cffunction>
    <!---User Registraton--->
    <!---Function pulls form values from flex form via Remote Object Call--->
<cffunction name="userRegMethod" access="remote" output="false" returntype="any">
        <cfargument name="fName" type="string" >
        <cfargument name="lName" type="string">
        <cfargument name="uName" type="string" >
        <cfargument name="password" type="string">                  
        <cfargument name="city" type="string">
        <cfargument name="state" type="any" default="CA">
        <cfargument name="zip" type="string">
        <cfargument name="email" type="string">
        <cfargument name="dob" type="any" default="05/20/1982">
        <cfargument name="secQuestion1" type="any" default="Mother's maiden name">
        <cfargument name="secQuestion2" type="any" default="What's your high school mascot?">
        <cfargument name="ans1" type="string" default="Maxine">
        <cfargument name="ans2" type="string" default="Tigers">
        <!---Query Checks for Existing User In DB--->
                <cfquery name="qryUserExistence" datasource="blackiceDS">
                    select email,userName 
                    from tblUserAccount 
                    where email = <cfqueryparam value="#argument.email#" cfsqltype="cf_sql_varchar">
                     OR uName = <cfqueryparam value="#argument.uName#" cfsqltype="cf_sql_varchar">
                </cfquery>
                <!---If No User found by email, record count set to 0--->
        <cfif qryUserExistence.RecordCount eq 0 >

        <!---New User is then created in the table--->
                <cfquery name="qryCreateUser" datasource="BlackIceDS">
                    INSERT INTO  [dbo].tblUserAccount (fName,lName,uName,password, city, state, zip, email, dob,secQuestion1, secQuestion2, ans1, ans2)
                    VALUES (<cfqueryparam value="#fName#" cfsqltype="cf_sql_varchar">,
                    <cfqueryparam value="#lName#" cfsqltype="cf_sql_varchar">,
                    <cfqueryparam value="#uName#" cfsqltype="cf_sql_varchar">,
                    <cfqueryparam value="#password#" cfsqltype="cf_sql_varchar">,
                    <cfqueryparam value="#city#" cfsqltype="cf_sql_varchar">,
                    <cfqueryparam value="#state#" cfsqltype="cf_sql_varchar">,
                    <cfqueryparam value="#zip#" cfsqltype="cf_sql_varchar">,
                    <cfqueryparam value="#email#" cfsqltype="cf_sql_varchar">,
                    <cfqueryparam value="#dob#" cfsqltype="cf_sql_varchar">,
                    <cfqueryparam value="#secQuestion1#" cfsqltype="cf_sqlvarchar">,
                    <cfqueryparam value="#secQuestion2#" cfsqltype="cf_sql_varchar">,
                    <cfqueryparam value="#ans1#" cfsqltype="cf_sql_varchar">,
                    <cfqueryparam value="#ans2#" cfsqltype="cf_sql_varchar">)       
                </cfquery>
    <!---Upon creation of new user account, user is returned a verification welcome message--->

        <cfelse>`enter code here`
<!---Otherwise if an existing account is discovered, user is informed of the    
 existing account and asked to select a different username and password--->
<cfreturn "An existing account was found with the selected username and/or           
password. Please select a different email address."/>
        </cfif>
</cffunction>
    <!---End of User Registraton Functions--->



    </cffunction>

我的表单有一个日期选择器。如何将选定的日期传回 CFC?当我尝试时,出现 Coldfusion 错误,提示 flex 值不是日期类型,因此我将 varchar 作为值类型,但仍然出现错误。我试图解决这个问题,以便我在 Flex 和 CF 之间的远程对象调用验证它是否有效。我已经包含了我的初步代码。如有任何想法,我们将不胜感激。

如果您将日期作为字符串从 flex 发送到 Coldfusion,则将 dob 定义为 Coldfusion 中的字符串:

<cfargument name="dob" type="string">

当你调用远程函数时,我看到你正在传递DOB.name。将 DOB.selectedDate 转换为字符串。您可以使用 getDOBString(DOB.selectedDate) 您可以定义函数如下:

private function getDOBString(date:Date):String{
        var df:DateFormatter = new DateFormatter();
        df.formatString = "MM/DD/YYYY";
        var dateString:String = df.format(date);
        return dateString;
}