Spring MVC没有初始请求映射

时间:2015-07-30 03:30:09

标签: spring spring-mvc

Web部署机制是否会影响Spring MVC的工作?

我创建了一个包含web.xml和pjsp页面的Web项目(SpringMVCTest)然后我创建了一个Java项目(SpringMVCTestSrc)并使用链接源函数链接test_src和SpringMVCTestSrc。

我的Eclipse工作区

enter image description here

我的问题是,如果我在war文件中部署项目,那么它工作正常 ,EX:如果我打开

  

本地主机:8080 / SpringMVCTest /消息/ showMessage.pages

它会转发到

  

主/ showMessage.jsp

如果我使用单独的web和jar文件部署项目,则不会发现请求映射,如下所示。

[DEBUG]-[2015/07/30 15:03:39,o.s.w.s.DispatcherServlet(init):139]: Servlet 'dispatcher' configured successfully
[DEBUG]-[2015/07/30 15:03:40,o.s.w.s.DispatcherServlet(doService):861]: DispatcherServlet with name 'dispatcher' processing GET request for [/SpringMVCTest/message/showMessage.pages]
[DEBUG]-[2015/07/30 15:03:40,o.s.w.s.m.m.a.RequestMappingHandlerMapping(getHandlerInternal):294]: Looking up handler method for path /message/showMessage.pages
[DEBUG]-[2015/07/30 15:03:40,o.s.w.s.m.m.a.RequestMappingHandlerMapping(getHandlerInternal):302]: Did not find handler method for [/message/showMessage.pages]
[WARN]-[2015/07/30 15:03:40,o.s.w.s.PageNotFound(noHandlerFound):1136]: No mapping found for HTTP request with URI [/SpringMVCTest/message/showMessage.pages] in DispatcherServlet with name 'dispatcher'

日志显示不同。

如果我部署在 war 文件中。日志显示发现 18 bean定义和请求映射工作正常。

  
      
  1. 从Eclipse导出war文件并将其放到apache-tomcat-8.0.21 \ webapps
  2.   
  3. 启动Tomcat并打开localhost:8080 / SpringMVCTest / message / showMessage.pages
  4.   
  5. 该页面将显示消息:Hello world
  6.   
o.s.c.a.ClassPathBeanDefinitionScanner(registerDefaultFilters):244]: JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
o.s.c.i.s.PathMatchingResourcePatternResolver(doFindMatchingFileSystemResources):631]: Looking for matching resources in directory tree [D:\Temp\apache-tomcat-8.0.21\webapps\SpringMVCTest\WEB-INF\classes\in\hotkey\mvc]
o.s.c.i.s.PathMatchingResourcePatternResolver(doRetrieveMatchingFiles):693]: Searching directory [D:\Temp\apache-tomcat-8.0.21\webapps\SpringMVCTest\WEB-INF\classes\in\hotkey\mvc] for files matching pattern [D:/Temp/apache-tomcat-8.0.21/webapps/SpringMVCTest/WEB-INF/classes/in/hotkey/mvc/**/*.class]
o.s.c.i.s.PathMatchingResourcePatternResolver(findPathMatchingResources):424]: Resolved location pattern [classpath*:in/hotkey/mvc/**/*.class] to resources [file [D:\Temp\apache-tomcat-8.0.21\webapps\SpringMVCTest\WEB-INF\classes\in\hotkey\mvc\MainForm.class]]
o.s.c.a.ClassPathBeanDefinitionScanner(findCandidateComponents):286]: Identified candidate component class: file [D:\Temp\apache-tomcat-8.0.21\webapps\SpringMVCTest\WEB-INF\classes\in\hotkey\mvc\MainForm.class]
o.s.b.f.x.XmlBeanDefinitionReader(loadBeanDefinitions):224]: Loaded 18 bean definitions from location pattern [classpath:test-dispatcher-context.xml]

如果我使用单独的web和jar 文件进行部署。日志显示发现 17 bean定义,请求映射不起作用。

  
      
  1. 在apache-tomcat-8.0.21 \ webapps中构建目录,EX:apache-tomcat-8.0.21 \ webapps \ SpringMVCTest
  2.   
  3. 将WEB-INF,META-INF从Eclipse工作区复制到apache-tomcat-8.0.21 \ webapps \ SpringMVCTest
  4.   
  5. 从SpringMVCTestSrc导出jar文件并将其放到apache-tomcat-8.0.21 \ webapps \ SpringMVCTest \ WEB-INF \ lib
  6.   
  7. 启动Tomcat并打开localhost:8080 / SpringMVCTest / message / showMessage.pages
  8.   
  9. 该页面显示HTTP 404
  10.   
o.s.c.a.ClassPathBeanDefinitionScanner(registerDefaultFilters):244]: JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
o.s.c.i.s.PathMatchingResourcePatternResolver(findPathMatchingResources):424]: Resolved location pattern [classpath*:in/hotkey/mvc/**/*.class] to resources []
o.s.b.f.x.XmlBeanDefinitionReader(loadBeanDefinitions):224]: Loaded 17 bean definitions from location pattern [classpath:test-dispatcher-context.xml]

有人知道为什么相同的代码但不同的部署机制有不同的结果吗?

我的代码是打击

的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>SpringMVCTest</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:test-service-context.xml</param-value>
    </context-param>
    <filter>
        <filter-name>SetCharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>SetCharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:test-dispatcher-context.xml</param-value> 
        </init-param>
        <load-on-startup>3</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.pages</url-pattern>
    </servlet-mapping>  
</web-app>

Spring配置(test-service-context.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"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

    <bean id="testMessageService" class="in.hotkey.service.TestMessageService" >
    </bean>

</beans>

Spring MVC Config(test-dispatcher-context.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/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">    


    <mvc:annotation-driven />   
    <context:component-scan base-package="in.hotkey.mvc"/>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>        

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />     
</beans>

主控制器

package in.hotkey.mvc;

import in.hotkey.service.TestMessageService;

import javax.annotation.Resource;

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

@Controller
public class MainForm {

    @RequestMapping(value="/message/showMessage.pages")
    public ModelAndView handleShowMessage() throws Exception {
        ModelAndView mv = new ModelAndView("main/showMessage");
        mv.addObject("message", "Hello world!");

        return mv;
    }   

}

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>Test</title>
</head>
<body>

    <h4>Message : ${message}</h4>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

我发现问题是什么......

根本原因来自Spring MVC documententer image description here

这意味着注释扫描需要目录条目!! 问题是我将整个项目导出到war文件中。它包含WEB-INF / classes目录中的完整目录条目,但jar文件没有目录条目信息。

解决此问题只需检查&#34;添加目录条目&#34; Eclipse导出函数中的选项。

enter image description here