如何解决控制器弹簧数据Jpa和H2
我有一个执行若干功能的FilmsController。当我通过控制器进行映射和调用时,我的addNewFilm页面将不会加载,并且在尝试调用它时会显示我的常规错误页面。我还有一个updateFilmName,它将不会加载到我的应用中,用户可以在其中更改显示相同错误的电影的名称。我尝试重命名我的班级,检查不是导致问题的大写字母,甚至删除了所有文件并从头开始重新加载它们,这似乎没有什么不同。我以为问题出在我的@GetMapping方法上,但我发现它们有什么问题。我的其他方法有效,并且没有问题。
我还有一个DirectorsController,直到一个小时前newDirector开始执行与addNewFilm相同的操作并且未加载时,我才遇到问题。他们似乎被正确呼叫,我已经检查了好几次,看不到问题所在。在问题开始之前,我没有对newDirector方法进行任何更改,因为它已经运行了一段时间。
这是我的FilmsController:
@GetMapping("/addNewFilm")
public String addNewFilmForm(Model model) //new film form where user inputs details and clicks add film
{
model.addAttribute("filmForm",new FilmForm());
model.addAttribute("directors",directorService.listInAlphabeticalOrder());
return "addNewFilm";
}
@PostMapping("/addNewFilm"
public String addNewFilm(Model model,@Valid FilmForm filmForm,BindingResult binding,RedirectAttributes redirectAttributes)
{
if (binding.hasErrors()){
model.addAttribute("directors",directorService.listInAlphabeticalOrder());
return "addNewFilm";
}
//films are allowed to share the same name - but they can't share a director
Film film = new Film(filmForm.getDirector(),filmForm.getFilmName(),filmForm.getReleaseYear());
return "redirect:/film?id="+film.getFilmId();
}
@GetMapping("/updateFilmName")
public String updateFilmname(Model model) //new film form where user inputs details and clicks add film
{
model.addAttribute("updateForm",new FilmUpdateForm());
List<Film> films = filmService.listInChronologicalOrder();
model.addAttribute("films",films);
return "updateFilmName";
}
@PostMapping("/updateFilmName")
public String updateFilmName(Model model,@Valid FilmUpdateForm updateForm,RedirectAttributes redirectAttributes)
{
if (binding.hasErrors())
return "updateFilmName";
Film film = new Film(updateForm.getDirector(),updateForm.getFilmName(),updateForm.getReleaseYear());
if (filmService.existsByFilmNameAndDirector_DirectorId(film)) {
int fId;
fId = film.getFilmId();
filmService.update(fId,updateForm.getFilmName());
}
else {
return "DuplicateFilmError";
}
return "redirect:film/"+ film.getFilmId();
}
}
这是我的filmForm对象-updateFilmForm相同,因为它需要相同的数据。 ``
public class FilmForm {
@Size(min=2,max=20)
@NotEmpty
@NotBlank
private String filmName;
//add minimum year and max year (won't always be 2020,but can't allow films that are further then current year)
@PastOrPresent
@NotBlank
@Min(1888)
private int releaseYear;
@NotEmpty
private Director director;
}
这是我的addNewFilm html,再次更新,因为它需要相同的数据:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<div th:insert="fragments.html :: head"></div>
<body>
<header>
<div th:insert="fragments.html :: menu"></div>
<h1><span th:text="#{newfilm.addFilm}"></span></h1>
</header>
<main>
<form id ="newFilmForm" method="post" th:action="@{/addNewFilm}" th:object="${filmForm}">
<label for="filmName" th:text="#{film.filmName}" >
<input type="text" th:field="*{filmName}"/><span th:if="${#fields.hasErrors('filmName')}" th:errors="*{filmName}" th:text='#{newFilm.Error}'></span>
<p>
</label>
<label for="directorId" th:text="#{newfilm.chooseDirector}">
</label>
<select th:field="*{directorId}">
<option th:each="director: ${directors}" th:text="${director.directorFirstName} + ${director.directorSurname}" th:value="${director.directorId}"/>
</select>
<br>
<label for="filmReleaseYear" th:text="#{film.releaseYear}" >
</label>
<input type="text" th:field="*{releaseYear}"/><span th:if="${#fields.hasErrors('releaseYear')}" th:errors="*{releaseYear}" th:text='#{newFilm.Error}'></span>
<input type="submit" th:value="#{newFilm.addFilm}"/>
</form>
</main>
</body>
<footer th:insert="fragments.html :: footer"></footer>
</html>
这是我遇到的DirectorsController部分:
@GetMapping("/showNewdirector")
public String addNewDirectorForm(Model model) //new director form - this is where the user inputs the name of the director and clicks "add director" button
{
model.addAttribute("directorForm",new DirectorForm());
return "newdirector";
}
@PostMapping("/showNewdirector")
public String addNewDirectorSave(@Valid @ModelAttribute("aDirectorForm") DirectorForm directorForm,RedirectAttributes redirectAttributes)
{
if (binding.hasErrors())
return "newdirector";
Director director = new Director(directorForm.getDirectorFirstName(),directorForm.getDirectorSurname()); //gets director name through form and combines into a director object
if(directorService.checkDirectorNameIsNotDuplicate(director) ==false) //checks the director name is not the same as another director in the database. Directors cannot have the same name.
directorService.save(director); //saves new director,which should now appear in the list
else
return "DuplicateDirectorError";
return "redirect:director/"+ director.getDirectorId(); //redirects to the page of the new Director with all of their details to view i.e. /director/id
}
用来检查导演是否重复的方法:
@Override
public boolean checkDirectorNameIsNotDuplicate(Director director) {
int id = 1;
String dFName;
String dSName;
while(directorDao.existsById(id)==true) {
dFName = directorDao.getDirectorFirstNameById(id);
dSName = directorDao.getDirectorSurnameById(id);
if (director.getDirectorFirstName().equals(dFName) && director.getDirectorSurname().equals(dSName)) {
return true; //if the entered director name is the same as any already stored it will return true
}
else {
id++;
// this condition ensures that an infinite loop is not caused
if (directorDao.existsById(id)==false) {
break;
}
}
}
return false;
}
DirectorForm对象:
public class DirectorForm {
@Size(min=2,max=20)
@NotEmpty
private String directorFirstName;
@Size(min=2,max=20)
@NotEmpty
private String directorSurname;
}
和newDirector html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<div th:insert="fragments.html :: head"></div>
<body>
<header>
<div th:insert="fragments.html :: menu"></div>
<h1><span th:text="#{newDirector.addDirector}"></span></h1>
</header>
<main>
<form id="newDirectorForm" method="post" th:action="@{/shownewdirector}" th:object="${aDirectorForm}">
<label for="directorFirstName" th:text="#{director.directorFirstName}" >
</label>
<input type="text" th:field="*{directorFirstName}"/><span th:if="${#fields.hasErrors('directorFirstName')}" th:errors="*{directorFirstName}" th:text='#{newDirector.error}'></span>
<p>
<label for="directorSurname" th:text="#{director.directorSurname}" >
</label>
<input type="text" th:field="*{directorSurname}"/><span th:if="${#fields.hasErrors('directorSurname')}" th:errors="*{directorSurname}" th:text='#{newDirector.error}'></span>
<br>
<input type="submit" th:value="#{newdirector.addDirector}"/>
</form>
<h3 class="error" th:if="${duplicate}" th:text="#{newdirector.duplicate}"></h3>
</main>
</body>
<footer th:insert="fragments.html :: footer">
</footer>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。