Top > SAStrutsあれこれ > 複数Employee一括更新でリセットを利用しよう

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
 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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
-
|
|
!
|
!
|
|
|
-
|
|
|
|
-
|
|
!
|
|
|
|
|
!
|
|
|
|
-
|
!
|
|
|
-
|
!
|
|
|
-
|
|
-
|
-
|
|
!
!
|
|
|
|
!
!
package examples.action;
 
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.seasar.framework.beans.util.Beans;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import examples.dto.DepartmentDto;
import examples.dto.EmployeeDto;
import examples.entity.Department;
import examples.entity.Employee;
import examples.form.EmployeeForm;
import examples.service.DepartmentService;
import examples.service.EmployeeService;
 
public class EmployeeAction {
 
    @ActionForm
    @Resource
    protected EmployeeForm employeeForm;
    
    @Resource
    protected EmployeeService employeeService;
    
    @Resource
    protected DepartmentService departmentService;
    
    public List<Employee> empItems;
    
    //初期一覧画面表示用
    @Execute(validator = false)
    public String index() {
        empItems = employeeService.findAll();
        employeeForm.deptItems = new ArrayList<DepartmentDto>();
        for( Department e : departmentService.findAll() ) {
            DepartmentDto d = Beans.createAndCopy(DepartmentDto.class, e).execute();
            employeeForm.deptItems.add(d);
        }
        return "index.jsp";
    }
    
    //一括更新画面表示用
    @Execute(validator = false)
    public String edit_for_bulk_upd() {
        empItems = employeeService.findAll();
 
        //エンティティ→DTOにコピーします。
        employeeForm.empUpdateItems = new ArrayList<EmployeeDto>();
        for( Employee e : empItems ) {
            EmployeeDto f = Beans.createAndCopy(EmployeeDto.class, e).execute();
            employeeForm.empUpdateItems.add(f);
        }
        
        //1)チェックボックスの初期状態をtrueに設定します。
        employeeForm.initialize();
        
        return "edit_for_bulk_upd.jsp";
    }
    
    //一括更新内容確認画面表示用
    //2)リセットメソッドを呼び出しています。
    @Execute(validate="checkName", input="edit_for_bulk_upd.jsp", reset="resetUpdateCheck")
    public String confirm_for_bulk_upd() {
        return "confirm_for_bulk_upd.jsp";
    }
    
    //一括更新内容確認画面→一括更新画面への戻る
    @Execute(validator = false)
    public String back_to_edit_for_bulk_upd() {
        return "edit_for_bulk_upd.jsp";
    }
    
    //一括更新処理
    @Execute(validator = false)
    public String bulk_upd() {
        
        List<Employee> efs = new ArrayList<Employee>();
        for( EmployeeDto f : employeeForm.empUpdateItems ) {
            //更新対象フラグがtrueのものだけ更新します。
            if( f.update_check ) {
                Employee e = Beans.createAndCopy(Employee.class, f).execute();
                efs.add(e);
            }
        }
        //一括更新
        employeeService.updateBatch(efs);
        employeeForm.empUpdateItems = null;
        return index();
    }
}

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
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
|
|
|
|
|
|
|
|
|
-
|
|
-
-
|
|
|
|
!
|
!
|
!
|
-
-
|
!
!
|
-
-
|
!
!
!
package examples.form;
 
import java.io.Serializable;
import java.util.List;
 
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.seasar.framework.container.annotation.tiger.Component;
import org.seasar.framework.container.annotation.tiger.InstanceType;
import examples.dto.DepartmentDto;
import examples.dto.EmployeeDto;
 
@Component(instance = InstanceType.SESSION)
public class EmployeeForm implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    //複数Employee情報格納用
    public List<EmployeeDto> empUpdateItems;
    
    //部署プルダウン表示用
    public List<DepartmentDto> deptItems;
    
    //EmployeeのNameをチェックします。
    public ActionMessages checkName() {
        ActionMessages errors = new ActionMessages();
        int i=0;
        for( EmployeeDto f : empUpdateItems ) {
            if( f.name.length()==0 ) {
              //第一パラメータには色つきにするタグ要素のname属性の値を設定
                errors.add("empUpdateItems[" + i + "].name", 
                        new ActionMessage("errors.required",
                        new Object[] { "Name (" + (i+1) + "行目)" }));
            }
            i++;
        }
        return errors;
    }
    
    public void resetUpdateCheck() {
        for( EmployeeDto d : empUpdateItems ) {
            d.update_check = false;
        }
    }
    
    public void initialize() {
        for( EmployeeDto d : empUpdateItems ) {
            d.update_check = true;
        }
    }
}
Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
-
-
|
!
!
    public void resetUpdateCheck() {
        for( EmployeeDto d : empUpdateItems ) {
            d.update_check = false;
        }
    }
Everything is expanded.Everything is shortened.
  1
  2
  3
