Spring - 在DispatcherServlet中找不到映射,请求URI包括应用程序名称

时间:2017-03-15 20:47:00

标签: spring spring-mvc tomcat uri

我正在尝试从头开始构建Spring应用程序,并使用以下目录结构和文件创建了一个简单的应用程序。

Eclipse folder structure


的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/(应用名称)作为前缀(我猜不应该在那里)。

我无法通过网址profile.jsp访问localhost:8080/LearningSpring/profile.html页面,index.html也未显示为欢迎页面。当我访问localhost:8080/LearningSpring/

时,它只会抛出404

我已经阅读了很多答案并用谷歌搜索了几个小时但没有运气。请指出什么是错误的帮助。

谢谢

1 个答案:

答案 0 :(得分:1)

一些事情......

  1. 将调度程序servlet的url-pattern更改为&#34; /&#34 ;;
  2. <url-pattern>/</url-pattern>

    1. 提供所有静态资源通过调度程序servlet;
    2. <mvc:resources location="/**" mapping="/"></mvc:resources>

      查看此示例示例 - https://github.com/code-4-fun/spring-mvc-demo/blob/master/src/main/webapp/WEB-INF/dispatcher-servlet.xml