我试图从两天开始运行一个简单的Spring启动应用程序,但仍然无法使其工作。我查看了所有相关的问题和博客,但问题仍然存在。
主要的springboot应用程序代码
package com.smart;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = { "com.smart.*" })
public class SmartcontactmanagerApplication {
public static void main(String[] args) {
SpringApplication.run(SmartcontactmanagerApplication.class, args);
}
}
注册页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:replace="base::layout(~{::section})">
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<section>
<div class="container">
<div class="row">
<div class="col-md-8 offset-md-2">
<div class="my-card mt-2">
<div class="container text-center">
<img class="" style="width: 80px;" alt="" src="" th:src="@{/img/register.png}">
</div>
<h1 class="text-center">Register Here !!</h1>
<form action="" th:action="@{do_register}" th:object="${user}" method="post">
<!-- name field -->
<div class="form-group">
<label for="name_field">Your Name</label>
<input type="text" class="form-control" id="name_field" aria-describedby="emailHelp"
placeholder="Enter you name here" required th:value="${user.name}" name="name" />
</div>
<!-- email field -->
<div class="form-group">
<label for="email_field">Your Email</label>
<input type="email" class="form-control" id="email_field" aria-describedby="emailHelp"
placeholder="Enter you email here" required th:value="${user.email}" name="email" />
</div>
<!-- password field -->
<div class="form-group">
<label for="password_field">Your Password</label>
<input name="passWord" type="password" class="form-control" id="password_field"
aria-describedby="emailHelp" placeholder="Enter you password here" required />
</div>
<!-- user about field -->
<div class="form-group">
<textarea rows="10" class="form-control" placeholder="Enter something about yourself"
th:value="${user.about}" name="about"></textarea>
</div>
<!-- terms and conditions -->
<div class="form-group form-check text-center">
<input type="checkbox" class="form-check-input" id="agreement" name="agreement" />
<label for="agreement">Accept terms and conditions</label>
</div>
<div class="container text-center">
<button type="submit" class="btn bg-primary text-white">Submit</button>
<button class="btn btn-warning text-white" type="reset">Reset</button>
</div>
</form>
</div>
</div>
</div>
</div>
</section>
</body>
</html>
主控制器页面
package com.smart.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.smart.dao.UserRepository;
import com.smart.entities.User;
@Controller
public class HomeController {
@Autowired
private UserRepository userRepository;
@RequestMapping("/")
public String home() {
return "home";
}
@RequestMapping("/about")
public String about(Model model) {
model.addAttribute("title", "About - Smart Contact Manager");
return "about";
}
@RequestMapping("/signup")
public String signup(Model model) {
model.addAttribute("title", "Register - Smart Contact Manager");
model.addAttribute("user", new User());
return "signup";
}
// this is Handler for register user
@RequestMapping(value = "/do_register", method = RequestMethod.POST)
public String registerUser(@ModelAttribute("user") User user,
@RequestParam(value = "agreement", defaultValue = "false") boolean agreement, Model model) {
System.out.println(agreement);
System.out.println(user);
return "signup";
}
}
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.5.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.smart</groupId>
<artifactId>smartcontactmanager</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>smartcontactmanager</name>
<description>First Spring boot project</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</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>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>compile</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
型号类别
package com.smart.entities;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "USER")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@Column(unique = true)
private String email;
private String passWord;
private String role;
private boolean enabled;
private String imageUrl;
@Column(length = 500)
private String about;
// create a seperate table for mapping
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
private List<Contact> contacts = new ArrayList<>();
public User() {
super();
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getAbout() {
return about;
}
public void setAbout(String about) {
this.about = about;
}
public List<Contact> getContacts() {
return contacts;
}
public void setContacts(List<Contact> contacts) {
this.contacts = contacts;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", email=" + email + ", passWord=" + passWord + ", role=" + role
+ ", enabled=" + enabled + ", imageUrl=" + imageUrl + ", about=" + about + ", contacts=" + contacts
+ "]";
}
}
当我尝试登录时,它会给我一个:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Jul 14 09:37:29 IST 2021
There was an unexpected error (type=Not Found, status=404).
No message available
我已经尽了一切努力来解决这个问题,但它仍然不起作用
th:action="@{/do_register}"
这样写u忘记了斜杠并删除了@ComponentScan