我是网络应用的初学者。 我试图运行第一个应用程序,但是发生了错误。 我复制了书中的所有内容,一切正常,在我的地方不是...通常。我一直在寻找解决方案的时间。找不到。 我正在使用10.02 java,Apache Tomcat / 9.0.12和IntelliJ。
这是我的文件结构:
这就是Tomcat的配置:
HomeController
package com.packt.webstore.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/")
public String welcome(Model model) {
model.addAttribute("greeting", "Hello");
model.addAttribute("tagline", "In the internet");
return "welcome";
}
}
welcome.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<title>Witaj</title>
</head>
<body>
<section>
<div class="jumbotron">
<div class="container">
<h1> ${greeting} </h1>
<p> ${tagline} </p>
</div>
</div>
</section>
</body>
</html>
DefaultServletservlet.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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.packt.webstore"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>DefaultServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DefaultServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.packt</groupId>
<artifactId>webstore</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build><plugins><plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin></plugins></build>
</project>
这是堆栈跟踪:
C:\Users\Admin\Desktop\apache-tomcat-9.0.12\bin\catalina.bat run
[2018-09-20 04:30:29,254] Artifact webstore:war exploded: Waiting for server connection to start artifact deployment...
Using CATALINA_BASE: "C:\Users\Admin\.IntelliJIdea2018.2\system\tomcat\Unnamed_webstore_2"
Using CATALINA_HOME: "C:\Users\Admin\Desktop\apache-tomcat-9.0.12"
Using CATALINA_TMPDIR: "C:\Users\Admin\Desktop\apache-tomcat-9.0.12\temp"
Using JRE_HOME: "C:\Program Files\Java\jdk-10.0.2"
Using CLASSPATH: "C:\Users\Admin\Desktop\apache-tomcat-9.0.12\bin\bootstrap.jar;C:\Users\Admin\Desktop\apache-tomcat-9.0.12\bin\tomcat-juli.jar"
20-Sep-2018 16:30:31.591 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/9.0.12
20-Sep-2018 16:30:31.596 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Sep 4 2018 22:13:41 UTC
20-Sep-2018 16:30:31.596 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 9.0.12.0
20-Sep-2018 16:30:31.596 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Windows 7
20-Sep-2018 16:30:31.597 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 6.1
20-Sep-2018 16:30:31.597 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
20-Sep-2018 16:30:31.597 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: C:\Program Files\Java\jdk-10.0.2
20-Sep-2018 16:30:31.597 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 10.0.2+13
20-Sep-2018 16:30:31.598 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: "Oracle Corporation"
20-Sep-2018 16:30:31.598 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: C:\Users\Admin\.IntelliJIdea2018.2\system\tomcat\Unnamed_webstore_2
20-Sep-2018 16:30:31.598 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: C:\Users\Admin\Desktop\apache-tomcat-9.0.12
20-Sep-2018 16:30:31.603 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED
20-Sep-2018 16:30:31.603 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED
20-Sep-2018 16:30:31.604 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
20-Sep-2018 16:30:31.604 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=C:\Users\Admin\.IntelliJIdea2018.2\system\tomcat\Unnamed_webstore_2\conf\logging.properties
20-Sep-2018 16:30:31.604 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
20-Sep-2018 16:30:31.604 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote=
20-Sep-2018 16:30:31.604 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.port=1099
20-Sep-2018 16:30:31.605 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.ssl=false
20-Sep-2018 16:30:31.605 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.authenticate=false
20-Sep-2018 16:30:31.605 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.rmi.server.hostname=127.0.0.1
20-Sep-2018 16:30:31.605 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
20-Sep-2018 16:30:31.606 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
20-Sep-2018 16:30:31.606 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
20-Sep-2018 16:30:31.606 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=C:\Users\Admin\.IntelliJIdea2018.2\system\tomcat\Unnamed_webstore_2
20-Sep-2018 16:30:31.606 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=C:\Users\Admin\Desktop\apache-tomcat-9.0.12
20-Sep-2018 16:30:31.606 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=C:\Users\Admin\Desktop\apache-tomcat-9.0.12\temp
20-Sep-2018 16:30:31.607 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent 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\Java\jdk-10.0.2\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;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:\Program Files\Git\cmd;C:\Users\Admin\AppData\Local\GitHubDesktop\bin;.]
20-Sep-2018 16:30:31.690 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
20-Sep-2018 16:30:31.719 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
20-Sep-2018 16:30:31.732 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
20-Sep-2018 16:30:31.735 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
20-Sep-2018 16:30:31.736 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 949 ms
20-Sep-2018 16:30:31.880 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
20-Sep-2018 16:30:31.880 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/9.0.12
20-Sep-2018 16:30:31.909 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
20-Sep-2018 16:30:31.930 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
20-Sep-2018 16:30:31.937 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 200 ms
Connected to server
[2018-09-20 04:30:32,156] Artifact webstore:war exploded: Artifact is being deployed, please wait...
[2018-09-20 04:30:33,434] Artifact webstore:war exploded: Artifact is deployed successfully
[2018-09-20 04:30:33,434] Artifact webstore:war exploded: Deploy took 1 278 milliseconds
20-Sep-2018 16:30:41.920 INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [C:\Users\Admin\Desktop\apache-tomcat-9.0.12\webapps\manager]
20-Sep-2018 16:30:42.028 INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\Users\Admin\Desktop\apache-tomcat-9.0.12\webapps\manager] has finished in [108] ms
这就是页面的外观:(我尝试过http://localhost:8080/
和http://localhost:8080/webstore
`
///编辑 根据@ Ce7的帖子更新了项目。
项目结构
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.packt</groupId>
<artifactId>webstore</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>webapp</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
web.xml
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>DefaultServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/DefaultServlet-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DefaultServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Stacktrace (我不得不将其粘贴到pastebin上,因为问题太长了)
页面
答案 0 :(得分:1)
正确配置pom,当前配置是创建jar文件,而不是war,并且您没有maven war插件将Web资源(jsp,web.xml等)放入war文件
<groupId>com.packt</groupId>
<artifactId>webstore</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>webapp</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
将您的WEB-INF文件夹放在一个根文件夹下,例如webapp / WEB-INF,此处webapp文件夹与src文件夹处于同一级别 https://support.google.com/firebase/answer/7201382
将您的DefaultServeletservlet.xml重命名为
DefaultServelet-servlet.xml
答案 1 :(得分:0)
上述问题是您将servlet configuration file
命名为DefaultServletservlet.xml
。
相反,您应该rename
作为DefaultServlet-servlet.xml
。在检查xml configuration
时,spring将文件名与后缀-servlet
匹配。由于找不到DefaultServlet-servlet.xml
,因此出现错误。