integrate Adobe flex flash with struts 2 action json

integrate Adobe flex flash with struts 2 action json

Postby haretu » Sun Jan 16, 2011 10:43 pm

Using HttpService is the convenient way to integrate Adobe Flex and Struts 2. I will show you how you can create a login form in Adobe Flex which calls a struts 2 action to to authenticate. The example will show how adobe flex (flash) call struts 2 action and get JSON result.

Step 1

Create the Login form in mxml:
Code: Select all
      <mx:Form  id="loginform">
         <mx:FormItem label="Email:" >
            <mx:TextInput id="sname"  editable="true" enabled="true" >
         </mx:FormItem>
         <mx:FormItem label="Password:">
            <mx:TextInput id="password" displayAsPassword="true" editable="true" enabled="true" />
         </mx:FormItem>
         <mx:FormItem>
         <mx:Button label="Submit" id="submit" enabled="true" click="loginAction();"  enterState="loginAction();" focusEnabled="true"/>
         </mx:FormItem>

      </mx:Form>


2 fields in the form: password and sname. The submit button also accepts "Enter" key as click through "enterState".

Step 2

define Httpservice "login" which points to Struts 2 action
Code: Select all
   <mx:HTTPService
      id="login"
      resultFormat="text"
      url="http://localhost:8080/myflex/Login.action"
       method="POST" result="onReturn(event)"/>




Step 3

Add loginAction() which call Httpservice and send sname and password to Struts 2 action.
Code: Select all
public function loginAction():void
         {   
            var sname:String =StringUtil.trim( sname.text);
            var password:String = StringUtil.trim(password.text);
            
            login.send({name:sname,password:password});
   
         }


Step 4

Struts 2 action defined in struts.xml
Code: Select all
     <action name="Login" method="Login" class="SampleAction">
                <result name="success" type="json"/>
                <result name="error" type="json"/>
             </action>



Step 5

Struts 2 Login action in Java
Code: Select all

public class SampleAction  {
    private boolean success=false;
   
    private String name=null;
    private String password=null;           
    private String message=null;
public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }



   public String getPassword() {
      return password;
   }

   public void setPassword(String password) {
      this.password = password;
   }
public boolean isSuccess() {
      return success;
   }

   public void setSuccess(boolean success) {
      this.success = success;
   }

   public String getMessage() {
      return message;
   }

   public void setMessage(String message) {
      this.message = message;
   }
public String Login()
    {
if (checkUser() )    // checkUser is your logic
   return SUCCESS;
else
{message="error login";
    return ERROR;
}

}


Step 6

Create OnReturn function in your mxml file
Code: Select all
private function onReturn(event:ResultEvent):void
         {
              var rawData:String = String(event.result);
      
            var raw =    JSON.decode(rawData);
   var success:Boolean = raw.success;
       if(success)
          Alert.show("Success");
       else
          Alert.show(raw.message);
}



First need to decode rawdata to json format. Through decoded json string, we may get those data field like "success", "message".

Hope it helps.
haretu
 
Posts: 132
Joined: Sat Jan 08, 2011 9:56 pm

Return to Adobe FLEX Flash

Who is online

Users browsing this forum: No registered users and 1 guest

cron