Spring mvc 404在JBoss上使用Spring MVC Java配置时出错

Spring mvc 404在JBoss上使用Spring MVC Java配置时出错,spring-mvc,jboss,spring-java-config,Spring Mvc,Jboss,Spring Java Config,我用Java配置编写了一个小型Spring MVC应用程序。它在Tomcat上工作得非常好,但在JBoss EAP 6.2上却不行。它成功地部署在JBoss上,但当我请求SpringMVC定义的任何页面和浏览器中的404错误时,我收到了这个警告 WARN[org.springframework.web.servlet.PageNotFound](http-/127.0.0.1:8080-1)在名为“dispatcher”的DispatcherServlet中找不到URI为[/example we

我用Java配置编写了一个小型Spring MVC应用程序。它在Tomcat上工作得非常好,但在JBoss EAP 6.2上却不行。它成功地部署在JBoss上,但当我请求SpringMVC定义的任何页面和浏览器中的404错误时,我收到了这个警告

WARN[org.springframework.web.servlet.PageNotFound](http-/127.0.0.1:8080-1)在名为“dispatcher”的DispatcherServlet中找不到URI为[/example web/pages/login.jsp]的http请求的映射。

在这里您可以看到我的代码:

public class WebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class[] { RootConfiguration.class};
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class[] { WebMvcConfig.class };
}

@Override
protected String[] getServletMappings() {
    return new String[] { "/*" };
}

@Override
protected Filter[] getServletFilters() {
    return new Filter[] { new HiddenHttpMethodFilter() };
}
}
和RootConfig:

@Configuration
@ComponentScan
public class RootConfiguration {
}
在部署期间,我可以在日志中看到请求确实被映射:

INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 71) Mapped "{[/start],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.spring.example.w.controller.StartController.handleStart() throws javax.servlet.ServletException,java.io.IOException
INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (ServerService Thread Pool -- 71) Mapped URL path [/login] onto handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
INFO  [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 71) Root WebApplicationContext: initialization completed in 2530 ms
INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/example-web]] (ServerService Thread Pool -- 71) Initializing Spring FrameworkServlet 'dispatcher'
INFO  [org.springframework.web.servlet.DispatcherServlet] (ServerService Thread Pool -- 71) FrameworkServlet 'dispatcher': initialization started

任何关于我为什么会出现404错误和此警告的帮助,我们都将不胜感激。我应该再次强调,它正在Tomcat上工作。提前感谢。

在阅读Sivalbs的教程时,在JBoss上运行应用程序时遇到了这样一个问题,该应用程序在tomcat上运行良好。通过将DispatcherServlet映射更改为“/app/*”,问题得以解决。您还可以尝试实现WebApplicationInitializer,而不是使用抽象类。下面是一个例子:

 @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        WebApplicationContext context = getContext();
        servletContext.addListener(new ContextLoaderListener(context));
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/"); // i have a more or less big website, and can't see advantages by using "/*" mapping, this can be also a problem. 
    }

    private AnnotationConfigWebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocations("ua.company.config.WebConfig", "ua.company.config.PersistenceConfig", "ua.company.config.SecurityConfig");
        return context;
    }

可能您遇到了“没有web.xml,无法覆盖默认servlet”。此问题显然会影响EAP 6.2、6.3和6.4.0。从错误报告中:

将Spring dispatcher servlet映射到url模式“/”无效 以编程方式。换句话说,覆盖默认servlet不是必需的 可以使用Java代码

结果:许多用户将Spring的DispatcherServlet映射到“/”,并且 在web.xml中执行此操作时效果良好。但是,当此配置 以编程方式完成,默认servlet首先绑定到“/”。这 防止以后绑定DispatcherServlet。弹簧控制器不可用 映射并检索404

解决方法(如果有):使用web.xml配置或映射调度器 servlet可编程为特定的URL模式。例如 “/dispatcher/*”

对于EAP 6.4,这在版本6.4.1中已修复。我不知道EAP的早期版本。该漏洞已于2017年1月修复,因此您需要查找此后发布的修补程序


有权访问Red Hat solutions knowledgebase的人员也可能需要查看。

部署和启动应用程序时,您的上下文是否正确?您是否解决了此问题?否,使用注释仍然存在问题。但它在xml中的效果与预期的一样。您是按照xml的方式还是找到了解决方法?
 @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        WebApplicationContext context = getContext();
        servletContext.addListener(new ContextLoaderListener(context));
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/"); // i have a more or less big website, and can't see advantages by using "/*" mapping, this can be also a problem. 
    }

    private AnnotationConfigWebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocations("ua.company.config.WebConfig", "ua.company.config.PersistenceConfig", "ua.company.config.SecurityConfig");
        return context;
    }