我有以下问题 - 我使用 com.bmuschko.tomcat
插件创建了一个名为“spring”的简单 Gradle 项目。但是当我运行 Gradle build 和 tomcatRun
任务并输入 http://localhost:8080/spring 时,我收到 404 错误。当然,在构建和服务器启动期间我没有收到任何错误消息。有build.gradle:
apply plugin: 'com.bmuschko.tomcat'
apply plugin: 'java'
apply plugin: 'war'
group 'training'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
def tomcatVersion = '9.0.17'
tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-logging-juli:9.0.0.M6",
"org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
}
tomcat {
httpProtocol = 'org.apache.coyote.http11.Http11Nio2Protocol'
ajpProtocol = 'org.apache.coyote.ajp.AjpNio2Protocol'
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.bmuschko:gradle-tomcat-plugin:2.5'
}
}
我还有一个名为 hello.jsp
的 jsp 文件放在 src/main/webapp
中:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello Gradle Web App</title>
</head>
<body>
<jsp:useBean id="greeting" class="test.Test"/>
<h3>${greeting.hello}</h3>
</body>
</html>
和Test.java
文件放在src/main/java/test
:
package test;
public class Test {
public String getHello() {
return "Hello Spring!";
}
}
如您所见,服务器似乎已成功启动:
17:46:12: Executing tasks 'build tomcatRun'...
> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :war UP-TO-DATE
> Task :assemble UP-TO-DATE
> Task :compileTestJava NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test NO-SOURCE
> Task :check UP-TO-DATE
> Task :build UP-TO-DATE
> Task :tomcatRun
Started Tomcat Server
The Server is running at http://localhost:8080/spring
最佳答案
正如评论中所指出的,您的 JSP 文件名为 hello.jsp
。
Tomcat 文档 (conf/web.xml
) 声明如下:
==================== Default Welcome File List =====================
When a request URI refers to a directory, the default servlet looks for a "welcome file" within that directory and, if present, to the corresponding resource URI for display. If no welcome files are present, the default servlet either serves a directory listing (see default servlet configuration on how to customize) or returns a 404 status, depending on the value of the listings setting.
默认的欢迎文件是:
- index.html
- index.htm,和
- index.jsp
那是什么意思呢?
当您将浏览器指向 http://localhost:8080/spring 时,请求 URL 中没有列出特定页面。 Tomcat 和其他 Web 服务器一样,仍然尝试提供一个页面,以便浏览器可以显示一些东西。默认情况下,如果URL中没有指定页面,则下发默认首页。由于您的文件名为 hello.jsp
,因此它不被视为欢迎文件,因此 Tomcat 会提供 404 错误。
您可以通过以下方式解决问题
- 将浏览器指向 http://localhost:8080/spring/hello.jsp
- 将
hello.jsp
重命名为index.jsp
- 在
web.xml
中自定义welcome-file-list
以包含hello.jsp
也就是说,问题不在于 com.bmuschko.tomcat
插件或您的构建文件。
关于java - 带有 Tomcat 插件的简单 Gradle 项目不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55577420/