gpt4 book ai didi

java - Tomcat/Spring 集成返回 404

转载 作者:搜寻专家 更新时间:2023-11-01 02:14:32 25 4
gpt4 key购买 nike

我遇到了与 HTTP Status 404 on Spring 3.1 MVC app 类似的问题

但我没有犯与发帖人相同的错误,但我仍然收到 404。

web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<display-name>Test</display-name>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

我可以加载 index.jsp 但不能加载实际的 Controller 。

dispatcher-servlet.xml 文件:

<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<!-- enable Spring’s component scanning -->
<context:component-scan base-package="com.test" />

<!-- process the @RequestMapping annotations at the class level -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

<!-- process the @RequestMapping annotations at the method level -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

Controller

package com.test.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;

@Controller
public class BenchController {

@RequestMapping(value = "/bench", method = RequestMethod.GET)
@ResponseBody
public String welcome(Model model) {
return "yo";
}

我可以访问 localhost:8080/Test/index.jsp,但 localhost:8080/Test/bench 返回 404。

如果有帮助的话,我正在使用 eclipse。

编辑 1:

这是 logging.properties。除了 out 日志,我找不到任何日志。

handlers = 1catalina.org.apache.juli.FileHandler, 2localhost.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler

.handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler

############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

1catalina.org.apache.juli.FileHandler.level = FINE
1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.FileHandler.prefix = catalina.

2localhost.org.apache.juli.FileHandler.level = FINE
2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.FileHandler.prefix = localhost.

java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.FileHandler

# For example, set the com.xyz.foo logger to only log SEVERE
# messages:
#org.apache.catalina.startup.ContextConfig.level = FINE
#org.apache.catalina.startup.HostConfig.level = FINE
#org.apache.catalina.session.ManagerBase.level = FINE
#org.apache.catalina.core.AprLifecycleListener.level=FINE

有趣的是,我不确定在哪里定义了 catalina.base 变量。

编辑 2:

pom.xml

<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.test</groupId>
<artifactId>Test</artifactId>
<version>0.1</version>
<packaging>war</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.1.0.RELEASE</spring.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>/WebContent/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
</dependencies>
</project>

我没有在我的依赖树中看到 log4j,即使它在我的 pom 文件中也是如此。

编辑 3:

这是我在 eclipse 中创建项目的方式:

  1. 新建 Maven 项目(m2 eclipse 插件)
  2. 我选择了简单项目
  3. 填写group/artificat id并选择war作为我的包装
  4. 我在项目文件夹窗口中右键单击我的项目并选择属性
  5. 我点击了 Project Facets 并在出现提示时进行了转换
  6. 我选择了“动态网络模块”并点击确定

完成所有这些后,我开始添加调度程序、用 spring 更新 pom 等。

编辑 4:

好吧,我终于到了某个地方。在 web.xml 中注释掉这一行后:

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

我能够通过 tomcat 管理器直接部署应用程序。现在我得到了

java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml]

它似乎试图在/WEB-INF 文件夹下找到 dispatcher-servlet.xml。查看war 文件后,我根本找不到该文件。每次我将 servlet xml 放在该文件夹中时,它似乎都被删除了。我可以将它放在任何其他文件夹下,但出于某种原因,即使在更新 web.xml 以引用新位置后,它似乎仍在/WEB-INF 下搜索文件。

编辑 5:

这是我的 web.xml 的更新

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd " 版本="2.5">

  <display-name>Test</display-name>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/applicationContext.xml</param-value>
</context-param>

<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>WEB-INF/classes/log4j.properties</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

</web-app>

显式添加applicationContext和dispatcher-servlet的位置后。 catalina.out 说:

Mapped URL path [/bench] onto handler 'benchController'
Mapped URL path [/bench.*] onto handler 'benchController'
Mapped URL path [/bench/] onto handler 'benchController'
Mapped URL path [/bench] onto handler 'benchController'
Mapped URL path [/bench/*] onto handler 'benchController'
FrameworkServlet 'dispatcher': initialization completed in 245 ms

但是当转到 localhost:8080/Test/bench - 它仍然返回 404。当我通过 tomcat 管理器部署时,我无法访问 index.jsp,但如果我通过 eclipse 部署,我可以。 catalina.out 没有其他输出。

日志:

127.0.0.1 - - [20/Feb/2012:14:41:13 -0800] "GET /Test/ HTTP/1.1" 200 182
127.0.0.1 - - [20/Feb/2012:14:41:19 -0800] "GET /Test/ HTTP/1.1" 200 182
127.0.0.1 - - [20/Feb/2012:14:41:24 -0800] "GET /Test/bench HTTP/1.1" 404 1012

解决方案:

我按照下面和项目文件夹内提供的说明进行操作,从命令行运行 mvn eclipse:eclipse -Dwtpversion=2.0 以便 eclipse 可以正确导入项目。我正在研究如何通过 Eclipse 来完成这一切,以避免每次都必须手动执行此操作。

最佳答案

您提到您想学习如何从头开始制作 Spring MVC 网络应用程序 - 以下是使用您的配置生成工作应用程序的步骤列表:

1) 创建文件夹a

2) 在该文件夹中创建一个文件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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<name>test</name>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<properties>
<org.springframework.version>3.0.5.RELEASE</org.springframework.version>
</properties>
<dependencies>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>test</warName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

3) 在文件夹 a 中创建子文件夹 src/main/java/test 并将文件 YoController.java 放在那里:

package test;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class YoController {

@RequestMapping(value = "/bench", method = RequestMethod.GET)
@ResponseBody
public String welcome(Model model) {
return "yo";
}
}

4) 创建子文件夹 src/main/webapp/WEB-INF 并将两个文件放在那里,web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>

dispatcher-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="test" />

</beans>

5) 创建子文件夹 src/main/resources 并将 log4j.properties 文件放在那里:

log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

6) 在文件夹 a 的命令提示符下键入:mvn package

7) 拷贝target/test.war到Tomcat的webapps文件夹

8) 启动 Tomcat 并转到 URL localhost:8080/test/bench

我刚刚完成了所有步骤并且成功了。日志记录会显示 Spring 消息,如果出现问题,这些消息会告诉您。

关于java - Tomcat/Spring 集成返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9346727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com