【问题标题】:Spring Boot - No Mapping for GET /newSpring Boot - 没有 GET /new 的映射
【发布时间】:2021-04-29 06:34:06
【问题描述】:

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

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

我该如何解决这个问题?

索引控制器:

@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 路径中。

【问题讨论】:

  • 尝试在请求映射上添加方法,例如:@RequestMapping(value = "/new", method = RequestMethod.GET)
  • @FelipeBonfante 我已经试过了,不幸的是它没有用
  • 您是否有一个小的、可运行的示例来演示该问题。从我在这里看到的来看,似乎没有任何问题。您可以使用@GetMapping@PostMapping,但应该不会有什么不同。另外,您使用的是哪个 Spring Boot 版本?
  • 我也尝试了@GetMapping@PostMapping,正如你已经说过的那样,它没有任何区别。目前我正在使用 Spring Boot 版本 2.3.2。

标签: spring-boot thymeleaf


【解决方案1】:

您在 /new 方法中返回“new_product”。 你必须创建一个 new_product 视图 (new_product.jsp / new_product.html..) 并在里面做任何你想做的事情,然后就可以了,你会得到 200 响应。

如果您的文件已经存在,则可能路径不正确,您必须对其进行配置。尝试将 html 文件放在静态文件夹中,否则将路径添加到 application.properties

【讨论】:

  • 我尝试将 new_product.html 文件移动到静态文件夹中,但仍然是相同的 404 not found 错误。
  • 尝试添加属性 spring.mvc.view.prefix= spring.mvc.view.suffix=<.jsp or .html>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-21
  • 2019-07-13
  • 2020-07-30
  • 1970-01-01
  • 2021-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多