SpringBoot Thymeleaf模板 HTML的select下拉回显、从页面返回选中的数据到controller、外键的处理等相关问题

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>
原文地址:https://www.cnblogs.com/zhangzimuzjq/p/12112418.html