我試圖從頭構建一個Spring應用程序,並且使用以下目錄結構和文件製作了一個簡單的應用程序。Spring - 在DispatcherServlet中找不到映射,請求URI包含應用程序名稱
的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>LearningSpring</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
調度-servlet.xml中
<mvc:annotation-driven/>
<context:component-scan base-package="com.learn.spring.app.web" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
CentralController.java
@Controller
public class CentralController {
@RequestMapping("/profile.html")
public ModelAndView showProfile(HttpServletRequest request, HttpServletResponse response){
return new ModelAndView("profile");
}
}
我不明白髮生了什麼事錯在上面的代碼,但我總是得到一個404錯誤。將tomcat日誌顯示,我注意到以下
15-Mar-2017 16:16:33.127 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /Users/SomeUser/Documents/apache-tomcat-9.0.0.M17/webapps/LearningSpring.war
15-Mar-2017 16:16:34.303 INFO [localhost-startStop-1] 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.
15-Mar-2017 16:16:34.501 INFO [localhost-startStop-1] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet 'dispatcher': initialization started
15-Mar-2017 16:16:34.533 INFO [localhost-startStop-1] org.springframework.web.context.support.XmlWebApplicationContext.prepareRefresh Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Wed Mar 15 16:16:34 EDT 2017]; root of context hierarchy
15-Mar-2017 16:16:34.594 INFO [localhost-startStop-1] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
15-Mar-2017 16:16:35.238 INFO [localhost-startStop-1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.registerHandlerMethod Mapped "{[/profile.html],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.learn.spring.app.web.CentralController.showProfile(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
15-Mar-2017 16:16:35.341 INFO [localhost-startStop-1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Wed Mar 15 16:16:34 EDT 2017]; root of context hierarchy
15-Mar-2017 16:16:35.413 INFO [localhost-startStop-1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Wed Mar 15 16:16:34 EDT 2017]; root of context hierarchy
15-Mar-2017 16:16:35.517 INFO [localhost-startStop-1] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet 'dispatcher': initialization completed in 1016 ms
15-Mar-2017 16:16:35.527 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /Users/SomeUser/Documents/apache-tomcat-9.0.0.M17/webapps/LearningSpring.war has finished in 2,399 ms
15-Mar-2017 16:16:35.530 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [http-nio-8080]
15-Mar-2017 16:16:35.536 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [ajp-nio-8009]
15-Mar-2017 16:16:35.536 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 2492 ms
^[15-Mar-2017 16:19:22.994 WARNING [http-nio-8080-exec-5] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/LearningSpring] in DispatcherServlet with name 'dispatcher'
15-Mar-2017 16:19:27.755 WARNING [http-nio-8080-exec-6] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/LearningSpring/] in DispatcherServlet with name 'dispatcher'
15-Mar-2017 16:19:40.881 WARNING [http-nio-8080-exec-7] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/LearningSpring/WEB-INF/jsp/profile.jsp] in DispatcherServlet with name 'dispatcher'
一件事是,所有的請求的URI有/LearningSpring/
(應用程序名稱)作爲前綴(我想應該不會有)。
我無法通過網址localhost:8080/LearningSpring/profile.html
訪問profile.jsp
頁面,也沒有顯示index.html
作爲歡迎頁面。它只是拋出404當我訪問localhost:8080/LearningSpring/
我已經通讀了很多答案和谷歌搜索幾個小時,但沒有運氣。請幫助指出什麼是錯的。
謝謝
解決方法1:添加到您的web.xml *。html的 url-pattern的>,並在您的本地主機yourl:8080/APPNAME/profile文件.html解決方案2:爲您的方法@RequestMapping(「/ profile」)更新requestMapping並在yourl localhost:8080/appName/profile中最好地reagrards –
@hichamabdedaime試過了。沒有幫助 –
刪除工作區中的文件夾metada並重新啓動eclise並再試一次 –