尽管tomcat已运行,Spring MVC无法映射方法?

时间:2019-02-25 15:22:46

标签: java spring eclipse tomcat tomcat9

我使用了Spring 5,并使用了tomcat运行应用程序。但是当我运行tomcat时,它运行成功,但是我可以从应用程序中访问我的方法。 Pom文件:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.baotrung</groupId>
    <artifactId>demo-spring-mvc</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-spring-mvc Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.hamcrest</groupId>
                    <artifactId>hamcrest-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.5.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.15.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>javax.servlet.jsp.jstl-api</artifactId>
            <version>1.2.1</version>
        </dependency>

        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-maven-plugin -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-api -->
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <dependencies>
                <dependency>
                    <groupId>org.ow2.asm</groupId>
                    <artifactId>asm</artifactId>
                    <version>5.0.2</version>
                </dependency>
                <dependency>
                    <groupId>org.ow2.asm</groupId>
                    <artifactId>asm-commons</artifactId>
                    <version>5.0.2</version>
                </dependency>
            </dependencies>
        </plugin>
    </dependencies>

    <build>
        <finalName>demo-spring-mvc</finalName>
        <plugins>

            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <failOnMissingWebXml>true</failOnMissingWebXml>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>

CustomerController。

package com.baotrung.springmvc.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.baotrung.springmvc.entity.Customer;
import com.baotrung.springmvc.exception.CustomerNotFoundException;
import com.baotrung.springmvc.service.CustomerService;

@Controller
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @GetMapping("/findAll")
    public String showAllCustomers(Model model) {
        List<Customer> customers = customerService.getAllCustomer();
        if (customers.isEmpty()) {
            throw new CustomerNotFoundException("Can't find any customer from database");
        }
        model.addAttribute("customer", customers);
        return "list-customer";
    }

    @GetMapping("/addCustomer")
    public String showFormForAdd(Model model) {
        Customer customer = new Customer();
        model.addAttribute("customer", customer);
        return "add-customer";
    }

    @PostMapping("/saveCustomer")
    public String addCustomer(@ModelAttribute Customer customer) {
        customerService.saveCustomer(customer);
        return "redirect:/customer/findAll";
    }

    @GetMapping("/updateCustomer")
    public String showFormUpdate(@RequestParam("customerId") int theId, Model model) {
        Customer customer = customerService.getCustomer(theId);
        model.addAttribute(customer);
        return "customer-form";
    }

    @GetMapping("/deleteCustomer")
    public String deleteCustomer(@RequestParam("customerId") int theId) {
        customerService.deleteCustomer(theId);
        return "redirect:/customer/findAll";
    }

}

当我运行tomcat时,它运行成功。

Feb 25, 2019 9:57:18 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.5.38
Feb 25, 2019 9:57:18 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Feb 5 2019 11:42:42 UTC
Feb 25, 2019 9:57:18 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.5.38.0
Feb 25, 2019 9:57:18 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 10
Feb 25, 2019 9:57:18 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.0
Feb 25, 2019 9:57:18 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          x86
Feb 25, 2019 9:57:18 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files (x86)\Java\jdk1.8.0_181\jre
Feb 25, 2019 9:57:18 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_181-b13
Feb 25, 2019 9:57:18 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Feb 25, 2019 9:57:18 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         G:\Ebooktinhoc\Java\Spring\EclipseSpring\.metadata\.plugins\org.eclipse.wst.server.core\tmp3
Feb 25, 2019 9:57:18 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         G:\Ebooktinhoc\Java\apache-tomcat-8.5.38-windows-x64\apache-tomcat-8.5.38
Feb 25, 2019 9:57:18 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=G:\Ebooktinhoc\Java\Spring\EclipseSpring\.metadata\.plugins\org.eclipse.wst.server.core\tmp3
Feb 25, 2019 9:57:18 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=G:\Ebooktinhoc\Java\apache-tomcat-8.5.38-windows-x64\apache-tomcat-8.5.38
Feb 25, 2019 9:57:18 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=G:\Ebooktinhoc\Java\Spring\EclipseSpring\.metadata\.plugins\org.eclipse.wst.server.core\tmp3\wtpwebapps
Feb 25, 2019 9:57:18 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=G:\Ebooktinhoc\Java\apache-tomcat-8.5.38-windows-x64\apache-tomcat-8.5.38\endorsed
Feb 25, 2019 9:57:18 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Feb 25, 2019 9:57:18 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Xbootclasspath:C:\Program Files (x86)\Java\jdk1.8.0_181\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_181\jre\lib\rt.jar;C:\Program Files (x86)\Java\jdk1.8.0_181\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_181\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_181\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_181\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_181\lib\tools.jar
Feb 25, 2019 9:57:18 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files (x86)\Java\jdk1.8.0_181\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_201/bin/server;C:/Program Files/Java/jre1.8.0_201/bin;C:/Program Files/Java/jre1.8.0_201/lib/amd64;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Users\Tran Bao Trung\AppData\Local\Microsoft\WindowsApps;C:\Users\Tran Bao Trung\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\Tran Bao Trung\AppData\Roaming\npm;C:\Users\Tran Bao Trung\Desktop;;.]
Feb 25, 2019 9:57:18 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Feb 25, 2019 9:57:19 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Feb 25, 2019 9:57:19 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Feb 25, 2019 9:57:19 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Feb 25, 2019 9:57:19 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1389 ms
Feb 25, 2019 9:57:19 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Feb 25, 2019 9:57:19 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.5.38
Feb 25, 2019 9:57:20 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Feb 25, 2019 9:57:20 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Feb 25, 2019 9:57:20 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 631 ms

但是当我通过以下链接访问链接时: http://localhost:8080/demo-spring-mvc/customer/findAll 。丢我404找不到?我不明白为什么?请帮忙?谢谢。

0 个答案:

没有答案