Top > SAStrutsあれこれ > 複数一括登録時のバリデーション

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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
-
|
|
!
|
!
|
|
|
-
|
|
|
-
|
!
|
!
|
|
|
-
|
!
|
|
|
-
|
!
|
|
|
-
|
-
|
|
!
|
|
|
!
!
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_add() {
 
        //入力値を設定するための空配列(3レコード分)を作成します。
        employeeForm.empInsertItems = new ArrayList<EmployeeDto>();
        for( int i=0; i<3; i++ ) {
            employeeForm.empInsertItems.add(new EmployeeDto());
        }
        return "edit_for_bulk_add.jsp";
    }
    
    //一括登録内容確認画面表示用
    @Execute(validate="checkName", input="edit_for_bulk_add.jsp")
    public String confirm_for_bulk_add() {
        return "confirm_for_bulk_add.jsp";
    }
    
    //一括登録内容確認画面→一括登録画面への戻る
    @Execute(validator = false)
    public String back_to_edit_for_bulk_add() {
        return "edit_for_bulk_add.jsp";
    }
    
    //一括登録処理
    @Execute(validator = false)
    public String bulk_add() {
        List<Employee> efs = new ArrayList<Employee>();
        for( EmployeeDto e : employeeForm.empInsertItems ) {
            Employee f = Beans.createAndCopy(Employee.class, e).execute();
            efs.add(f);
        }
        employeeService.insertBatch(efs);
        employeeForm.empInsertItems = 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
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
|
|
|
|
|
|
-
|
|
-
-
|
|
|
|
!
|
!
|
!
|
|
|
!
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> empInsertItems;
    
    //EmployeeのNameをチェックします。
    public ActionMessages checkName() {
        ActionMessages errors = new ActionMessages();
        int i=0;
        for( EmployeeDto f : empInsertItems ) {
            if( f.name.length()==0 ) {
              //第一パラメータには色つきにするタグ要素のname属性の値を設定
                errors.add("empInsertItems[" + i + "].name", 
                        new ActionMessage("errors.required",
                        new Object[] { "Name (" + (i+1) + "行目)" }));
            }
            i++;
        }
        return errors;
    }
    
    //部署プルダウン表示用
    public List<DepartmentDto> deptItems;
}
Everything is expanded.Everything is shortened.
  1
  2
  3
  4
 
 
-
|
    ----
    //EmployeeのNameをチェックします。
    public ActionMessages checkName() {
    ----
Everything is expanded.Everything is shortened.
  1
  2
  3
  4
 
 
-
|
    ----
    @Execute(validate="checkName", input="edit_for_bulk_add.jsp")
    public String confirm_for_bulk_add() {
    ----

Everything is expanded.Everything is shortened.
  1
  2
  3
-
|
!
    public int[] insertBatch(List<Employee> emps) {
        return this.jdbcManager.insertBatch(emps).execute();
    }
Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 
 
 
 
-
|
|
|
|
|
|
|
|
|
!
package examples.dto;
 
import java.io.Serializable;
 
public class EmployeeDto implements Serializable {
    private static final long serialVersionUID = 1L;
    
    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
 26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
<%@page pageEncoding="UTF-8" %>
<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_add" 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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
<%@page pageEncoding="UTF-8"%>
<html>
<head>
<title>Employee Bulk Insert</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>Name</th>
            <th>JobType</th>
            <th>Salary</th>
            <th>Department</th>
        </tr>
        <c:forEach var="empInsertItems" items="${empInsertItems}">
            <tr>
                <td><html:text name="empInsertItems" property="name" errorStyleClass="err" indexed="true" /></td>
                <td><html:select name="empInsertItems" 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="empInsertItems" property="salary" indexed="true" /></td>
                <td><html:select name="empInsertItems" 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_add" value="登録内容確認" />
    <input type="submit" name="index" value="戻る"/>
  </s:form>
  </body>
</html>
Everything is expanded.Everything is shortened.
  1
  2
  3
 
 
 
    ----
    <td><html:text name="empInsertItems" property="name" errorStyleClass="err" indexed="true" /></td>
    ----
Everything is expanded.Everything is shortened.
  1
  2
  3
 
 
 
    ----
    <td><input type="text" name="empInsertItems[1].name" value="" class="err"></td>
    ----
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
<%@page pageEncoding="UTF-8"%>
<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="${empInsertItems}">
            <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:forEach>
    </table>
    <input type="submit" name="back_to_edit_for_bulk_add" value="戻る" />
    <input type="submit" name="bulk_add" 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_add_vali_4.jpg 468件 [詳細] fileblk_add_vali_3.jpg 442件 [詳細] fileblk_add_vali_2.jpg 383件 [詳細] fileblk_add_vali_1.jpg 583件 [詳細]

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