无法在Spring Boot thymeleaf中插入数据到数据库

时间:2019-03-18 14:04:25

标签: spring-boot

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

  

Whitelabel错误页面

     

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

     

发生了意外错误(类型=未找到,状态= 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>

1 个答案:

答案 0 :(得分:0)

这样对我有用

控制器类:

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>