2

I just started learning spring boot, and I do have a problem with my first application.

first, I will show the file structure, and then I will show my code and explain the problem and the solutions I tried.

File Structure

enter image description here

The problem and the code

so the problem was that I could not get mapped to the function users in the "UsersLoginController.java" file, and I was getting a 404 error. after searching, I found that both controller and the class with the main method should be in the same package, or I can use the @ComponentScan("My.Package.Name"), so I did use it.

so now I was able to get my request mapped to the correct function (as showen in the code section of the question. but the problem with the jsp file was not done. I still get 404 error on it. I did add this dependency to my pom.xml file as a solution seen in YouTube and some other articles, but nothing changed.

<dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jasper</artifactId>
            <version>9.0.68</version>
        </dependency>

class with the main method

package AEINTech.CO.DevicesManager;



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

@SpringBootApplication
@ComponentScan("AEINTech.CO.DevicesManager.LoginPage")
public class DevicesManagerApplication {

     
    public static void main(String[] args) {
        SpringApplication.run(DevicesManagerApplication.class, args);
    }

}

Controller class

package AEINTech.CO.DevicesManager.LoginPage;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UsersLoginController {
    
    @RequestMapping("/r")
    public String users() {
        return "LoginPage.jsp";
    }
    
    @RequestMapping(value = {"/rr"})
    @ResponseBody
    public String userse() {
        return "LoginPage";
    }

}

to pom.xml file

<?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.7.5-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>AEINTech.CO</groupId>
    <artifactId>Devices-Manager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Devices-Manager</name>
    <description>A project for the devices management web system</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    
    <dependencies>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
        
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jasper</artifactId>
            <version>9.0.68</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>
    

</project>

In the "application.properties" file I configured the port to be 44444

when I start the application This is printed to the console:


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
[32m :: Spring Boot :: [39m     [2m (v2.7.5-SNAPSHOT)[0;39m

[2m2022-10-19 02:51:34.519[0;39m [32m INFO[0;39m [35m26704[0;39m [2m---[0;39m [2m[           main][0;39m [36mA.C.D.DevicesManagerApplication         [0;39m [2m:[0;39m Starting DevicesManagerApplication using Java 17.0.2 on Abdelrahman-Nassar with PID 26704 (C:\Users\aenas\Desktop\Devices-Manager\target\classes started by aenas in C:\Users\aenas\Desktop\Devices-Manager)
[2m2022-10-19 02:51:34.522[0;39m [32m INFO[0;39m [35m26704[0;39m [2m---[0;39m [2m[           main][0;39m [36mA.C.D.DevicesManagerApplication         [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default"
[2m2022-10-19 02:51:35.036[0;39m [32m INFO[0;39m [35m26704[0;39m [2m---[0;39m [2m[           main][0;39m [36mtrationDelegate$BeanPostProcessorChecker[0;39m [2m:[0;39m Bean 'org.springframework.ws.config.annotation.DelegatingWsConfiguration' of type [org.springframework.ws.config.annotation.DelegatingWsConfiguration$$EnhancerBySpringCGLIB$$a1174588] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[2m2022-10-19 02:51:35.090[0;39m [32m INFO[0;39m [35m26704[0;39m [2m---[0;39m [2m[           main][0;39m [36m.w.s.a.s.AnnotationActionEndpointMapping[0;39m [2m:[0;39m Supporting [WS-Addressing August 2004, WS-Addressing 1.0]
[2m2022-10-19 02:51:35.328[0;39m [32m INFO[0;39m [35m26704[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port(s): 44444 (http)
[2m2022-10-19 02:51:35.337[0;39m [32m INFO[0;39m [35m26704[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.apache.catalina.core.StandardService  [0;39m [2m:[0;39m Starting service [Tomcat]
[2m2022-10-19 02:51:35.337[0;39m [32m INFO[0;39m [35m26704[0;39m [2m---[0;39m [2m[           main][0;39m [36morg.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/9.0.68]
[2m2022-10-19 02:51:35.511[0;39m [32m INFO[0;39m [35m26704[0;39m [2m---[0;39m [2m[           main][0;39m [36morg.apache.jasper.servlet.TldScanner    [0;39m [2m:[0;39m At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
[2m2022-10-19 02:51:35.527[0;39m [32m INFO[0;39m [35m26704[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2022-10-19 02:51:35.527[0;39m [32m INFO[0;39m [35m26704[0;39m [2m---[0;39m [2m[           main][0;39m [36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 962 ms
[2m2022-10-19 02:51:35.832[0;39m [32m INFO[0;39m [35m26704[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port(s): 44444 (http) with context path ''
[2m2022-10-19 02:51:35.840[0;39m [32m INFO[0;39m [35m26704[0;39m [2m---[0;39m [2m[           main][0;39m [36mA.C.D.DevicesManagerApplication         [0;39m [2m:[0;39m Started DevicesManagerApplication in 1.626 seconds (JVM running for 2.132)

when I use the url http://127.0.0.1:44444/rr the text "LoginPage.jsp" is printed to the screen (which means that the mappng is working fine).

but when I use the url http://127.0.0.1:44444/r the 404 (Whitelabel Error Page) error appears

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Oct 19 02:57:22 AST 2022
There was an unexpected error (type=Not Found, status=404).

and when I use any of the URLs I get the following printed to the console one time

2022-10-19 02:55:23.230[0;39m [32m INFO[0;39m [35m32628[0;39m [2m---[0;39m [2m[io-44444-exec-1][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring DispatcherServlet 'dispatcherServlet'
[2m2022-10-19 02:55:23.230[0;39m [32m INFO[0;39m [35m32628[0;39m [2m---[0;39m [2m[io-44444-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Initializing Servlet 'dispatcherServlet'
[2m2022-10-19 02:55:23.230[0;39m [32m INFO[0;39m [35m32628[0;39m [2m---[0;39m [2m[io-44444-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Completed initialization in 0 ms

the solution I tried was to change the file location (putting it in the same package with the controller or the class with main method) , or trying to access it using something like: LoginPage.jsp ./LoginPage.jsp ../LoginPage.jsp /AEINTech/CO/DevicesManager/LoginPage/LoginPage.jsp

One solution tried was to create a folder with the name "webapp" and put my .jsp folder in it, but that did not work also.

I have been searching for a solution for so much time, unfortunately, it is either I did not understand what the problem really is, so I am searching in the wrong direction. Or I just could not find the solution for it yet. I will keep looking for solutions, and I will update the question if I find anything new

  • Usually a webapp will have separate folders for java code and jsp e.g. /src/main/java//... and /src/main/webapp. If you use an IDE I suggest that you use a wizard to generate the project, it will make the necessary paths for you. – Scary Wombat Oct 19 '22 at 00:26
  • @ScaryWombat if you mean that I need to create a folder with the name webapp in the main folder and move my files to it, unfortunately it did not work also. Could you please suggest a wizard name to try?, since I do not know any, and I do not know which one is good – Abdelrahman Nassar Oct 19 '22 at 00:36

1 Answers1

-1

Maybe you can try creating a webapp folder under the root and put your jsp inside

jie Old
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 21 '22 at 04:49