-
|
!
    public int[] updateBatch(List<Employee> emps) {
        return this.jdbcManager.updateBatch(emps).execute();
    }

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
 
 
 
 
-
|
|
|
|
|
|
|
|
|
|
|
|
|
!
package examples.dto;
 
import java.io.Serializable;
 
public class EmployeeDto implements Serializable {
    private static final long serialVersionUID = 1L;
    
    public boolean update_check;
    
    public String id;
    
    public String name;
    
    public String jobType;
    
    public String salary;
    
    public String departmentId;
}

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 
 
 
 
-
|
|
|
|
|
!
package examples.dto;
 
import java.io.Serializable;
 
public class DepartmentDto implements Serializable {
    private static final long serialVersionUID = 1L;
    
    public String id;
    
    public String name;
}

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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
<html>
<head>
  <title>Tutorial: Employee List</title>
</head>
<body>
  <h1>Tutorial: Employee List</h1>
  <table border="1">
    <tr style="background-color:pink">
      <th>Id</th><th>Name</th><th>JobType</th><th>Salary</th><th>Department</th>
    </tr>
    <c:forEach var="e" varStatus="s" items="${empItems}">
    <tr style="background-color:${s.index %2  * 0 ? 'white' : 'yellow'}">
      <td align="right">${f:h(e.id)}</td>
      <td>${f:h(e.name)}</td>
      <td>${f:h(e.jobType)}</td>
      <td align="right"><fmt:formatNumber value="${e.salary}" pattern="#,##0"/></td>
      <td>${f:label(e.departmentId, deptItems, "id", "name")}</td>
    </tr>
    </c:forEach>
  </table>
  <s:form>
    <input type="submit" name="edit_for_bulk_upd" value="一括更新" />
  </s:form>
</body>
</html>

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
 39
 40
 41
 42
 43
 44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
<html>
<head>
<title>Employee Bulk Update</title>
<link rel="stylesheet" type="text/css" href="${f:url('/css/error.css')}" />
</head>
<body>
<h4>一括更新入力</h4>
  <html:errors />
  <s:form>
    <table border="1">
        <tr>
            <th>&nbsp;</th>
            <th>Name</th>
            <th>JobType</th>
            <th>Salary</th>
            <th>Department</th>
        </tr>
        <c:forEach var="empUpdateItems" items="${empUpdateItems}">
            <tr>
                <td><html:checkbox name="empUpdateItems" property="update_check" indexed="true"/></td>
                <td><html:text name="empUpdateItems" property="name" errorStyleClass="err" indexed="true" /></td>
                <td><html:select name="empUpdateItems" property="jobType" indexed="true">
                    <html:option value="CLERK">CLERK</html:option>
                    <html:option value="SALESMAN">SALESMAN</html:option>
                    <html:option value="MANAGER">MANAGER</html:option>
                    <html:option value="ANALYST">ANALYST</html:option>
                    <html:option value="PRESIDENT">PRESIDENT</html:option>
                </html:select></td>
                <td><html:text name="empUpdateItems" property="salary" indexed="true" /></td>
                <td><html:select name="empUpdateItems" property="departmentId" indexed="true">
                    <html:option value=""></html:option>
                    <c:forEach var="d" items="${deptItems}">
                        <html:option value="${f:h(d.id)}">${f:h(d.name)}</html:option>
                    </c:forEach>
                </html:select></td>
            </tr>
        </c:forEach>
    </table>
    <br />
    <input type="submit" name="confirm_for_bulk_upd" value="更新内容確認" />
    <input type="submit" name="index" value="戻る"/>
  </s:form>
  </body>
</html>

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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
<html>
<head>
<title>Employee List</title>
</head>
<body>
<h4>一括更新内容の確認</h4>
<s:form>
    <table border="1">
        <tr style="background-color: pink">
            <th>Name</th>
            <th>JobType</th>
            <th>Salary</th>
            <th>Department Name</th>
        </tr>
        <c:forEach var="e" varStatus="s" items="${empUpdateItems}">
          <c:if test="${e.update_check  * true}">
            <tr>
                <td>${f:h(e.name)}</td>
                <td>${f:h(e.jobType)}</td>
                <td><fmt:formatNumber value="${e.salary}" pattern="#,##0" /></td>
                <td>${f:label(e.departmentId, deptItems, "id", "name")}</td>
            </tr>
          </c:if>
        </c:forEach>
    </table>
    <input type="submit" name="back_to_edit_for_bulk_upd" value="戻る" />
    <input type="submit" name="bulk_upd" value="一括更新" />
</s:form>
</body>
</html>

Everything is expanded.Everything is shortened.
  1
  2
  3
 
 
 
@CHARSET "utf-8";
body { background-color: #EEEEEE; }
input.err { background-color: orange; }


添付ファイル: fileblk_upd_reset_5.jpg 443件 [詳細] fileblk_upd_reset_4.jpg 352件 [詳細] fileblk_upd_reset_3.jpg 360件 [詳細] fileblk_upd_reset_2.jpg 459件 [詳細] fileblk_upd_reset_1.jpg 424件 [詳細]

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