我无法让一个简单的Spring示例(Spring 4.1.7.RELEASE)正常工作。

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() ;

}


战争内容:

C:\ projects \ bne_main \ JavaProjects \ webApps \ epw> jar tf target \ 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,日志中没有其他内容。

我想念什么?

最佳答案

看来您的调度程序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批注。

10-08 02:34