Play框架:如何在错误请求期间再次访问我提交的表单,我的代码无法再次访问列表

时间:2018-08-15 06:06:32

标签: java eclipse playframework-2.1

index.scala.html

@(form: Form[applicationmodel.ApplicationModel])
@if(form.hasErrors) {
@for((key, value) <- form.errors) {
     <p>@value(0).message</p>
}
} else {
<form action="/search" method="GET">
            <label class="search-label">ID</label>
            <input type="text" name="empId" id="empId" value="@(form.get().empId)"/>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <label class="search-label">HIRE DATE</label>
            <input type="text" name="hireDateFrom" id="hireDateFrom" value="@(form.get().hireDateFrom)" autocomplete="off"/>
                <div id="curly-dash">~</div>
            <input type="text" name="hireDateTo" id="hireDateTo" value="@(form.get().hireDateTo)" autocomplete="off"/>
                <br>
            <label class="search-label">FIRST NAME</label>
            <input type="text" name="firstName" id="firstName" value="@(form.get().firstName)"/>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <label class="search-label">MANAGER</label>
            <input type="text" name="managerName" id="managerName" value="@(form.get().managerName)"/>
                <br>
            <div>
                <label class="search-label">LAST NAME</label>
                <input type="text" name="lastName" id="lastName" value="@(form.get().lastName)"/>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input type="image" src="@routes.Assets.at("images/BtnSearch.jpg")">
            </div>
        </form>
    </div>
    <form action="/addEdit" method="POST" id="list-form">
        <table id="table-list">
            <caption class="position-relative">
                <img src="@routes.Assets.at("images/Search.jpg")" id="btn-search" class="btn-size">
                <h1 class="inline-block">Employee Records</h1>
                <img src="@routes.Assets.at("images/Add.jpg")" id="btn-add" class="btn-size">
            </caption>
            <thead>
                <tr>
                    <th class="solid-green-border column-title">EMPLOYEE_ID</th>
                    <th class="solid-green-border column-title">FIRST NAME</th>
                    <th class="solid-green-border column-title">LAST NAME</th>
                    <th class="solid-green-border column-title">EMAIL</th>
                    <th class="solid-green-border column-title">PHONE NUMBER</th>
                    <th class="solid-green-border column-title">HIRE DATE</th>
                    <th class="solid-green-border column-title">JOB</th>
                    <th class="solid-green-border column-title">SALARY</th>
                    <th class="solid-green-border column-title">COMMISSION_PCT</th>
                    <th class="solid-green-border column-title">DEPARTMENT</th>
                    <th class="solid-green-border column-title">MANAGER</th>
                    <th class="solid-green-border column-title">ACTION</th>
                </tr>
            </thead>
            <tbody>
                @for(item <- form.get().emplist) {
                <tr>
                    <td class="solid-green-border column-content">@item.employeeId</td>
                    <td class="solid-green-border column-content">@item.firstName</td>
                    <td class="solid-green-border column-content">@item.lastName</td>
                    <td class="solid-green-border column-content">@item.email</td>
                    <td class="solid-green-border column-content">@item.phoneNo</td>
                    <td class="solid-green-border column-content">
                        <div class="position-relative">
                            <span class="class-hiredate">@item.displayHiredate</span>
                            <input type="image" src="/assets/images/imgDate.jpg" class="icon-size date" disabled>
                        </div>
                    </td>
                    <td class="solid-green-border column-content">@item.job.jobTitle</td>
                    <td class="solid-green-border column-content">@item.displaySalary</td>
                    <td class="solid-green-border column-content">@item.commission</td>
                    <td class="solid-green-border column-content department">@item.deptName</td>
                    <td class="solid-green-border column-content">@item.managerName</td>
                    <td class="solid-green-border column-content">
                        <input type="image" src="@routes.Assets.at("images/Edit.jpg")" class="icon-size edit"></td>
                </tr>
                }
            </tbody>
        </table>
         <input type="image" src="@routes.Assets.at("images/BtnSave.jpg")"  id="btn-save">
        </form>
}

在错误请求期间应使用什么语法再次访问该表单并获得该emplist,无论何时在错误请求中,form.get()都没有用,似乎表单本身在请求期间不一样索引

控制器 索引

@Transactional
public static Result index() {
System.out.print("asdf");
appModel = new ApplicationModel();
Form<ApplicationModel> form = 
Form.form(ApplicationModel.class).fill(appModel.init(appModel));
return ok(index.render(form));
}

控制器 提交后,我希望再次显示所有内容,但当我错误提交时,我现在只能显示错误而不能保留其值

@Transactional
public static Result addEdit() {
Form<ApplicationModel> form = 
Form.form(ApplicationModel.class).bindFromRequest();
if (form.hasErrors()) {
  return badRequest(index.render(form));
}
form.get().update();
ApplicationModel newAppModel = new ApplicationModel();
form = Form.form(ApplicationModel.class).fill(newAppModel.init(newAppModel));
return ok(index.render(form));
}

1 个答案:

答案 0 :(得分:0)

解决方案:经过多次尝试和错误

将其括起来并访问您想要获得的特定值

@for(i <- 0 to {form("emplist").indexes.size.toInt} - 1) {
}

*我个人希望这将对使用游戏框架的后代有所帮助,以帮助他们通过迭代来访问表单值:) *