【发布时间】:2018-12-08 02:27:16
【问题描述】:
这个问题已经问了好几年了,但无济于事,我想是时候在这里问了。
为什么我的小组会看到此 Whilelabel 错误页面? --> (https://i.imgur.com/hGd61Qr.png)
文件结构图一:https://i.imgur.com/x5Jn4gP.png
文件结构图二:https://i.imgur.com/K8gP59y.png
文件结构图3:https://i.imgur.com/JFFEidy.png
似乎我们拥有 Spring 喜欢的一般结构,主应用程序高于所有其他控制器等。
MainApp 类: 包 com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication(scanBasePackages = {"com.example.demo",
"com.example.demo.domain", "com.example.demo.dao",
"com.example.demo.service"})
@ComponentScan("com.example.demo.domain.Book_CopiesMapper")
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
}
我们必须有那个 ComponentScan 否则我们会得到这个错误: https://i.imgur.com/BKDwHyn.png
控制器类:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.domain.Borrower;
import com.example.demo.domain.Employee;
import com.example.demo.service.BorrowerService;
@RestController
public class LoginController {
@Autowired
BorrowerService borrowerService;
@GetMapping("/login")
public String login() {
System.out.println("hello from LoginController");
return "login";
}
@GetMapping("/signup")
public String signup() {
return "signup";
}
@RequestMapping(value = "/registerUser", method = RequestMethod.POST)
public String registerUser(@ModelAttribute Borrower borrower, Model model) {
borrowerService.insertBorrower(borrower);
model.addAttribute("message", "Success! You may now login with Card Number: " + borrower.getcardNo());
return "registerUser";
}
}
其中 login 是一个简单的登录页面,signup 是一种允许用户注册我们的库的表单,而 registerUser 用于连接到我们的 Oracle SQL 数据库并将用户插入到相应的表中(我们假设是这样做的正确方法)
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>
<groupId>com.example</groupId>
<artifactId>MySpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MySpringBoot</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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-jdbc</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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>codelds</id>
<url>https://code.lds.org/nexus/content/groups/main-repo</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties:
#spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
spring.thymeleaf.cache = false
#mybatis
mybatis.type-aliases-package=com.example.demo.domain
spring.datasource.url = jdbc:oracle:thin:@dataserv.mscs.mu.edu:1521:orcl
spring.datasource.username =
spring.datasource.password =
spring.datasource.driver-class-name = oracle.jdbc.driver.OracleDriver
由于显而易见的原因,我们的数据库的用户名和密码被省略了。
如果有人能提供任何反馈,我们将不胜感激!如果人们希望我发布其他图片或文件,请发表评论。
谢谢!
编辑:我们使用的路径是 localhost:8080/login 或 localhost:8080/signup。目前看来,我们采取的任何路径都会导致此错误
此外,我们所有的 DAO 类都对我们使用的任何服务使用 @Autowired 标签,同样,我们在不同的类中使用 @Autowire 任何其他我们需要的东西,例如 Service 包中的 DAO 等等。对不起,这句话有点含糊
【问题讨论】:
-
您没有提到哪个路由产生 404...我们假设它是您的登录路由吗?
http://localhost:8080/login还是别的什么?另外,Book_CopiesMapper类上有哪些注释? -
请尝试从您的控制器中删除
@RestController,使用@Controller希望它会有所帮助。 -
您的应用程序在启动时以及当您发出产生 404 的请求时会产生什么日志输出?
-
正如@RandyCasburn 所暗示的,我怀疑
Book_CopiesMapper缺少使其成为组件的注释。这里发生了很多事情。你能瘦下来并分享minimal, complete, and verifiable example吗? -
删除“@ComponentScan”注释,正如其他人所说,使用“@Controller”而不是“@RestController”。 '@ComponentScan' 注释覆盖了 scanBasePackages,因此 springcontext 中唯一的 bean 将来自 com.example.demo.domain.Book_CopiesMapper。
标签: spring spring-mvc spring-boot