spring thymeleaf中的嵌套对象返回null

oknrviil  于 2023-04-28  发布在  Spring
关注(0)|答案(1)|浏览(135)

这是我的表格,用于更新员工实体。

<input name="_method" type="hidden" value="put" />

        <input type="text" style="background-color:#EEEEEE" th:field="*{id}" hidden readonly placeholder="Enter mail" class="form-control col-4 mb-4" />

        <label>Member Email</label>
        <input type="text" style="background-color:#EEEEEE" th:field="*{email}" placeholder="Enter mail" class="form-control col-4 mb-4" />

        <label>Availability for Runner</label>
        <select class="form-control col-4 mb-4" style="background-color:#EEEEEE;" th:field="*{isAvailable}" name="type">
            <option th:value="1" >Available</option>
            <option th:value="0" >Not Available</option>
        </select>

        <label>Current Desk</label>
        <select th:field="*{currentDropOffPoint}" class="form-control col-4 mb-4" style="background-color:#EEEEEE" name="type">
            <option th:each="point : ${dropOffPoints}" th:value="${point}" th:text="${point}"></option>
        </select>

        <label>Member Role</label>
            <select th:field="*{role.name}" class="form-control col-4 mb-4" style="background-color:#EEEEEE"name="type">
                <option th:each="role : ${roles}" th:value="${role}" th:text="${role}"></option>
            </select>

        <div style="width: 790px;height: 100px;background-color: #F6F6F6;border-radius: 20px">
            <button th:href="@{/api/v1/portal/showList}" class="btn btn-primary col-2" style="color: red;background-color: #00000014;border-color: #00000014;margin: 26px 26px 26px 21px;width: 82px;height: 48px;border-radius: 999px" type="submit">Delete</button>
            <button class="btn btn-primary col-2" type="submit" style="background-color: #000000; border-color: #000000;float: right;width: 70px;height: 48px;margin: 26px 21px 26px 10px;border-radius: 999px">Save</button>
            <button th:href="@{/api/v1/portal/staff/showList}" class="btn btn-primary col-2" style="color: black;background-color: #00000014;border-color: #00000014;float: right;width: 85px;height: 48px;margin-top: 26px;margin-bottom: 26px;margin-right: 10px;border-radius: 999px " type="submit">Cancel</button>
        </div>

    </form>

这是员工实体。

data class StaffDTO(
    val id: StaffId,
    val email: String,
    val role: RoleDTO?,
    val isAvailable: Boolean?,
    val currentDropOffPoint: Long?
)
fun Staff.toDto() = StaffDTO(id, email, role?.toDto(), isAvailable, currentDropOffPoint)

这是roleDTO

data class RoleDTO(
    val id: RoleId=1L,
    val name: String="OPERATOR"
)

fun Role.toDto() = RoleDTO(id, name)

这是控制器

@Controller
class UpdateController(
    private val staffServiceImpl: StaffServiceImpl
) {

    @GetMapping("/portal/updateStaff/{id}")
    fun showStaffUpdateForm(@PathVariable("id") staffID: StaffId): ModelAndView? {
        val mav = ModelAndView("update-admin-form")
        val roles = listOf("OPERATOR","RUNNER","EXECUTOR","ADMIN")
        val dropOffPoints = listOf(1L,2L,3L,4L,5L)
        val staff = staffServiceImpl.findById(staffID)!!.toDto()
        val isAvailableObject = true;
        val isAvailable= listOf(true,false)
        mav.addObject("dropOffPoints",dropOffPoints)
        mav.addObject("staff", staff)
        mav.addObject("roles",roles)
        mav.addObject("boolean",isAvailableObject)
        mav.addObject("isAvailable",isAvailable)
        return mav
    }

    @RequestMapping("/portal/saveStaff/{id}")
    fun updateStaff(@PathVariable("id") id: Long?, @ModelAttribute staffDTO: StaffDTO): RedirectView? {
        if (staffDTO != null) {
            staffServiceImpl.updateStaff(staffDTO)
        }
        val redirectView = RedirectView()
        redirectView.url = "http://localhost:8080/portal/test"
        return redirectView
    }

问题是,即使我设置了role的属性,它也会向控制器返回null,并抛出NoSuchMethodException。我在互联网上搜索了这个异常,它说这个异常被抛出是因为roleDTO没有args构造函数。但是当我为roleDTO给予默认值时,它仍然会抛出相同的异常。
对此有什么解决办法吗?

uujelgoq

uujelgoq1#

问题出在StaffDTO的构造函数上。
当我在staffDTO中创建一个RoleDTO作为默认值时,问题就解决了。
在使用thymeleaf时,您还需要创建子对象。不能将null作为默认值发送给子对象。

相关问题