求解决!springMVC入门案例中出现的问题

一、配置的jar包:

二、springMVC配置:

1.mvc.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:p="http://www.springframework.org/schema/p"
	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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	
	<!--1、开启注解管理对象IOC注解(重点掌握) :扫描包,扫描的是controller层-->
	<context:component-scan base-package="com.henu.controller" />
	<!--2、开启注解开发mvc(映射访问路径) MVC注解-->
	<!--配置注解驱动(重点掌握),会默认加载上边两个HandlerMapping,HandlerAdapter;使controller层注解生效-->
	<mvc:annotation-driven></mvc:annotation-driven>
</beans>

2.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SpringMVC</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
		<servlet-name>mvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>  
    	<servlet-name>mvc</servlet-name>  
    	<!--所有控制器的访问路径都要以.do结尾  -->
    	<url-pattern>*.do</url-pattern>  
	</servlet-mapping>
</web-app>

三、FirstController.java

package com.henu.controller;

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

@Controller
public class FirstController {
	@RequestMapping("/hello")
	public String hello() {
		System.out.println("控制器被访问了");
		return null;
	}
}

四、运行

URL  http://localhost:8080/SpringMVC/hello.do

运行结果

   控制台未出现“控制器被访问了”:

@RequestMapping("/hello.do")

要改成 .do 结尾的

mvc:annotation-driven

这段配置需要放在第一行

行,我试试

 

http://localhost:8080/SpringMVC/hello.do

1.上面这个路径中的SpringMVC是需要配置的,在idea的tomcat中配置项目访问路径

2.如果不配置,一般为空或者默认使用当前工程的报名加后缀的方式,在tomcat中也可以看到

3.看你代码,你学习的资料太老了,换个新的吧

3月 30, 2021 1:34:44 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server version:        Apache Tomcat/8.5.38
3月 30, 2021 1:34:44 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server built:          Feb 5 2019 11:42:42 UTC
3月 30, 2021 1:34:44 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server number:         8.5.38.0
3月 30, 2021 1:34:44 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Name:               Windows 10
3月 30, 2021 1:34:44 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Version:            10.0
3月 30, 2021 1:34:44 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Architecture:          amd64
3月 30, 2021 1:34:44 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Java Home:             C:\Program Files\Java\jdk-15.0.2
3月 30, 2021 1:34:44 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Version:           15.0.2+7-27
3月 30, 2021 1:34:44 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Vendor:            Oracle Corporation
3月 30, 2021 1:34:44 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_BASE:         D:\apache-tomcat-8.5.38
3月 30, 2021 1:34:44 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_HOME:         D:\apache-tomcat-8.5.38
3月 30, 2021 1:34:44 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.base=D:\apache-tomcat-8.5.38
3月 30, 2021 1:34:44 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.home=D:\apache-tomcat-8.5.38
3月 30, 2021 1:34:44 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dwtp.deploy=D:\apache-tomcat-8.5.38\webapps
3月 30, 2021 1:34:44 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dfile.encoding=GBK
3月 30, 2021 1:34:44 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -XX:+ShowCodeDetailsInExceptionMessages
3月 30, 2021 1:34:44 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: Loaded APR based Apache Tomcat Native library [1.2.26] using APR version [1.7.0].
3月 30, 2021 1:34:44 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
3月 30, 2021 1:34:44 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
3月 30, 2021 1:34:44 下午 org.apache.catalina.core.AprLifecycleListener initializeSSL
信息: OpenSSL successfully initialized [OpenSSL 1.1.1i  8 Dec 2020]
3月 30, 2021 1:34:44 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-nio-8080"]
3月 30, 2021 1:34:44 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
信息: Using a shared selector for servlet write/read
3月 30, 2021 1:34:44 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-nio-8009"]
3月 30, 2021 1:34:44 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
信息: Using a shared selector for servlet write/read
3月 30, 2021 1:34:44 下午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 1799 ms
3月 30, 2021 1:34:44 下午 org.apache.catalina.core.StandardService startInternal
信息: Starting service [Catalina]
3月 30, 2021 1:34:44 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/8.5.38
3月 30, 2021 1:34:44 下午 org.apache.catalina.startup.HostConfig deployDescriptor
信息: Deploying configuration descriptor [D:\apache-tomcat-8.5.38\conf\Catalina\localhost\SpringMVC.xml]
3月 30, 2021 1:34:44 下午 org.apache.catalina.startup.HostConfig deployDescriptor
警告: A docBase [D:\apache-tomcat-8.5.38\webapps\SpringMVC] inside the host appBase has been specified, and will be ignored
3月 30, 2021 1:34:47 下午 org.apache.catalina.core.ApplicationContext log
信息: No Spring WebApplicationInitializer types detected on classpath
3月 30, 2021 1:34:47 下午 org.apache.jasper.servlet.TldScanner scanJars
信息: 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.
3月 30, 2021 1:34:48 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'mvc'
3月 30, 2021 1:34:48 下午 org.springframework.web.servlet.FrameworkServlet initServletBean
信息: FrameworkServlet 'mvc': initialization started
3月 30, 2021 1:34:48 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing WebApplicationContext for namespace 'mvc-servlet': startup date [Tue Mar 30 13:34:48 CST 2021]; root of context hierarchy
3月 30, 2021 1:34:48 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [mvc.xml]
3月 30, 2021 1:34:49 下午 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
信息: Looking for @ControllerAdvice: WebApplicationContext for namespace 'mvc-servlet': startup date [Tue Mar 30 13:34:48 CST 2021]; root of context hierarchy
3月 30, 2021 1:34:49 下午 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
信息: Looking for @ControllerAdvice: WebApplicationContext for namespace 'mvc-servlet': startup date [Tue Mar 30 13:34:48 CST 2021]; root of context hierarchy
3月 30, 2021 1:34:49 下午 org.springframework.web.servlet.FrameworkServlet initServletBean
信息: FrameworkServlet 'mvc': initialization completed in 1527 ms
3月 30, 2021 1:34:49 下午 org.apache.catalina.startup.HostConfig deployDescriptor
信息: Deployment of configuration descriptor [D:\apache-tomcat-8.5.38\conf\Catalina\localhost\SpringMVC.xml] has finished in [5,135] ms
3月 30, 2021 1:34:49 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory [D:\apache-tomcat-8.5.38\webapps\ROOT]
3月 30, 2021 1:34:49 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory [D:\apache-tomcat-8.5.38\webapps\ROOT] has finished in [65] ms
3月 30, 2021 1:34:49 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-8080"]
3月 30, 2021 1:34:50 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-nio-8009"]
3月 30, 2021 1:34:50 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 5378 ms
3月 30, 2021 1:34:59 下午 org.springframework.web.servlet.DispatcherServlet noHandlerFound
警告: No mapping found for HTTP request with URI [/SpringMVC/hello.do] in DispatcherServlet with name 'mvc'

 

您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632