Reputation: 31
I am learning how to build web application by spring boot.
When I try to use controller to use @Controller to show the jsp page, the "Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback." is shown.
I've considered if the problem is about component scan, but when I change the @Controller to @RestController, the web page could show the string I typed.
I would like to ask why the bean cannot be scanned when the @Controller is used and how can I fixed the problem.
This is the controller class
package com.lms.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class MainController {
@GetMapping("h")
public String init(ModelMap Model) {
return "index";
}
}
the application properties
spring.mvc.view.prefix = /WEB-INF/views/
spring.mvc.view.suffix = .jsp
spring.datasource.url = jdbc:mysql://localhost:3306/mydb?useSSL=false
spring.datasource.username = root
spring.datasource.password = 0000
#Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto = update
logging.level.org.hibernate.SQL = debug
the 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.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.library</groupId>
<artifactId>library</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>LMS</name>
<description>Demo project for Spring Boot Library Management System</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Persistance related -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId> org.apache.tomcat.embed </groupId>
<artifactId> tomcat-embed-jasper </artifactId>
<scope> provided </scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
the log
2021-08-22 02:55:47.719 DEBUG 17084 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8]
2021-08-22 02:55:47.719 DEBUG 17084 --- [nio-8080-exec-1] o.s.web.servlet.view.JstlView : View name 'index', model {}
2021-08-22 02:55:47.721 DEBUG 17084 --- [nio-8080-exec-1] o.s.web.servlet.view.JstlView : Forwarding to [/WEB-INF/views/index.jsp]
2021-08-22 02:55:47.726 DEBUG 17084 --- [nio-8080-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2021-08-22 02:55:47.726 DEBUG 17084 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2021-08-22 02:55:47.727 DEBUG 17084 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={}
2021-08-22 02:55:47.728 DEBUG 17084 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2021-08-22 02:55:47.728 DEBUG 17084 --- [nio-8080-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2021-08-22 02:55:47.741 DEBUG 17084 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2021-08-22 02:55:47.744 DEBUG 17084 --- [nio-8080-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2021-08-22 02:55:47.744 DEBUG 17084 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
I wonder if I put .jsp file in the wrong place. This is where I put the .jsp file.
I've been stucked for several hours. Hope someone can help me to solve the problem.
Upvotes: 0
Views: 619
Reputation: 24
The GetMapping
’s value is wrong. Change value to /h
may work.
Changed: Try this code to instead the origin method:
@GetMapping(“/h”)
public ModelAndView init() {
return new ModelAndView(“index”);
}
Upvotes: 0