SpringBoot 实现下拉框

前端页面:

<div class="layui-form-item">
                <label class="layui-form-label" ><span
                            class="x-red">*span>素材类型:label>
                    <div class="layui-input-inline">

                        <select id="name" name="name" class="select"  size="1" >
                            <option th:each="sourceMateriaTypes:${sourceMateriaTypes}" th:value="${sourceMateriaTypes.name}"
                                    th:text="${sourceMateriaTypes.name}" th:selected="${sourceMateria.sourceId==sourceMateriaTypes.id}">option>
                        select>
                        div>
                        div>

后端代码:

/**
    * 添加跳转页面
    * @return
    */
    @GetMapping("editBefore/{id}")
    public String editBefore(Model model,@PathVariable("id")Long id){
        SourceMateria sourceMateria= sourceMateriaService.getById(id);
        //获取类型名用以回显数据
        String name=sourceMateriaTypeService.selectById(id).getName();
        sourceMateria.setName(name);

        List<SourceMateriaType> sourceMateriaTypes=sourceMateriaService.selectAllType();
        model.addAttribute("sourceMateriaTypes",sourceMateriaTypes);
        model.addAttribute("sourceMateria",sourceMateria);
        return prefix+"edit";
    }
    /**
    * 添加
    * @param sourceMateria
    * @return
    */
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(SourceMateria sourceMateria,
                           @RequestParam(required = false,value = "videoFile") MultipartFile videoFile,
                           @RequestParam(required = false,value = "imageFile") MultipartFile imageFile,
                           @RequestParam(required = false) String name,
                           @RequestParam(required = false) String title,
                           @RequestParam(required = false) String content,
                           @RequestParam(required = false) Integer traffic){
        sourceMateria.setTitle(title);
        sourceMateria.setContent(content);
        sourceMateria.setTraffic(traffic);
        //根据素材类型查素材id并加入sourceMateria进行更改数据
        QueryWrapper<SourceMateriaType> queryWrapper = new QueryWrapper<SourceMateriaType>();
        SourceMateriaType sourceMateriaType = sourceMateriaTypeService.selectOne(queryWrapper.eq("name", name));
        sourceMateria.setSourceId(sourceMateriaType.getId());
        //上传文件
        if (imageFile != null){
            String upload = null;
            upload = OssFileUtils.uploadSingleFile(imageFile);
            sourceMateria.setIcon(upload);
        }

        if (videoFile != null){
            String upload = null;
            upload = OssFileUtils.uploadSingleFile(imageFile);
            sourceMateria.setVideoUrl(upload);
        }
        return toAjax(sourceMateriaService.updateById(sourceMateria));
    }

你可能感兴趣的:(框架,java编程)