java—通过html页面和使用控制器修改对象的最佳方式?

odopli94  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(194)

我对java和spring还很陌生,我不知道如何继续使用控制器更新对象。关于这个我有很多问题。如果这很重要的话,我正在使用postgresql。
目前,我有一个configure页面,其中显示了由登录用户创建的会议列表。所有会议在“modify”一侧都有一个按钮,该按钮发送到“/updateconference”页面并传递所选会议的id。
我没有conferenceservice.updateconference(conferencedto,id)的方法;但是我仍然可以使用updateconference页面来显示所选的会议详细信息,尽管当我单击submit时它会转到http://localhost:8080/teacher/updateconference,由于某种原因是404。
我的控制器类上的modify方法:

@GetMapping(path = "/updateConference/{id}")
public ModelAndView showUpdateConferenceForm(@PathVariable(name = "id") Long id){
    Optional<Conference> conference = conferenceService.findById(id);
    ModelAndView mav = new ModelAndView("updateConference");
    mav.addObject("conference",conference.get());
    return mav;
}

@PostMapping("/updateConference/{id}")
public ModelAndView updateConference(
        @ModelAttribute("conference") @Valid ConferenceDto conferenceDto, Conference conference, BindingResult result, @PathVariable Long id) {

    if(result.hasErrors()){
        return new ModelAndView("updateConference","conferenceDto", conferenceDto);
    }

        try {
            conferenceService.updateConference(conferenceDto, id); // I have not implemented this method yet
        } catch (ConferenceAlreadyExistException uaeEx) {        // Dont really know how to handle this exception if I am changing the conference but want to keep the same name
            ModelAndView mav = new ModelAndView("updateConference","conferenceDto", conferenceDto);
            mav.addObject("message", uaeEx.getMessage());
            return mav;
        }
        ModelAndView mav = new ModelAndView("redirect:/teacher/configure");
        mav.addObject("message", "Successfully modified conference.");
        return mav;
    }

我的updateconference的html:

<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml" xmlns:sec="http://www.w3.org/1999/xhtml">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"/>
    <title>Modify conference</title>

</head>
<body>

<div class="topnav" id="myTopnav">
    <a th:href="@{/teacher/teacherIndex}" class="active">Home</a>
    <a th:href="@{/teacher/users}">Users</a>
    <a th:href="@{/teacher/configure}">Configure</a>
    <a th:href="@{/perform_logout}" >Logout</a>
    <a href="javascript:void(0);" class="icon" onclick="myFunction()">
        <i class="fa fa-bars"></i>
    </a>
</div>
<div class="hero">
    <div class="content">
        <div  th:if="${message != null}" th:align="center" class="alert alert-danger" th:utext="${message}">message</div>
        <div class="container my-3">
            <div class="row">
                <div class="col-md-8 mx-auto">
                    <div th:align="center">
                        <h2>Modifying your conference</h2>
                    </div>
                    <br></br>
                    <form th:action="@{/teacher/updateConference}" th:object="${conference}" method="post">
                        <div class="form-row">
                            <div class="col-md-6 form-group">
                                <label th:text="#{label.conference.name}">name</label>
                                <input type="text" id="name" class="form-control" name="name"  th:field="*{name}" required="required"/>
                                <p th:each="error: ${#fields.errors('name')}"
                                   th:text="${error}">Validation error</p>
                            </div>
                            <div class="col-md-6 form-group">
                                <label th:text="#{label.conference.description}">description</label>
                                <input type="text" id="description" class="form-control" name="description" value="" th:field="*{description}" required="required"/>
                                <p th:each="error: ${#fields.errors('description')}"
                                   th:text="${error}">Validation error</p>
                            </div>
                            <div class="col-md-6 form-group">
                                <label th:text="#{label.conference.startDate}">start</label>
                                <input type="date" id="startConference" class="form-control" name="startConference" value="" th:field="*{startConference}" required="required"/>
                                <span th:if="${#fields.hasErrors('startConference')}" th:errors="*{startConference}">ERROR!</span>
                            </div>
                            <div class="col-md-6 form-group">
                                <label th:text="#{label.conference.endDate}">end</label>
                                <input type="date" id="endConference" class="form-control" name="endConference" value="" th:field="*{endConference}" required="required"/>
                                <p th:each="error: ${#fields.errors('endConference')}"
                                   th:text="${error}">Validation error</p>
                            </div>
                            <button type="submit" class="btn btn-primary btn-block mt-3">Submit</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

我真的不知道下一步是什么。
我是否应该在conferenceservice中创建不同的方法:updatename、updatedescription、updatestartdate和updateenddate?或者我可以创建一个方法来传递一个新的会议对象并用相同的id保存它吗?
如何在控制器类中实现这些方法?
如何处理conferencealreadyexistexception?以目前的方式,如果我想保持会议的相同名称,它会抛出。
任何提示是感激的,如果你需要任何其他信息,我很乐意提供!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题