2015-07-21 111 views
1

我無法獲得一個簡單的Spring例子,Spring版本4.1.7.RELEASE。Tomcat中的Spring處理函數映射

web.xml中:

<web-app id="epw" version="2.5" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"> 

    <display-name>EPW</display-name> 

    <servlet> 
     <servlet-name>SpringDispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <!-- Doesn't seem to help 
     <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>WEB-INF/epw-config.xml WEB-INF/SpringDispatcher-servlet.xml</param-value> 
     </init-param> 
     -->  
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>SpringDispatcher</servlet-name> 
    <url-pattern>/epw/*</url-pattern> 
    </servlet-mapping> 

    <!-- Doesn't seem to help 
    <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/epw-config.xml</param-value> 
    </context-param> 

    <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    --> 

</web-app> 

SpringDispatcher-servlet.xml中:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    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.1.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.1.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">  

    <mvc:annotation-driven/> 
    <context:component-scan base-package="g.n.h.a.b.e"/> 
    <context:annotation-config/> 

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" > 
     <property name="prefix" value="/templates/"/> 
     <property name="suffix" value=".html"/> 
    </bean>   

</beans> 

DemoController.java:

package g.n.h.a.b.e; 

import javax.servlet.http.HttpServletRequest; 

import org.apache.logging.log4j.LogManager; 
import org.apache.logging.log4j.Logger; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.servlet.ModelAndView; 

@Controller 
@RequestMapping(value="/epw") 
public class DemoController { 

    public DemoController() { 
     log.info("created"); 
    } 

    @RequestMapping(value="/foo", method=RequestMethod.GET, produces="text/plain") 
    public String foo(@RequestParam(value="name", required=false, defaultValue="bar") final String name, Model model, final HttpServletRequest request) { 
     log.entry(); 
     String view = "foo" ; 
     try { 
      model.addAttribute("name", name); 
      return view ; 
     } finally { 
      log.exit(view); 
     } 
    } 

    @RequestMapping(value="/riddle", method=RequestMethod.GET) 
    public ModelAndView doRiddle(Model model) { 
     log.entry(); 
     String view = "foo"; 
     ModelAndView mav = null ; 
     try { 
      model.addAttribute("name", "riddle"); 
      mav = new ModelAndView(view, null, model) ; 
      return mav ; 
     } finally { 
      log.exit(mav) ; 
     } 
    } 

    private static final Logger log = LogManager.getLogger() ; 

} 

WAR內容:

C:\項目\ bne_main \ JavaProjects \ WEBAPPS \ EPW>罐子TF目標\ epw.war

META-INF/ 
META-INF/MANIFEST.MF 
WEB-INF/ 
WEB-INF/classes/ 
WEB-INF/classes/g/ 
WEB-INF/classes/g/n/ 
WEB-INF/classes/g/n/h/ 
WEB-INF/classes/g/n/h/a/ 
WEB-INF/classes/g/n/h/a/b/ 
WEB-INF/classes/g/n/h/a/b/e/ 
WEB-INF/classes/templates/ 
WEB-INF/lib/ 
index.html 
WEB-INF/classes/g/n/h/a/b/e/DemoController.class 
WEB-INF/classes/log4j.xml 
WEB-INF/classes/log4j2.xml 
WEB-INF/classes/revision.class 
WEB-INF/classes/revision.properties 
WEB-INF/classes/templates/foo.html 
WEB-INF/epw-config.xml 
WEB-INF/lib/aopalliance-1.0.jar 
WEB-INF/lib/commons-logging-1.2.jar 
WEB-INF/lib/groovy-all-2.4.3.jar 
WEB-INF/lib/javassist-3.16.1-GA.jar 
WEB-INF/lib/log4j-api-2.3.jar 
WEB-INF/lib/log4j-core-2.3.jar 
WEB-INF/lib/ognl-3.0.8.jar 
WEB-INF/lib/slf4j-api-1.6.6.jar 
WEB-INF/lib/spring-aop-4.1.7.RELEASE.jar 
WEB-INF/lib/spring-beans-4.1.7.RELEASE.jar 
WEB-INF/lib/spring-context-4.1.7.RELEASE.jar 
WEB-INF/lib/spring-core-4.1.7.RELEASE.jar 
WEB-INF/lib/spring-expression-4.1.7.RELEASE.jar 
WEB-INF/lib/spring-jdbc-4.1.7.RELEASE.jar 
WEB-INF/lib/spring-tx-4.1.7.RELEASE.jar 
WEB-INF/lib/spring-web-4.1.7.RELEASE.jar 
WEB-INF/lib/spring-webmvc-4.1.7.RELEASE.jar 
WEB-INF/lib/thymeleaf-2.1.4.RELEASE.jar 
WEB-INF/lib/unbescape-1.1.0.RELEASE.jar 
WEB-INF/SpringDispatcher-servlet.xml 
WEB-INF/web.xml 
META-INF/maven/g.n.h.a.b/e/pom.xml 
META-INF/maven/g.n.h.a.b/e/pom.properties 
META-INF/INDEX.LIST 

根據部署日誌,/epw/riddle/epw/foo都映射:

21-Jul-2015 11:59:00.054 INFO [http-nio-8080-exec-94] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive C:\tomcat-8.0.24\webapps\epw.war 
21-Jul-2015 11:59:01.568 INFO [http-nio-8080-exec-94] 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. 
21-Jul-2015 11:59:01.677 INFO [http-nio-8080-exec-94] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet 'SpringDispatcher': initialization started 
21-Jul-2015 11:59:01.708 INFO [http-nio-8080-exec-94] org.springframework.web.context.support.XmlWebApplicationContext.prepareRefresh Refreshing WebApplicationContext for namespace 'SpringDispatcher-servlet': startup date [Tue Jul 21 11:59:01 EDT 2015]; root of context hierarchy 
21-Jul-2015 11:59:01.755 INFO [http-nio-8080-exec-94] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from ServletContext resource [/WEB-INF/SpringDispatcher-servlet.xml] 
21-Jul-2015 11:59:02.176 INFO [http-nio-8080-exec-94] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.registerHandlerMethod Mapped "{[/epw/riddle],methods=[GET]}" onto public org.springframework.web.servlet.ModelAndView gov.ny.health.adsg.bne.epw.DemoController.doRiddle(org.springframework.ui.Model) 
21-Jul-2015 11:59:02.176 INFO [http-nio-8080-exec-94] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.registerHandlerMethod Mapped "{[/epw/foo],methods=[GET],produces=[text/plain]}" onto public java.lang.String gov.ny.health.adsg.bne.epw.DemoController.foo(java.lang.String,org.springframework.ui.Model,javax.servlet.http.HttpServletRequest) 
21-Jul-2015 11:59:02.254 INFO [http-nio-8080-exec-94] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache Looking for @ControllerAdvice: WebApplicationContext for namespace 'SpringDispatcher-servlet': startup date [Tue Jul 21 11:59:01 EDT 2015]; root of context hierarchy 
21-Jul-2015 11:59:02.301 INFO [http-nio-8080-exec-94] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache Looking for @ControllerAdvice: WebApplicationContext for namespace 'SpringDispatcher-servlet': startup date [Tue Jul 21 11:59:01 EDT 2015]; root of context hierarchy 
21-Jul-2015 11:59:02.691 INFO [http-nio-8080-exec-94] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet 'SpringDispatcher': initialization completed in 1014 ms 
21-Jul-2015 11:59:02.691 INFO [http-nio-8080-exec-94] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive C:\tomcat-8.0.24\webapps\epw.war has finished in 2,637 ms 

在運行時,localhost:8080/epw/顯示index.html,符合市場預期。但是,localhost:8080/epw/foolocalhost:8080/epw/riddle都會返回404,而且日誌中沒有任何更多內容。

我錯過了什麼?

+0

您是否嘗試過'localhost:8080/epw/epw/epw/foo'? (不是開玩笑) – Roman

回答

0

看來你的分發程序Servlet被映射到/ EPW這裏(web.xml)中的上下文路徑:

<servlet-mapping> 
    <servlet-name>SpringDispatcher</servlet-name> 
    <url-pattern>/epw/*</url-pattern> 
</servlet-mapping> 

這意味着,您的應用程序應該localhost:8080/epw/epw/foo而不是localhost:8080/epw/foo下作出迴應。您可以嘗試更改servlet映射,或從控制器中刪除@RequestMapping註釋。

+0

感謝您的評論。我嘗試過許多組合,例如'localhost:8080/epw/epw/foo','localhost:8080/foo'等,但沒有成功。這是我這次得到的:'21-Jul-2015 13:28:49.948警告[http-nio-8080-exec-104] org.springframework.web.servlet.PageNotFound.noHandlerFound沒有找到具有URI的HTTP請求的映射[在DispatcherServlet中名爲'SpringDispatcher''的012/014/epw/epw/foo] – devnull5475

+0

另一方面,'localhost:8080/epw/x/foo'只是獲取404而沒有任何日誌條目。 – devnull5475

0

請確保您通過任何鏈接點擊的網址,或通過jquery進行的任何點擊操作,它會在控制器中追加上下文url以及您的url映射。就像我的情況一樣,我通過jquery填充數據表,並在表的每一行上綁定了一個點擊動作,該動作正在重新渲染以讓URL「/ projects」; 然後我重新認識到我錯過了我的上下文url,然後用「/ context_url/projects」改變了映射。

0

嘗試這種情況:

模型值可以在視圖可以直接使用,將其通過ModelAndView的構造不是必需的。

@RequestMapping(value="/riddle", method=RequestMethod.GET) 
    public ModelAndView doRiddle(Model model) { 
     log.entry(); 
     String view = "foo"; 
     ModelAndView mav = new ModelAndView(view); 
     try { 
     model.addAttribute("name", "riddle"); 
     return mav ; 
     } finally { 
     log.exit(mav) ; 
    } 
}