这个问题已被问过几次,我看了很多。我尝试了所有的建议,加上一些。首先,我将显示我的代码并告诉您我是如何尝试调试它的。
的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"
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>SpringMVCpractice</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>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/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>
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
调度-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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.practice.controller"></context:component-scan>
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
IndexController.java
package com.practice.comtroller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping({"/","/SpringMVCpractice/","index"})
public String index(Model model){
return "index";
}
}
的index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello World..
</body>
</html>
控制台
Aug 25, 2015 4:10:22 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SpringMVCpractice' did not find a matching property.
Aug 25, 2015 4:10:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/8.0.24
Aug 25, 2015 4:10:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Jul 1 2015 20:19:55 UTC
Aug 25, 2015 4:10:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 8.0.24.0
Aug 25, 2015 4:10:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Mac OS X
Aug 25, 2015 4:10:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.10.5
Aug 25, 2015 4:10:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: x86_64
Aug 25, 2015 4:10:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: /Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home/jre
Aug 25, 2015 4:10:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_31-b13
Aug 25, 2015 4:10:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Aug 25, 2015 4:10:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: /Users/DrewJocham/Documents/springMVC/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Aug 25, 2015 4:10:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: /Library/eclipse-tomcat-servers/apache-tomcat-8.0.24
Aug 25, 2015 4:10:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/Users/DrewJocham/Documents/springMVC/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Aug 25, 2015 4:10:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/Library/eclipse-tomcat-servers/apache-tomcat-8.0.24
Aug 25, 2015 4:10:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/Users/DrewJocham/Documents/springMVC/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
Aug 25, 2015 4:10:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/Library/eclipse-tomcat-servers/apache-tomcat-8.0.24/endorsed
Aug 25, 2015 4:10:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Aug 25, 2015 4:10:22 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/DrewJocham/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
Aug 25, 2015 4:10:22 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Aug 25, 2015 4:10:22 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Aug 25, 2015 4:10:22 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Aug 25, 2015 4:10:22 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Aug 25, 2015 4:10:22 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1175 ms
Aug 25, 2015 4:10:23 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 25, 2015 4:10:23 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.24
Aug 25, 2015 4:10:24 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: 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.
Aug 25, 2015 4:10:24 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Aug 25, 2015 4:10:24 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
Aug 25, 2015 4:10:24 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization started
Aug 25, 2015 4:10:24 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Tue Aug 25 16:10:24 CEST 2015]; root of context hierarchy
Aug 25, 2015 4:10:24 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
Aug 25, 2015 4:10:25 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
INFO: Mapped "{[/ || /SpringMVCpractice/ || /index]}" onto public java.lang.String com.practice.comtroller.IndexController.index(org.springframework.ui.Model)
Aug 25, 2015 4:10:26 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Tue Aug 25 16:10:24 CEST 2015]; root of context hierarchy
Aug 25, 2015 4:10:26 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Tue Aug 25 16:10:24 CEST 2015]; root of context hierarchy
Aug 25, 2015 4:10:26 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 1682 ms
Aug 25, 2015 4:10:26 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Aug 25, 2015 4:10:26 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Aug 25, 2015 4:10:26 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 3509 ms
Aug 25, 2015 4:10:27 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringMVCpractice/WEB-INF/jsps/index.jsp] in DispatcherServlet with name 'dispatcher'
现在我试图像这样改变servlet-mapping,因为上面的警告说没有找到带有URI [/ SpringMVCpractice /]的HTTP请求的映射。
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/SpringMVCpractice/*</url-pattern>
</servlet-mapping>
然后警告消失但我仍然收到404错误。
-------更新一个--------
是的,这确实有效,但为什么呢?这不仅仅是我的页面的样子:
答案 0 :(得分:0)
您应该在<mvc:default-servlet-handler />
中添加dispatcher-servlet.xml
。添加此内容后应该有效。