我是Spring MVC的初学者,并尝试执行YouTube教程中给出的演示程序。
https://www.youtube.com/watch?v=qXIESogSi_Q&index=6&list=PLBgMUB7xGcO31B2gBmy1igpZn6LK78-CJ
演示代码不使用注释,并且在运行程序时出现HTTP 404错误。
网络.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>FirstSpringMvcProj</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id = "HandlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name = "/welcome.html" class="com.demo.controller.HelloController"/>
<bean id = "viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
您好控制器.java
package com.demo.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class HelloController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,HttpServletResponse res) throws Exception {
ModelAndView modelAndView = new ModelAndView("HelloPage");
modelAndView.addObject("welcomeMessage","Hi, Welcome to my Page");
return modelAndView;
}
}
您好页面.jsp
<html>
<body>
<h1>Message is: ${welcomeMessage}</h1>>
</body>
</html>
在运行上面的代码时,我在控制台中得到以下错误
Aug 25, 2018 10:31:43 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/FirstSpringMvcProj/] in DispatcherServlet with name 'spring-dispatcher'
Aug 25, 2018 10:43:03 PM org.apache.catalina.core.StandardContext reload
完整的服务器日志:
Aug 26, 2018 5:50:28 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:FirstSpringMvcProj' did not find a matching property.
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/7.0.90
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Jul 2 2018 17:05:37 UTC
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 7.0.90.0
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Windows 10
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.0
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: C:Program FilesJavajre1.8.0_131
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_131-b11
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: F:StudySpring_Workspace.metadata.pluginsorg.eclipse.wst.server.coretmp0
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: F:Studyapache-tomcat-7.0.90apache-tomcat-7.0.90
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=F:StudySpring_Workspace.metadata.pluginsorg.eclipse.wst.server.coretmp0
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=F:Studyapache-tomcat-7.0.90apache-tomcat-7.0.90
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=F:StudySpring_Workspace.metadata.pluginsorg.eclipse.wst.server.coretmp0wtpwebapps
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=F:Studyapache-tomcat-7.0.90apache-tomcat-7.0.90endorsed
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Aug 26, 2018 5:50:28 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: C:Program FilesJavajre1.8.0_131bin;C:WINDOWSSunJavabin;C:WINDOWSsystem32;C:WINDOWS;C:/Program Files/Java/jre1.8.0_131/bin/server;C:/Program Files/Java/jre1.8.0_131/bin;C:/Program Files/Java/jre1.8.0_131/lib/amd64;C:ProgramDataOracleJavajavapath;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;%SystemRoot%system32;%SystemRoot%;%SystemRoot%System32Wbem;%SYSTEMROOT%System32WindowsPowerShellv1.0;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsDAL;C:Program FilesIntelIntel(R) Management Engine ComponentsDAL;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsIPT;C:Program FilesIntelIntel(R) Management Engine ComponentsIPT;C:Android;C:WindowsSystem32;%SYSTEMROOT%System32OpenSSH;C:UsersDellAppDataLocalMicrosoftWindowsApps;;F:StudyEclipse_Mars;;.
Aug 26, 2018 5:50:28 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Aug 26, 2018 5:50:28 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1136 ms
Aug 26, 2018 5:50:28 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 26, 2018 5:50:28 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.90
Aug 26, 2018 5:50:28 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
WARNING: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [132] milliseconds.
Aug 26, 2018 5:50:30 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://www.springframework.org/tags is already defined
Aug 26, 2018 5:50:30 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://www.springframework.org/tags/form is already defined
Aug 26, 2018 5:50:30 PM org.apache.catalina.startup.TldConfig execute
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 26, 2018 5:50:30 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Aug 26, 2018 5:50:30 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Aug 26, 2018 5:50:30 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Aug 26, 2018 5:50:30 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2178 ms
Aug 26, 2018 5:50:33 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'spring-dispatcher'
Aug 26, 2018 5:50:33 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'spring-dispatcher': initialization started
Aug 26, 2018 5:50:33 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'spring-dispatcher-servlet': startup date [Sun Aug 26 17:50:33 IST 2018]; root of context hierarchy
Aug 26, 2018 5:50:33 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-dispatcher-servlet.xml]
Aug 26, 2018 5:50:34 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/welcome] onto handler '/welcome'
Aug 26, 2018 5:50:34 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'spring-dispatcher': initialization completed in 607 ms
Aug 26, 2018 5:50:34 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/FirstSpringMvcProj/] in DispatcherServlet with name 'spring-dispatcher'
Aug 26, 2018 5:51:17 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/FirstSpringMvcProj/] in DispatcherServlet with name 'spring-dispatcher'
Aug 26, 2018 5:51:31 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/FirstSpringMvcProj/welcome.html] in DispatcherServlet with name 'spring-dispatcher'
在浏览器中打开网址http://localhost:8080/FirstSpringMvcProj/welcome.html时,我收到HTTP 404错误。有人可以帮助我了解导致此错误的原因吗?
我还尝试使用Anootation并修改我的HelloController和spring-dispatcher-servlet.xml文件,如下所示。但是,我仍然遇到同样的错误。
您好控制器.java
package com.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping ("/welcome")
protected ModelAndView helloWorld(){
ModelAndView modelAndView = new ModelAndView("HelloPage");
modelAndView.addObject("welcomeMessage","Hello World !!");
return modelAndView;
}
}
spring-dispatcher-servlet.xml
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:context = "http://www.springframework.org/schema/context"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
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 = "com.demo.controller"/>
<bean id = "viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
这里的问题可能与模型和视图导入有关
取代import org.springframework.web.servlet.ModelAndView;
跟import org.springframework.web.portlet.ModelAndView;
https://stackoverflow.com/a/29096411/1664430
1. Change your servlet mapping to *.html.
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
2. Now how would dispatcher servlet map this reuqest to the handler method?
you need to tell dispacther servlet by adding this property into spring-dispatcher-servlet.xml file.
<property name="urlMap">
<map>
<entry key="/welcome.html">
<ref bean="abstractController"/>
</entry>
</map>
</property>
去点击这个网址-: http://localhost:8080/FirstSpringMvcProj/welcome.html
---------------祝你好运
-------------------/welcome而不是/welcome.html
<bean name = "/welcome" class="com.demo.controller.HelloController"/>
首先在spring-dispatcher-servlet 中.xml删除 WEB-INF/" " "好的,这应该可以工作。没有任何编码错误。但是您需要在 Web 应用程序文件夹中创建索引.jsp或索引.html。为此,右键单击webapp->创建新的html或jsp文件名为index.jsp/html。在索引中.html输入"欢迎"。