0

I always get this error message after I insert data. I don't know what is wrong with my code:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Mar 17 13:04:00 PDT 2019

There was an unexpected error (type=Not Found, status=404).
No message available

Student info form:

<!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>

controller class

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"; 
    }
}

Repository

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> {

}

service class

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);
    }
}

application property file

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>
Community
  • 1
  • 1
  • You are able to insert data and page is not redirected to a views/success? – Romil Patel Mar 18 '19 at 15:17
  • the database is always empty. I don't know why my code is not working – peter hilson Mar 18 '19 at 19:43
  • You do have a view called `views/success` right? – g00glen00b Mar 19 '19 at 13:13
  • yes please, even if i redirect to the same page studentinfo form I still get the same error message. i don't know why this is not working. – peter hilson Mar 19 '19 at 13:19
  • 1
    For starters stop mixing 2.1.3 and 2.1.2 jars of Spring Boot. Next to `@Valid` use `@ModelAttribute` to indicate you want to use this for binding. Start your application with `--debug` so that you get more information on why it fails. I suspect that validation fails and it tries to determine which page to show. – M. Deinum Mar 21 '19 at 09:30
  • @peterhilson As M.Deinum has told please refer these https://stackoverflow.com/questions/43716767/spring-mvc-requestbody-vs-modelattribute https://stackoverflow.com/questions/8688135/modelattribute-annotation-when-to-use-it/26916920#26916920 – Romil Patel Mar 21 '19 at 12:20
  • I don't know how but data insertion is now working. Thanks to all contributors that helped in solving this problem. I really appreciate your help.God bless you all. – peter hilson Mar 21 '19 at 15:37

1 Answers1

0

This way it works for me

controller class:

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"; 
        }
}

on your html file I think you can do it this way. You don't need the "action="@{/studentinfo}"" because the controller is doing it for you. For me this caused some problems.

Student info form:

<!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>
LukasI
  • 11
  • 2