在JBoss上使用Spring MVC Java Config时发生404错误 [英] 404 error using Spring MVC Java Config on JBoss

查看:109
本文介绍了在JBoss上使用Spring MVC Java Config时发生404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Java Config编写了一个小的Spring MVC应用程序.它在Tomcat上工作得很好,但在JBoss EAP 6.2上却不能.它已成功部署在JBoss上,但是当我请求Spring MVC定义的任何页面和浏览器中的404错误时,都会收到此警告.

I wrote a small Spring MVC application with Java Config. It is working perfectly fine on Tomcat but not on JBoss EAP 6.2. It gets successfully deployed on JBoss but I get this warning when I request any page defined by Spring MVC and 404 error in browser.

WARN [org.springframework.web.servlet.PageNotFound] (http-/127.0.0.1:8080-1) No mapping found for HTTP request with URI [/example-web/pages/login.jsp] in DispatcherServlet with name 'dispatcher'

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

Here you can see my code:

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

这是我的Spring MVC配置:

Here is my Spring MVC configuration:

@EnableWebMvc
@ComponentScan("com.spring.example.w.controller")
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("login").setViewName("login");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/pages/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

和RootConfig:

And RootConfig:

@Configuration
@ComponentScan
public class RootConfiguration {
}

在部署期间,我可以在日志中看到请求确​​实被映射了:

During deployment, I can see in the log that the requests do get mapped:

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上运行.预先感谢.

Any help about why I get 404 error and this warning is highly appreciated. Once again I should emphasize that it is working on Tomcat. Thanks in advance.

推荐答案

您可能正在遇到

It's possible that you were experiencing Red Hat bug 1094248, "Default servlet can't be overridden without web.xml". This issue apparently affects EAP 6.2, 6.3, and 6.4.0. From the bug report:

将Spring调度程序servlet映射到url模式"/"不起作用 以编程方式.换句话说,覆盖默认的servlet不是 Java代码可以实现.

Mapping Spring dispatcher servlet to url pattern "/" does not work programmatically. In other words, overriding default servlet is not possible with Java code.

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

Consequence: Many users map Spring's DispatcherServlet to '/', and this works fine when done in web.xml. However when this configuration is done programmatically, default servlet is bind to "/" first. This prevents to bind DispatcherServlet later. Spring controllers are not mapped and 404 is retrieved.

解决方法(如果有):使用web.xml配置或地图调度程序 以编程方式将servlet设置为某些特定的URL模式.例如 "/dispatcher/*"

Workaround (if any): Use web.xml configuration or map dispatcher servlet programmaticaly to some specific URL pattern. e.g. "/dispatcher/*"

对于EAP 6.4,此问题已在6.4.1版中修复.我不知道EAP的早期版本.该错误已于2017年1月修复,因此您需要查找此后发布的补丁.

For EAP 6.4, this was fixed in version 6.4.1. I don't know about earlier versions of EAP. The bug was fixed in January 2017, so you'd want to look for patches issued after then.

有权访问Red Hat解决方案知识库的人们可能还希望查看解决方案1211203 .

People with access to Red Hat's solutions knowledgebase may also want to look at solution 1211203.

这篇关于在JBoss上使用Spring MVC Java Config时发生404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