1

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

I couldn't find what is wrong with this code.

URL:localhost:8080/home

pom.xml My 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 https://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.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>CrudOperations</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>CrudOperations</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>

        <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- JSTL for JSP -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!-- Need this to compile JSP -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- Need this to compile JSP, 
            tomcat-embed-jasper version is not working, no idea why -->
        <dependency>
            <groupId>org.eclipse.jdt.core.compiler</groupId>
            <artifactId>ecj</artifactId>
            <version>4.6.1</version>
            <scope>provided</scope>
        </dependency>


        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <scope>provided</scope>
            <!--  version>4.1.4.Final</version --> 
        </dependency> 

        <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <!-- version>5.2.3.Final</version-->
</dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

Controller My controller inside the com.example.CrudOperations.Controllers and also included in @ComponentScan in main application

 package com.example.CrudOperations.Controllers;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;


    @Controller
    public class homeController {

        @RequestMapping("/home")
        public ModelAndView register() {
            ModelAndView mv = new ModelAndView("register");

            return mv;
        }

    }

register.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h1>register here</h1> 

    <form method="POST" action="">
            <label>NAME:</label>
            <input type=text id="name" placeholder="enter your name"> 

            <label>Password</label>
            <input type="text" id="password" placeholder="enter your password"> 

            <label>Confirm Password</label>
            <input type="text" id="confirmPassword" placeholder="enter your password">

            <button type="submit">REGISTER</button>

    </form> 

</body>
</html> 

application.properties

spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp 


spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:h2:mem:demo1
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 

MainApplication CrudOperations.java here i have included componentscan annotation for scanning the controllers in controller package

package com.example.CrudOperations;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import com.example.CrudOperations.Controllers.homeController;


    @SpringBootApplication
    @ComponentScan(basePackages= {"com.example.CrudOperations.Controllers"})
    public class CrudOperationsApplication {

        public static void main(String[] args) {
            SpringApplication.run(CrudOperationsApplication.class, args);
            System.out.println("User Applications Started Running ");
        }

    }
  • Take care of java naming convetions. package names should be lower case and class names should start with upper case character – Jens Dec 12 '19 at 19:24
  • Are you sure your register.jsp is located under /WEB-INF/view/? Or are you using the Spring Boot convention location resources/templates? Also, if `com.example.CrudOperations` is your default, top-level package, then you can remove the explicit `@ComponentScan` annotation on `CrudOperationsApplication` class. Spring Boot should detect your components (controllers), services etc. in the child packages of your SpringBootApplication by default. – Troley Dec 12 '19 at 20:15

0 Answers0