一个简单且带spring mvc的hello word的java web项目,启动tomcat成功,输入正确的请求url,回车页面一直显示404,如何解决?

weixin_38974328 2019-04-19 10:49:41
我试图通过有关spring mvc教程搭建一个简单且带spring mvc的hello word的非maven的javaWeb项目, tomcat启动成功,在浏览器url地址栏中,输入正确的请求url(是正确的),回车页面一直显示404,不过只要一回车,而且是带项目路径的url的话。eclipse的console栏中会出现诸如如下的两行信息,没有报错的,是的,但会出现下列两行信息

四月 19, 2019 10:05:27 下午 org.springframework.web.servlet.DispatcherServlet noHandlerFound
警告: No mapping found for HTTP request with URI [/cwManage/index] in DispatcherServlet with name 'springDispatcherServlet'

org.springframework.web.servlet.DispatcherServlet noHandlerFound和 No mapping found for HTTP request with URI [/cwManage/index] in DispatcherServlet with name 'springDispatcherServlet',

这两个在网上也被搜到过是关于spring mvc的异常问题,我试图通过围绕上面的线索通过网络搜索答案解决,但是目前还是找不到解决方案。我目前根本不知道是什么原因导致的,所以请求大家帮帮忙。我的项目是通过eclipce创建的Dynamic Web Project,而且eclipse应该是这一年内出的版本,建的java web项目并不是maven项目,所有的jar包都是手动导入的。同时我也不接受任何有关弄成maven项目的解决方式和答案


我目前已经排除了下列问题
tomcat,(我有其他的两个项目也是spring mvc项目,都是jre1.8,但不是我的,是别人给我的,tomcat启动没有问题,能正常浏览网站和内容)
eclipse(在两个eclipse上都试过,会重复同样的问题),
jre版本
spring的jar包

以下是问题的相关信息
web.xml也是复制网上教程里的,spring路径没有问题,可以确定的是,main函数测试spring注入是没有问题的。

<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>cwManage</display-name>


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

<!-- 配置DispatchcerServlet -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置Spring mvc下的配置文件的位置和名称 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:resources/spring-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

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

</web-app>


Spring配置文件内容

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

<!-- 使用Annotation自动注册Bean,解决事物失效问题:在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解。 -->
<context:component-scan base-package="com.tc"><!-- base-package 如果多个,用“,”分隔 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value="/WEB-INF/views/"></property>
<property name = "suffix" value = ".jsp"></property>
</bean>
</beans>


Controller内容


@Controller
package com.tc.test.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {

@RequestMapping("/index")
public String hello(){
System.out.println("hello world");
return "result";
}
}

浏览器回车输入http://127.0.0.1:8888/cwManage/index ,这个应该是正确的,不过回车提示org.springframework.web.servlet.DispatcherServlet noHandlerFound和警告: No mapping found for HTTP request with URI [/cwManage/index] in DispatcherServlet with name 'springDispatcherServlet'。当然输入带项目名称的其他url也一样

全部jar包


测试项目时使用的第一个eclipce版本信息
Spring Tool Suite 4

Version: 4.1.2.RELEASE
Build Id: 201902210640

Copyright (c) 2007 - 2019 Pivotal, Inc.
All rights reserved. Visit http://spring.io/tools4

This product includes software developed by the
Apache Software Foundation http://www.apache.org

测试项目时第二个eclipce信息

Eclipse Java EE IDE for Web Developers.

Version: Mars.2 Release (4.5.2)
Build id: 20160218-0600

(c) Copyright Eclipse contributors and others 2000, 2016. All rights reserved. Eclipse and the Eclipse logo are trademarks of the Eclipse Foundation, Inc., https://www.eclipse.org/. The Eclipse logo cannot be altered without Eclipse's permission. Eclipse logos are provided for use under the Eclipse logo and trademark guidelines, https://www.eclipse.org/logotm/. Oracle and Java are trademarks or registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.

项目百度云地址,本问题所搭建时所用的java web项目
https://pan.baidu.com/s/1iqxbh_f1jTvRMlI-cvHHcA
提取码:fqjw

