控制器弹簧数据Jpa和H2

如何解决控制器弹簧数据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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-