Top > SAStrutsあれこれ > Flexで従業員情報を登録しましょう


Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 
 
 
 
 
 
 
 
 
-
|
|
|
|
|
-
|
|
|
|
!
!
package tutorial.action;
 
import java.io.IOException;
import javax.annotation.Resource;
import javax.xml.bind.JAXBException;
import org.seasar.struts.annotation.Execute;
import tutorial.dto.Addresses;
import tutorial.service.AddressService;
import tutorial.util.XmlUtil;
 
public class AddressAction {
 
    @Resource
    protected AddressService addressService;
 
    @Execute(validator = false)
    public String findAll() throws IOException, JAXBException {
        Addresses addresses = new Addresses();
        addresses.addressList = addressService.findAll();
        XmlUtil.sendXML(Addresses.class, addresses);
        return null;
    }
}
Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 
 
 
 
 
 
 
 
 
-
|
|
|
|
|
-
|
|
|
|
!
!
package tutorial.action;
 
import java.io.IOException;
import javax.annotation.Resource;
import javax.xml.bind.JAXBException;
import org.seasar.struts.annotation.Execute;
import tutorial.dto.Departments;
import tutorial.service.DepartmentService;
import tutorial.util.XmlUtil;
 
public class DepartmentAction {
 
    @Resource
    protected DepartmentService departmentService;
 
    @Execute(validator = false)
    public String findAll() throws IOException, JAXBException {
        Departments departments = new Departments();
        departments.departmentList = departmentService.findAll();
        XmlUtil.sendXML(Departments.class, departments);
        return null;
    }
}
Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
|
|
|
|
|
|
|
|
-
|
-
|
|
-
|
|
|
!
|
|
!
!
package tutorial.action;
 
import java.io.IOException;
import javax.annotation.Resource;
import javax.xml.bind.JAXBException;
import org.seasar.framework.beans.util.Beans;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import tutorial.dto.ResponseBase;
import tutorial.entity.Employee;
import tutorial.form.EmployeeForm;
import tutorial.service.EmployeeService;
import tutorial.util.XmlUtil;
 
public class EmployeeAction {
 
    @ActionForm
    @Resource
    protected EmployeeForm employeeForm;
 
    @Resource
    protected EmployeeService employeeService;
 
    @Execute(validator = false)
    public String add() throws JAXBException, IOException {
        ResponseBase rb = new ResponseBase();
        try {
            Employee emp = Beans.createAndCopy(Employee.class, employeeForm).execute();
            employeeService.insert(emp);
        } catch (RuntimeException e) {
            rb.result = ResponseBase.FAULT;
            ////なんか適当な切り方ですが。
            rb.message = e.toString().substring(0, Math.min(e.toString().length(), 600));
        }
        XmlUtil.sendXML(ResponseBase.class, rb);
        return null;
    }
}

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 
 
 
 
 
 
 
 
 
-
|
|
|
|
!
package tutorial.dto;
 
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import tutorial.entity.Address;
 
@XmlRootElement(name="Response")
public class Addresses extends ResponseBase {
    
    @XmlElementWrapper(name="Content")
    @XmlElement(name="Address")
    public List<Address> addressList;
}

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 
 
 
 
 
 
 
 
 
-
|
|
|
|
!
package tutorial.dto;
 
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import tutorial.entity.Department;
 
@XmlRootElement(name="Response")
public class Departments extends ResponseBase {
    
    @XmlElementWrapper(name="Content")
    @XmlElement(name="Department")
    public List<Department> departmentList;
}

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 
 
-
|
|
|
|
|
|
|
|
|
|
!
package tutorial.form;
 
public class EmployeeForm {
    
    public String name;
    
    public String jobType;
    
    public String salary;
    
    public String departmentId;
    
    public String addressId;
}
  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?xml version="1.0" encoding="utf-8"?>
