做了一個添加信息的demo,用了SpringMVC的form表單
//有一部分?jǐn)?shù)據(jù)需要回顯
<a?href="emp">Add?New?Employee</a>
//對應(yīng)回顯的控制器的方法
@RequestMapping(value="/emp",method?=?RequestMethod.GET)
public?String?getDepts(Map<String,?Object>?map)?{
????map.put("departments",?departmentDao.getDepartments());
????map.put("employee",?new?Employee());
????return?"add";
}
//添加信息的表單
<center>
????<h4>添加員工</h4>
????<form:form?action="emp"?method="POST"?modelAttribute="employee">
????????<!--?path屬性值對應(yīng)HTML表單標(biāo)簽的name屬性值?-->
????????Name:<form:input?path="name"/>
????????<br><br>
????????Email:<form:input?path="email"/>
????????<br><br>
????????<%
????????????Map<String,String>?genders?=?new?HashMap<String,String>();
????????????genders.put("0",?"Male");
????????????genders.put("1",?"Female");
????????????request.setAttribute("genders",?genders);
????????%>
????????Gender:<form:radiobuttons?path="gender"?items="${genders}"/>
????????<br><br>
????????Department:<form:select?path="dept"?items="${departments}"?itemLabel="name"?
????????????????????????itemValue="id"></form:select>
????????<br><br>
????????<input?type="submit"?value="點(diǎn)擊提交">
????</form:form>
</center>
//對應(yīng)添加信息控制器的方法
@RequestMapping(value="/emp",method?=?RequestMethod.POST)
public?String?addEmp(Employee?employee)?{
????employeeDao.save(employee);
????return?"redirect:/emps";
}
添加回答
舉報
0/150
提交
取消