1、從后台傳數據到頁面並在select中被選中
Java controller類代碼:
package cn.mg39.ssm.controller; import java.util.List; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import cn.mg39.ssm.entity.EmpDepart; import cn.mg39.ssm.entity.EmpEmployee; import cn.mg39.ssm.entity.SysRoler; import cn.mg39.ssm.service.EmpDepartService; import cn.mg39.ssm.service.EmpEmployeeService; import cn.mg39.ssm.service.SysRolerService; @Controller @RequestMapping("/empEmployee") public class EmpEmployeeController { @Autowired private EmpEmployeeService empEmployeeService; //員工表 @Autowired private EmpDepartService empDepartService; //部門表 @Autowired private SysRolerService sysRolerService; //角色表 String path = "/pages/empEmployees"; //跳轉的路徑(resource/templates) /** * 數據顯示頁面 * @param model 存分頁和員工表中查詢到的數據 * @param pageNum 當前頁碼 * @param pageSize 分頁時每頁顯示數據的條數 * @param session 用於存角色表和部門表中查詢到的數據 * @return */ @RequestMapping("/list") public String list(Model model,Integer pageNum,Integer pageSize,HttpSession session) { // if (pageNum == null) { pageNum = 1; } if (pageSize == null) { pageSize = 5; } PageHelper.startPage(pageNum, pageSize); List<EmpEmployee> list = empEmployeeService.findAll(); Page<EmpEmployee> pages = (Page<EmpEmployee>) list; List<SysRoler> findRolerAll = sysRolerService.findAll(); session.setAttribute("findRolerAll", findRolerAll); List<EmpDepart> findEmpDepartAll = empDepartService.findAll(); session.setAttribute("findEmpDepartAll", findEmpDepartAll); model.addAttribute("pages", pages); model.addAttribute("findEmployeeAll", list); return path + "/list"; } /** * * @param empEmployee 需要需改的數據(里面有id),通過id找到需要修改的數據 * @param model 用於存通過id查詢到的員工表的數據 * @return 跳轉到修改頁面 */ @RequestMapping("/updateUrl") public String updateUrl(EmpEmployee empEmployee,Model model) { EmpEmployee emp = empEmployeeService.findById(empEmployee); model.addAttribute("findById", emp); return path + "/edit"; } /** * * @param empEmployee 修改頁面傳進來的修改過后的數據,通過service的修改方法作為參數傳到數據庫 * @return 成功則跳轉到數據顯示頁面,失敗到失敗頁面 */ @RequestMapping("/update") public String update(EmpEmployee empEmployee) { if (empEmployeeService.modify(empEmployee)) { return "forward:/empEmployee/list"; }else { return "error"; } } /** * 點擊新增數據按鈕時的過度頁面 * @return 跳轉到怎加數據的頁面 */ @RequestMapping("/saveUrl") public String saveUrl() { return path + "/save"; } /** * * @param empEmployee 從增加頁面獲取到的數據 * @return 成功失敗的跳轉 */ @RequestMapping("/add") public String add(EmpEmployee empEmployee) { if (empEmployeeService.add(empEmployee)) { return "forward:/empEmployee/list"; }else { return "error"; } } /** * 刪除數據 * @param empEmployee 里面有id用於通過id刪除數據 * @return */ @RequestMapping("/remove") public String remove(EmpEmployee empEmployee) { if (empEmployeeService.remove(empEmployee)) { return "forward:/empEmployee/list"; }else { return "error"; } } }
HTML代碼(修改數據頁面):
<div class="row"> <div class="col-md-6 margin-bottom-15"> <label>所屬部門</label> <p class="form-control-static" id="username"> <!--select中的name值為實體類中的外鍵(外鍵的表的實體類.屬性名) --> <select class="form-control" name="empDepart.id"> <!--option中循環遍歷外鍵表中取到的session、value為主鍵 --> <option th:each="fdAll:${session.findEmpDepartAll}" th:value="${fdAll.id}" th:selected="${findById.empDepart.id eq fdAll.id}" th:text="${fdAll.name}"> </select> <!--th:selected="${findById.empDepart.id eq fdAll.id}"表示model值中的外鍵的id和session中的id相比較, 如果兩個id相同,就選中 --> </p> </div> </div>
2、在頁面中自己選數據,並傳回后台
HTML代碼(新增數據頁面):和修改數據相同,只是少一句選中的代碼,因為新增需要自己選外鍵
<div class="row"> <div class="col-md-6 margin-bottom-15"> <label>所屬部門</label> <p class="form-control-static" id="username"> <select class="form-control" name="empDepart.id"> <option th:each="fdAll:${session.findEmpDepartAll}" th:value="${fdAll.id}" th:text="${fdAll.name}"> </select> <!-- th:selected="${findById.empDepart.id eq fdAll.id}" --> </p> </div> </div>