Spring Boot -没有GET /new映射



我目前正在Spring Boot中开发一个常见问题Web应用程序。我的索引页工作正常,但如果我想打开另一个HTML页面,我得到一个404未找到错误页面在我的浏览器和Intellij说:

WARN 8068 --- [nio-8080-exec-6] o.s.web.servlet.PageNotFound             : No mapping for GET /new

我怎样才能解决这个问题?

IndexController:

@Controller
public class IndexController {
@Autowired
private FAQService service;

@RequestMapping("/")                                          
public String viewHomePage(Model model) {
List<FAQ> listFaqs = service.listAll();
model.addAttribute("listFaqs", listFaqs);
return "index";
}

@RequestMapping("/home")                                           
public String backHomePage(Model model) {
List<FAQ> listFaqs = service.listAll();
model.addAttribute("listFaqs", listFaqs);
return "index";
}
@RequestMapping("/new")                                        
public String showNewProductForm(Model model) {
FAQ faq = new FAQ();
model.addAttribute("faq", faq);
return "new_product";
}

@RequestMapping(value = "/save", method = RequestMethod.POST)           
public String saveProduct(@ModelAttribute("faq") FAQ faq) {
service.save(faq);
return "redirect:/";
}

@RequestMapping("/edit/{id}")                                       
public ModelAndView showEditProductPage(@PathVariable(name = "id") int id) {
ModelAndView mav = new ModelAndView("edit_faq");
FAQ faq = service.get(id);
mav.addObject("faq", faq);
return mav;
}
@RequestMapping("/delete/{id}")                                     
public String deleteProduct(@PathVariable(name = "id") int id) {
service.delete(id);
return "redirect:/";
}

index . html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3" lang="en">
<link rel="stylesheet" type="text/css" th:href="@{/static/css/styles.css}"/>
<head>
<title>WebApp</title>
</head>
<body>
<div align="center"><h3 th:inline="text">Welcome [[${#httpServletRequest.remoteUser}]]</h3></div>
<div class="logout-btn" th:align="center">
<a th:href="@{/logout}"><button>Logout</button></a>
</div>
<div align="center">
<br>
<h1>Manage Excel Files</h1>
<th:block th:include="/_menu"></th:block>
<div class="ncontent-btn">
<a href="/new"><button>New Entry</button></a>
<a th:href="@{/excel/export}"><button>Export to Excel</button></a>
</div>
<br/>
</div>
</body>
</html>

POM.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<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>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>8.0.22</version>
</dependency>
<dependency>
<groupId>net.sf.supercsv</groupId>
<artifactId>super-csv</artifactId>
<version>2.4.0</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

application.properties

spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=5MB
spring.servlet.multipart.max-request-size=10MB
spring.mvc.dispatch-options-request=true
spring.datasource.continue-on-error=true
spring.mvc.static-path-pattern=/static/**
spring.jpa.open-in-view=false

我所有的HTML文件都在/resources/templates路径下。

在/new方法中返回"new_product"您必须创建一个new_product视图(new_product.jsp/new_product.html..),并在其中做任何您想做的事情,然后它就会好了,您将得到一个200的响应。

如果你的文件已经存在,可能路径不正确,你必须配置它。尝试将html文件放在静态文件夹中,否则在application.properties

中添加您的路径。

最新更新