使用 Spring Mvc WebApplicationInitializer,但未找到 HTTP 请求的映射
Using Spring Mvc WebApplicationInitializer, But No mapping found for HTTP request
我正在使用 Eclipse 中的 Spring MVC 和 JBoss 6.2.
设置示例代码库
但是我得到“404” http://localhost:8080/rest/simple/main
Jboss 日志如下:
2015-07-29 11:51:27,356 ERROR [controller.simpleController] (http-/0.0.0.0:8080-1) get request
2015-07-29 11:51:27,391 WARN [org.springframework.web.servlet.PageNotFound] (http-/0.0.0.0:8080-1) No mapping found for HTTP request with URI [/rest/WEB-INF/views/main.jsp] in DispatcherServlet with name 'dispatcher'
目录:
>rest-server-simple
> -src
> -main
-java
-config
-InitConfig.java
-ServletConfig.java
-controller
-simpleController.java
> -webapp
> -WEB-INF
-jboss-web.xml
>views
-main.jsp
初始化配置:
public class InitConfig implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(ServletConfig.class);
ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx));
registration.setLoadOnStartup(1);
registration.addMapping("/*"); }}
Servlet配置:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages ="controller")
public class ServletConfig {
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
jboss-web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<context-root>/rest</context-root>
</jboss-web>
简单控制器:
@Controller
@RequestMapping(value = "/simple")
public class simpleController {
private static final Logger logger = LoggerFactory.getLogger(simpleController.class);
@RequestMapping(value = "/main", method = RequestMethod.GET)
public String hello(){
logger.error("get request");
return "main";
}
}
registration.addMapping("/*");
改为
registration.addMapping("/");
/*
和 /
有区别。
/*
表示每个请求都将由 DispatcherServlet
处理,在这种情况下检索 jsp
或类似 .../abc.xyz
等的任何内容也将转发给 Dispatcher
,所以当控制器请求 view
它实际上寻找 RequestMapping
映射到 /WEB-INF/views/main.jsp
但
/
告诉容器只有那些没有 pathinfo i.e /rest/simple/main
的请求才会被 DispatcherServlet
.
处理
更新#1
嗯..我发现 jboss AS 7
不喜欢覆盖 default servlet
即 /
没有 web.xml
因此你仍然得到 404
甚至没有在记录器上得到任何东西,原因很简单,因为 Dispatcher
从未映射到任何 url
。如果你想检查只是在 addMapping("/*");
之后添加以下内容
System.out.println("registration.getMappings() = " + registration.getMappings());
它与 Tomcat >= 7.0.15
或 WildFly
都检查过。
要使其在 JBoss7
上运行,有几个选项:
1. 将 DispatcherServlet
映射从 /
更改为 *.htm
或除 DefaultServlet Mapping
.
之外的内容
2. 将您的配置切换为 web.xml
。您必须在那里初始化 DispatcherServlet
并将 Annotated class 作为 `contextConfigLocation. Check here for REF
我正在使用 Eclipse 中的 Spring MVC 和 JBoss 6.2.
设置示例代码库但是我得到“404” http://localhost:8080/rest/simple/main
Jboss 日志如下:
2015-07-29 11:51:27,356 ERROR [controller.simpleController] (http-/0.0.0.0:8080-1) get request
2015-07-29 11:51:27,391 WARN [org.springframework.web.servlet.PageNotFound] (http-/0.0.0.0:8080-1) No mapping found for HTTP request with URI [/rest/WEB-INF/views/main.jsp] in DispatcherServlet with name 'dispatcher'
目录:
>rest-server-simple
> -src
> -main
-java
-config
-InitConfig.java
-ServletConfig.java
-controller
-simpleController.java
> -webapp
> -WEB-INF
-jboss-web.xml
>views
-main.jsp
初始化配置:
public class InitConfig implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(ServletConfig.class);
ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx));
registration.setLoadOnStartup(1);
registration.addMapping("/*"); }}
Servlet配置:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages ="controller")
public class ServletConfig {
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
jboss-web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<context-root>/rest</context-root>
</jboss-web>
简单控制器:
@Controller
@RequestMapping(value = "/simple")
public class simpleController {
private static final Logger logger = LoggerFactory.getLogger(simpleController.class);
@RequestMapping(value = "/main", method = RequestMethod.GET)
public String hello(){
logger.error("get request");
return "main";
}
}
registration.addMapping("/*");
改为
registration.addMapping("/");
/*
和 /
有区别。
/*
表示每个请求都将由 DispatcherServlet
处理,在这种情况下检索 jsp
或类似 .../abc.xyz
等的任何内容也将转发给 Dispatcher
,所以当控制器请求 view
它实际上寻找 RequestMapping
映射到 /WEB-INF/views/main.jsp
但
/
告诉容器只有那些没有 pathinfo i.e /rest/simple/main
的请求才会被 DispatcherServlet
.
更新#1
嗯..我发现 jboss AS 7
不喜欢覆盖 default servlet
即 /
没有 web.xml
因此你仍然得到 404
甚至没有在记录器上得到任何东西,原因很简单,因为 Dispatcher
从未映射到任何 url
。如果你想检查只是在 addMapping("/*");
System.out.println("registration.getMappings() = " + registration.getMappings());
它与 Tomcat >= 7.0.15
或 WildFly
都检查过。
要使其在 JBoss7
上运行,有几个选项:
1. 将 DispatcherServlet
映射从 /
更改为 *.htm
或除 DefaultServlet Mapping
.
之外的内容
2. 将您的配置切换为 web.xml
。您必须在那里初始化 DispatcherServlet
并将 Annotated class 作为 `contextConfigLocation. Check here for REF