<!-- Simple example to demonstrate Form layout container. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" applicationComplete="init()">
 
    <mx:Script>
        <![CDATA[
            import mx.core.UIComponent;
            import mx.events.ValidationResultEvent;
            import mx.validators.Validator;
            import mx.rpc.http.mxml.HTTPService;
            import mx.utils.ObjectUtil;
            import mx.controls.Alert;
            import mx.utils.URLUtil;
            import mx.rpc.AsyncToken;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            import mx.collections.XMLListCollection;
            import mx.collections.ArrayCollection;
            
            import util.SAStrutsUtils;
 
            //入力フォームの初期表示処理
            private function init():void {
                
                System.useCodePage = true;
                jobType.selectedIndex = -1;
                
                SAStrutsUtils.post2sastruts("department", SAStrutsUtils.REQ_FINDALL, setDepartment, faultHandler);
                SAStrutsUtils.post2sastruts("address", SAStrutsUtils.REQ_FINDALL, setAddress, faultHandler);
            }
            
            //「登録」ボタンクリック時処理
            private function regist():void {
                
                var errors:Array = Validator.validateAll(this.validatorArray);
                if(errors.length  * 0){
                    //データが正常なので登録処理を行います。
                    SAStrutsUtils.post2sastruts("employee", checkModel, registResultHandler, faultHandler);
                } else {
                    //バリデーションエラー時の処理
                    var errorFields:Array = [];
                    var errorMessages:Array = [];
                    for each (var e:ValidationResultEvent in errors) {
                        //エラー対象のプロパティ名格納
                        errorFields.push(e.target.source);
                        //エラーメッセージ格納
                        errorMessages.push(e.message);
                    }
                    for each (var c:UIComponent in [ ename, jobType, salary, departmentId, addressId ]) {
                        var n:int = errorFields.indexOf(c);
                        if (n >= 0) {
                            //エラープロパティを強調(太枠)表示します。
                            c.setStyle("borderStyle", "solid");
                            c.setStyle("borderThickness", 3);
                        } else {
                            c.setStyle("borderStyle", "inset");
                        }
                    }
                }
            } 
           
            // サーバーから受信した部署データをコンボボックスにセット
            private function setDepartment(e:ResultEvent):void {
                departmentId.dataProvider = e.result.Content.Department as XMLList;
                departmentId.selectedIndex = -1;
            }
                        
            // サーバーから受信した住所データをコンボボックスにセット
            private function setAddress(e:ResultEvent):void {
                addressId.dataProvider = e.result.Content.Address as XMLList;
                addressId.selectedIndex = -1;
            }
                       
            private function registResultHandler(e:ResultEvent):void {
                //サーバー側の処理結果をチェックします。
                if( e.result.result  * "success" ) {
                    Alert.show("登録成功です");
                } else {
                    Alert.show("サーバー処理失敗\n\n理由 : " + e.result.message);
                }
            }
            
            // HTTPServiceでエラーが発生した
            private function faultHandler(e:FaultEvent):void {
                Alert.show("HTTPService失敗\n\n理由 : " + e.fault.message);
            }
        ]]>
    </mx:Script>
        
    <!-- 
         フォームのデータをバインディングしてデータモデルとしてまとめます
         タグ名はPOSTデータのデータ名になりアクションフォームのプロパティ
         名と対応します。
    -->
    <mx:Model id="checkModel">
      <root>
        <add>method</add>
        <name>{ename.text}</name>
        <jobType>{jobType.value}</jobType>
        <salary>{salary.text}</salary>
        <departmentId>{departmentId.selectedItem.id}</departmentId>
        <addressId>{addressId.selectedItem.id}</addressId>
      </root>
    </mx:Model>
 
    <!-- 全件検索用POSTデータ -->
    <mx:Model id="findAll">
      <root>
        <findAll>method</findAll>
      </root>
    </mx:Model>
 
    <mx:Panel title="Form Container Example" height="450" width="50%" 
        paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">
 
        <mx:Text width="100%" color="blue"
            text="Moving from one form field to another triggers the validator."/>
 
        <mx:Form width="100%" height="100%" defaultButton="{submit}">
            <mx:FormHeading label="Enter values into the form."/>
 
            <mx:FormItem label="Name" required="true">
                <mx:TextInput id="ename" width="200"/>
            </mx:FormItem>
 
            <mx:FormItem label="JobType" required="true">
                <mx:ComboBox id="jobType" width="200">
                  <mx:dataProvider>
                    <mx:Array>
                      <mx:Object label="CLERK" data="CLERK" />
                      <mx:Object label="SALESMAN" data="SALESMAN" />
                      <mx:Object label="MANAGER" data="MANAGER" />
                    </mx:Array>
                  </mx:dataProvider>
               </mx:ComboBox>
            </mx:FormItem>
 
            <mx:FormItem label="Salary" required="true">
                <mx:TextInput id="salary" width="200"/>
            </mx:FormItem>
 
            <mx:FormItem label="Department" required="true">
                <mx:ComboBox id="departmentId" width="200" labelField="name">
                </mx:ComboBox> 
            </mx:FormItem>
             
            <mx:FormItem label="Address" required="true">
                <mx:ComboBox id="addressId" width="200" labelField="name">
                </mx:ComboBox> 
            </mx:FormItem> 
            
            <mx:FormItem>
                <mx:Button label="登録" id="submit" width="108" click="regist();"/>
            </mx:FormItem>
        </mx:Form>
 
    </mx:Panel>
 
    <!-- 
         フォームデータのバリデーションチェック
         まとめてチェックできるように配列化しておきます。
     -->
    <mx:Array id="validatorArray">
        <mx:StringValidator source="{ename}" property="text" minLength="1" maxLength="32"/>
        <mx:StringValidator source="{jobType}" property="text" />
        <mx:StringValidator source="{departmentId}" property="text" />
        <mx:StringValidator source="{addressId}" property="text" />
        <mx:NumberValidator source="{salary}" property="text" integerError="Enter Integer value"
            minValue="0" domain="int"/>
    </mx:Array>
</mx:Application>

添付ファイル: fileflex_emp_add_3.jpg 321件 [詳細] fileflex_emp_add_2.jpg 302件 [詳細] fileflex_emp_add_1.jpg 297件 [詳細]

リロード   新規 編集 凍結解除 差分 添付 複製 名前変更   ホーム 一覧 単語検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS
Last-modified: 2012-08-08 (水) 23:26:02 (2923d)