当前位置: 首页 > 知识库问答 >
问题:

Spring Boot-GET /new无映射

秦涵涤
2023-03-14

我目前正在Spring Boot中开发常见问题Web应用程序。我的索引页面运行良好,但如果我想打开另一个超文本标记语言页面,我的浏览器中会出现404未找到错误页面,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:/";
    }

指数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>

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>

应用属性

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

共有1个答案

澹台阳秋
2023-03-14

您正在以/new方法返回“new_product”。您必须创建一个新的产品视图(new\u product.jsp/new\u product.html.)在里面做任何你想做的事,然后一切都会好起来,你会得到200的回复。

如果文件已经存在,则可能路径不正确,您必须对其进行配置。尝试将html文件放在静态文件夹中,否则请在应用程序中添加路径。属性

 类似资料: