SpringMVCとHibernateを使用しています。私には「プロジェクト」と従業員の2つのエンティティがあり、それらは双方向の1対多のマッピングです。データベース内の既存の従業員をプロジェクトに追加することができます。しかし、ユーザーがjspで従業員のフォームフィールドに入力すると、ドロップダウンにすべての従業員の名前が自動的に表示され、従業員が選択されると、他のフィールドが自動的に設定されるようにしたいと思います。どうやってやるの。私は春のmvcとjqueryの初心者です。
プロジェクトに従業員を追加する私のjsp
addEmployee.jsp
<form:form modelAttribute="employeeAttribute" method="POST" action="${Url}">
<table>
<tr>
<td>ProjectId:</td>
<td><input type="text" value="${projectId}" />
</tr>
<tr>
<td><form:label path="employeeId"></form:label></td>
<td><form:input path="employeeId" type="hidden"/></td>
</tr>
<tr>
<td><form:label path="employeeName">Employee Name:</form:label></td>
<td><form:input path="employeeName"/></td>
</tr>
<tr>
<td><form:label path="designation">designation:</form:label></td>
<td><form:input path="designation"/></td>
</tr>
<tr>
<td><form:label path="department">department:</form:label></td>
<td><form:input path="department"/></td>
</tr>
<tr>
<td><form:label path="password">password:</form:label></td>
<td><form:input path="password"/></td>
</tr>
<tr>
</table>
<input type="submit" value="Save" />
</form:form>
フォーム入力後のコントローラーでの対応するアクション
@RequestMapping(value="/add",method = RequestMethod.GET)
public String getAdd(@RequestParam("id")Integer projectId,Model model){
Employee emp = new Employee();
model.addAttribute("projectId",projectId);
model.addAttribute("employeeAttribute",emp);
return "addEmployee";
}
@RequestMapping(value="/add", method = RequestMethod.POST)
public String postAdd(@RequestParam("id")Integer projectId,@ModelAttribute("employeeAttribute")Employee employee) throws IOException{
Employee emp = employeeService.getEmployeeByName(employee.getEmployeeName());
if(emp==null){
return "Error";
}
else{
employeeService.add(projectId,emp);
return "redirect:/project3/list";
}
}
onChange()
を含む選択ボックスにjavascriptを使用するためのイベントを作成user list
できます。ajaxを使用すると、ユーザーIDをコントローラーに送信できます。
そのため、ユーザーに関連付けられている属性をフェッチして、に配置しますrequest
。ajaxが成功したら、データベースからフェッチされた適切な値をフィールドに入力します。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加