intellij idea 运行 spring mvc 应用程序中的问题

Problem in running a spring mvc app in intellij idea

我在教程中看到他想创建一个 Spring MVC 应用程序,他还创建了以下文件,但是教程的延续是在 eclipse 中,我使用 IntelliJ 并配置了 tomcat,当我 运行 上面的程序时,tomcat 启动但 HTML 页面打不开。

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">

    <display-name>spring-org.isoft.mvc-demo</display-name>

    <absolute-ordering />
    

    <servlet>
        <servlet-name>isoftDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>org/isoft/mvc/configuration/spring-mvc-demo-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    
    <servlet-mapping>
        <servlet-name>isoftDispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
</web-app>

spring-mvc-demo-servlet.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    
    <context:component-scan base-package="org.isoft" />

    <mvc:annotation-driven/>

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="org/isoft/mvc/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

家庭控制器:

@Controller
public class HomeController {

    @RequestMapping("/")
    public String showHomePage(){
        System.out.println("Home Page started!");
        return "home-page";
    }
}

家-page.jsp:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=yes, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Home Page</title>
</head>

<body>
    <h1>The first Spring MVC app</h1>

</body>
</html>

tomcat configuration

输出:

"C:\Program Files\Apache Software Foundation\Tomcat 9.0\bin\catalina.bat" run
[2022-04-25 02:51:42,855] Artifact artifact: Waiting for server connection to start artifact deployment…
Using CATALINA_BASE:   "C:\Users\User\AppData\Local\JetBrains\IntelliJIdea2021.3\tomcate74877d-73ba-4dbb-a1e4-c6dfbb2f5fb3"
Using CATALINA_HOME:   "C:\Program Files\Apache Software Foundation\Tomcat 9.0"
Using CATALINA_TMPDIR: "C:\Program Files\Apache Software Foundation\Tomcat 9.0\temp"
Using JRE_HOME:        "C:\Program Files\Java\jdk-17.0.1"
Using CLASSPATH:       "C:\Program Files\Apache Software Foundation\Tomcat 9.0\bin\bootstrap.jar;C:\Program Files\Apache Software Foundation\Tomcat 9.0\bin\tomcat-juli.jar"
Using CATALINA_OPTS:   ""
NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
25-Apr-2022 14:51:44.254 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name:   Apache Tomcat/9.0.62
25-Apr-2022 14:51:44.258 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Mar 31 2022 14:34:15 UTC
25-Apr-2022 14:51:44.258 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 9.0.62.0
25-Apr-2022 14:51:44.259 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Windows 10
25-Apr-2022 14:51:44.259 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            10.0
25-Apr-2022 14:51:44.259 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
25-Apr-2022 14:51:44.259 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             C:\Program Files\Java\jdk-17.0.1
25-Apr-2022 14:51:44.259 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           17.0.1+12-LTS-39
25-Apr-2022 14:51:44.260 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
25-Apr-2022 14:51:44.260 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         C:\Users\User\AppData\Local\JetBrains\IntelliJIdea2021.3\tomcate74877d-73ba-4dbb-a1e4-c6dfbb2f5fb3
25-Apr-2022 14:51:44.260 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         C:\Program Files\Apache Software Foundation\Tomcat 9.0
25-Apr-2022 14:51:44.263 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED
25-Apr-2022 14:51:44.273 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED
25-Apr-2022 14:51:44.273 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util=ALL-UNNAMED
25-Apr-2022 14:51:44.273 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util.concurrent=ALL-UNNAMED
25-Apr-2022 14:51:44.274 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
25-Apr-2022 14:51:44.274 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=C:\Users\User\AppData\Local\JetBrains\IntelliJIdea2021.3\tomcate74877d-73ba-4dbb-a1e4-c6dfbb2f5fb3\conf\logging.properties
25-Apr-2022 14:51:44.274 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
25-Apr-2022 14:51:44.274 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote=
25-Apr-2022 14:51:44.274 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.port=1099
25-Apr-2022 14:51:44.275 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.ssl=false
25-Apr-2022 14:51:44.275 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.password.file=C:\Users\User\AppData\Local\JetBrains\IntelliJIdea2021.3\tomcate74877d-73ba-4dbb-a1e4-c6dfbb2f5fb3\jmxremote.password
25-Apr-2022 14:51:44.275 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.access.file=C:\Users\User\AppData\Local\JetBrains\IntelliJIdea2021.3\tomcate74877d-73ba-4dbb-a1e4-c6dfbb2f5fb3\jmxremote.access
25-Apr-2022 14:51:44.275 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.rmi.server.hostname=127.0.0.1
25-Apr-2022 14:51:44.276 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
25-Apr-2022 14:51:44.276 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
25-Apr-2022 14:51:44.276 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
25-Apr-2022 14:51:44.276 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=C:\Users\User\AppData\Local\JetBrains\IntelliJIdea2021.3\tomcate74877d-73ba-4dbb-a1e4-c6dfbb2f5fb3
25-Apr-2022 14:51:44.276 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 9.0
25-Apr-2022 14:51:44.276 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=C:\Program Files\Apache Software Foundation\Tomcat 9.0\temp
25-Apr-2022 14:51:44.285 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.32] using APR version [1.7.0].
25-Apr-2022 14:51:44.285 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [true].
25-Apr-2022 14:51:44.285 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
25-Apr-2022 14:51:44.291 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1n  15 Mar 2022]
25-Apr-2022 14:51:44.742 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8081"]
25-Apr-2022 14:51:44.788 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [887] milliseconds
25-Apr-2022 14:51:44.893 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
25-Apr-2022 14:51:44.893 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/9.0.62]
25-Apr-2022 14:51:44.910 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8081"]
25-Apr-2022 14:51:44.996 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [207] milliseconds
Connected to server
[2022-04-25 02:51:45,181] Artifact artifact: Artifact is being deployed, please wait…
[2022-04-25 02:51:45,202] Artifact artifact: Error during artifact deployment. See server log for details.
[2022-04-25 02:51:45,203] Artifact artifact: com.intellij.javaee.oss.admin.jmx.JmxAdminException: com.intellij.execution.ExecutionException: C:\Users\User\IdeaProjects\spring_demo1\out\artifacts\artifact not found for the web module.

IDE 尝试 运行 使用工件 \spring_demo1\out\artifacts\artifact 的应用程序,但我认为只有当您这样做时才会出现这种情况不要使用 Gradle 或 Maven。但是由于您使用 Spring Framework,我相信您使用的是其中之一。 对于 Gradle,您在 /build/lib 文件夹 中有工件,对于 /target 中的 Maven ]文件夹。

配置有问题。

  1. 可能 IDE 无法将您的项目识别为 Gradle/maven。
  2. 您的配置图片中的 'build artifact' 作业不工作,或者在不同的地方创建了一个工件。