我正在尝试用Spring3.0.x编写spring应用程序(是的,我想要那个版本)。做了一个非常简单的返回字符串的方法。
package fitnessapp;
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;
@Controller
public class Hello {
@RequestMapping(value = "/sayHello", method = RequestMethod.GET)
@ResponseBody
public String sayHello() {
return "hello!";
}
}
打电话后http://localhost:8080/你好我得到404不知道为什么。
HTTP/1.1 404
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 682
Date: Fri, 04 Dec 2020 18:44:04 GMT
Keep-Alive: timeout=20
Connection: keep-alive
<!doctype html>
<html lang="en">
<head><title>HTTP Status 404 – Not Found</title>
<style type="text/css">body {
font-family: Tahoma, Arial, sans-serif;
}
...
</head>
<body><h1>HTTP Status 404 – Not Found</h1>
<hr class="line"/>
<p><b>Type</b> Status Report</p>
<p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing
to disclose that one exists.</p>
<hr class="line"/>
<h3>Apache Tomcat/8.5.60</h3></body>
</html>
我正在使用Tomcat8.5.60。
我的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>org.example</groupId>
<artifactId>stara</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
</dependencies>
</project>
网站.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>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring/webcontext/DispatcherServlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
dispartcherservlet-context.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:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="fitnessapp"/>
<mvc:annotation-driven/>
</beans>
在我得到的日志中
04-Dec-2020 21:22:38.073 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2f66c1fd: defining beans [hello,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0]; root of factory hierarchy
04-Dec-2020 21:22:38.310 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler Mapped URL path [/sayHello] onto handler 'hello'
04-Dec-2020 21:22:38.310 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler Mapped URL path [/sayHello.*] onto handler 'hello'
04-Dec-2020 21:22:38.311 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler Mapped URL path [/sayHello/] onto handler 'hello'
你能告诉我我做错了什么让这个控制器工作吗?
1条答案
按热度按时间lsmepo6l1#
试试这个:
应该变成:
并将dispartcherservlet-context.xml正确重命名为myservlet-servlet.xml。
您在pom中的依赖关系应该是:
或者类似的。