【问题标题】:cannot insert data into database in spring boot thymeleaf无法在 Spring Boot thymeleaf 中将数据插入数据库
【发布时间】:2019-08-08 22:19:29
【问题描述】:

插入数据后,我总是收到此错误消息。我不知道我的代码有什么问题:

白标错误页面

此应用程序没有显式映射 /error,因此您将其视为后备。
2019 年 3 月 17 日星期日 13:04:00 PDT

出现意外错误(类型=未找到,状态=404)。
没有可用的消息

学生信息表:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
    layout:decorate="~{fragments/main_layout}">
<head>

</head>
<body>
    <div layout:fragment="content" class="container mySpace">
        <form action="@{/studentinfo}" method="post" th:object="${stdinfo}">
            <div class="form-row">
                <div class="form-group col-md-6">
                    <label for="student_no">Student ID</label> 
                    <input type="text" class="form-control" id="student_no"  th:field="*{studentId}" placeholder="Enter Student Id" />                  
                        <label for="coutnry">Country of Birth</label> 
                        <input type="text" class="form-control" id="country" th:field="*{country}"
                        placeholder="Enter country of nationality" /> 
                        <label for="mother">Mother's
                        name</label> <input type="text" class="form-control" id="mother"
                        placeholder="mother or guidian name" th:field="*{motherName}" />                        
                        <label for="nationality">Nationality</label>
                    <input type="text" class="form-control" id="nationality"
                        placeholder="Nationality" th:field="*{nationality}" />                      
                </div>              
            </div>
            <div class="text-center">
            <!--    <button type="submit" class="btn btn-primary">Sign in</button> -->
<input type="submit" value="Submit" class="btn btn-primary" />
            </div>
        </form>
    </div>
</body>
</html>

控制器类

package com.ecc.telink.controllers;

import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.ecc.telink.entities.StudentInfo;
import com.ecc.telink.services.StudentInfoServices;    

@Controller
public class StudentInfoController {
    @Autowired
    private StudentInfoServices studentInfoService;
          
    @RequestMapping(value="/studentinfo", method=RequestMethod.GET)
    public String showStudentInfo(Model model) {
        model.addAttribute("stdinfo", new StudentInfo());
        return "views/studentInfoForm";
    }
     
    @RequestMapping(value="/studentinfo", method=RequestMethod.POST) 
    public String processStudentInfo(@Valid
    StudentInfo studentInfo) { 
        studentInfoService.addStudentInfo(studentInfo);
        return "views/success"; 
    }
}

存储库

package com.ecc.telink.repositories;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import com.ecc.telink.entities.StudentInfo;

public interface StudentInfoRepository extends JpaRepository<StudentInfo, String> {

}

服务等级

package com.ecc.telink.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ecc.telink.entities.StudentInfo;
import com.ecc.telink.repositories.StudentInfoRepository;

@Service
public class StudentInfoServices {
    
    @Autowired
    private StudentInfoRepository studentInfoRepository;
    
    public void addStudentInfo(StudentInfo studentInfo) {
        studentInfoRepository.save(studentInfo);
    }
}

应用程序属性文件

spring.datasource.url=jdbc:mysql://localhost:3306/schoolmanagementsystem
spring.datasource.username=root
spring.datasource.password=123
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
server.error.include-stacktrace=always
spring.thymeleaf.cache = false

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>ChurchManagementSystem</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ChurchManagementSystem</name>
    <description>Church Management System</description>

    <properties>
        <java.version>1.8</java.version>
        
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>webjars-locator</artifactId>
            <version>0.36</version>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>
        <dependency>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.1.3</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

【问题讨论】:

  • 您可以插入数据并且页面未重定向到视图/成功?
  • 数据库始终为空。我不知道为什么我的代码不起作用
  • 您确实有一个名为views/success 的视图对吗?
  • 是的,请,即使我重定向到同一页面的学生信息表单,我仍然会收到相同的错误消息。我不知道为什么这不起作用。
  • 对于初学者,请停止混合 2.1.3 和 2.1.2 jar 的 Spring Boot。在@Valid 旁边使用@ModelAttribute 表示您想使用它进行绑定。使用--debug 启动您的应用程序,以便获得有关它失败原因的更多信息。我怀疑验证失败,它会尝试确定要显示的页面。

标签: spring-boot


【解决方案1】:

这样对我有用

控制器类:

package com.ecc.telink.controllers;

import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.ecc.telink.entities.StudentInfo;
import com.ecc.telink.services.StudentInfoServices;    

@Controller
public class StudentInfoController {
    @Autowired
    private StudentInfoServices studentInfoService;
          
    @RequestMapping(value="/studentinfo", method=RequestMethod.GET)
    public String showStudentInfo(Model model) {
        model.addAttribute("stdinfo", new StudentInfo());
        return "views/studentInfoForm";
    }
     
    @RequestMapping(value="/studentinfo", method=RequestMethod.POST) 
        public String processStudentInfo(@ModelAttribute("stdinfo") String 
        studentInfo { 
            studentInfoService.addStudentInfo(studentInfo);
            return "views/success"; 
        }
}

在您的 html 文件上,我认为您可以这样做。您不需要“action="@{/studentinfo}"”,因为控制器正在为您执行此操作。对我来说,这引起了一些问题。

学生信息表:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
    layout:decorate="~{fragments/main_layout}">
<head>

</head>
<body>
    <div layout:fragment="content" class="container mySpace">
        <form method="post" th:object="${stdinfo}">
            <div class="form-row">
                <div class="form-group col-md-6">
                    <label for="student_no">Student ID</label> 
                    <input type="text" class="form-control" id="student_no"  th:field="*{studentId}" placeholder="Enter Student Id" />                  
                        <label for="coutnry">Country of Birth</label> 
                        <input type="text" class="form-control" id="country" th:field="*{country}"
                        placeholder="Enter country of nationality" /> 
                        <label for="mother">Mother's
                        name</label> <input type="text" class="form-control" id="mother"
                        placeholder="mother or guidian name" th:field="*{motherName}" />                        
                        <label for="nationality">Nationality</label>
                    <input type="text" class="form-control" id="nationality"
                        placeholder="Nationality" th:field="*{nationality}" />                      
                </div>              
            </div>
            <div class="text-center">
            <!--    <button type="submit" class="btn btn-primary">Sign in</button> -->
<input type="submit" value="Submit" class="btn btn-primary" />
            </div>
        </form>
    </div>
</body>
</html>

【讨论】:

    猜你喜欢
    • 2020-10-02
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 2017-11-28
    • 2019-12-06
    • 2021-02-25
    • 2019-10-22
    • 2012-04-13
    相关资源
    最近更新 更多