控制台信息
edit\;C:\Users\root\AppData\Roaming\npm;.
四月 19, 2019 10:05:19 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-bio-8888"]
四月 19, 2019 10:05:19 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-bio-8006"]
四月 19, 2019 10:05:19 下午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 923 ms
四月 19, 2019 10:05:19 下午 org.apache.catalina.core.StandardService startInternal
信息: Starting service Catalina
四月 19, 2019 10:05:19 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.69
四月 19, 2019 10:05:19 下午 org.apache.catalina.startup.TldConfig execute
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
四月 19, 2019 10:05:20 下午 org.apache.catalina.startup.HostConfig deployDescriptor
信息: Deploying configuration descriptor F:\MyWorkSpace\workspace_EclipceSpringToolSuite\.metadata\.plugins\org.eclipse.wst.server.core\tmp5\conf\Catalina\localhost\cwManage.xml
四月 19, 2019 10:05:20 下午 org.apache.catalina.startup.SetContextPropertiesRule begin
警告: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:cwManage' did not find a matching property.
四月 19, 2019 10:05:21 下午 org.apache.catalina.startup.TldConfig execute
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
四月 19, 2019 10:05:21 下午 org.apache.catalina.core.ApplicationContext log
信息: No Spring WebApplicationInitializer types detected on classpath
四月 19, 2019 10:05:22 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'springDispatcherServlet'
四月 19, 2019 10:05:22 下午 org.springframework.web.servlet.FrameworkServlet initServletBean
信息: FrameworkServlet 'springDispatcherServlet': initialization started
四月 19, 2019 10:05:22 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing WebApplicationContext for namespace 'springDispatcherServlet-servlet': startup date [Fri Apr 19 22:05:22 CST 2019]; root of context hierarchy
四月 19, 2019 10:05:22 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [resources/spring-context.xml]
四月 19, 2019 10:05:23 下午 org.springframework.web.servlet.FrameworkServlet initServletBean
信息: FrameworkServlet 'springDispatcherServlet': initialization completed in 1191 ms
四月 19, 2019 10:05:23 下午 org.apache.catalina.startup.HostConfig deployDescriptor
信息: Deployment of configuration descriptor F:\MyWorkSpace\workspace_EclipceSpringToolSuite\.metadata\.plugins\org.eclipse.wst.server.core\tmp5\conf\Catalina\localhost\cwManage.xml has finished in 3,215 ms
四月 19, 2019 10:05:23 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8888"]
四月 19, 2019 10:05:23 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-bio-8006"]
四月 19, 2019 10:05:23 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 4142 ms
四月 19, 2019 10:05:27 下午 org.springframework.web.servlet.DispatcherServlet noHandlerFound
警告: No mapping found for HTTP request with URI [/cwManage/index] in DispatcherServlet with name 'springDispatcherServlet'
四月 19, 2019 10:05:28 下午 org.springframework.web.servlet.DispatcherServlet noHandlerFound
警告: No mapping found for HTTP request with URI [/cwManage/index] in DispatcherServlet with name 'springDispatcherServlet'
四月 19, 2019 10:05:29 下午 org.springframework.web.servlet.DispatcherServlet noHandlerFound
警告: No mapping found for HTTP request with URI [/cwManage/index] in DispatcherServlet with name 'springDispatcherServlet'
四月 19, 2019 10:29:36 下午 org.springframework.web.servlet.DispatcherServlet noHandlerFound
警告: No mapping found for HTTP request with URI [/cwManage/indexsdfsd] in DispatcherServlet with name 'springDispatcherServlet'




说点问题以外的提示吧,我一年前是有过搭建过spring mvc框架的印象的,之后过了差不多一年没怎么搭建过java web项目框架,那个时候搭建教程跟现在的是差不多的,而且我搭建都后能运行,能正常访问页面,不过那个时候我用的是myeclipce。而现在搭建的软件平台是eclipce,我怀疑是不是eclipse中新建的java web项目相对于myeclipce有什么特色要求导致的,这几天在eclipce上为能搭建好springmvc浪费了不少时间了,要是再不行我就考虑一下strust2把
...全文
117 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
greengrass2011 2019-04-20
  • 打赏
  • 举报
回复
spring配置文件删除这句:<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 因为spring扫描不到该controller,导致404 另外在WEB-INF目录下面创建views文件夹,在views文件夹下面再创建result.jsp即可

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