Spring MVC 和 Servlets 3.0 - 你还需要 web.xml 吗? [英] Spring MVC and Servlets 3.0 - Do you still need web.xml?

查看:22
本文介绍了Spring MVC 和 Servlets 3.0 - 你还需要 web.xml 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在典型的 Spring MVC Web 应用程序中,您可以像这样在 web.xml 中声明 DispatcherServlet

In a typical Spring MVC web app, you would declare the DispatcherServlet in web.xml like so

<!-- MVC Servlet -->
<servlet>
    <servlet-name>sample</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>sample</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

以及监听器、过滤器等

使用 servlet-api 3.0,您可以使用注释 @WebServlet 声明您的 servlet,而不是将它们添加到您的 web.xml.Spring 3.2 已经有 @Configuration@EnableXYZ 用于其上下文配置.DispatcherServlet 是否有类似的东西,即.有没有办法在没有任何 xml 的情况下配置完整的 Spring 应用程序?

With servlet-api 3.0, you can declare your servlets with the annotation @WebServlet instead of adding them to your web.xml. Spring 3.2 already has @Configuration and @EnableXYZ for its context configuration. Does it have something similar for the DispatcherServlet, ie. is there a way to configure your full Spring application without any xml?

推荐答案

使用 JEE6,如果您的应用程序容器已准备好 Servlet 3.0,您需要做的是:

With JEE6, if your application container is Servlet 3.0 ready what you need to do is:

  1. 创建一个实现ServletContainerInitializer的自定义类(即com.foo.FooServletContainer)
  2. 在您的 META-INF/services 文件夹中创建一个名为 javax.servlet.ServletContainerInitializer 的文件,该文件将包含上述实现的名称 (com.foo.FooServletContainer)
  1. Create a custom class that implements ServletContainerInitializer (i.e. com.foo.FooServletContainer)
  2. Create a file in your META-INF/services folder named javax.servlet.ServletContainerInitializer which will contain the name of your implementation above (com.foo.FooServletContainer)

Spring 3 捆绑了一个名为 SpringServletContainerInitializer 的类,该类实现了上述内容(因此您无需在 META-INF/services 中创建自己的文件.这个类只是调用了一个WebApplicationInitializer.所以你只需要在你的类路径中提供一个实现它的类(以下代码取自上面的文档).

Spring 3 is bundled with a class named SpringServletContainerInitializer that implements the stuff above (so you don't need to create yourself the file in META-INF/services. This class just calls an implementation of WebApplicationInitializer. So you just need to provide one class implementing it in your classpath (the following code is taken from the doc above).

public class FooInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) {
        WebApplicationContext appContext = ...;

        ServletRegistration.Dynamic dispatcher =
           container.addServlet("dispatcher", new DispatcherServlet(appContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
 }

web.xml 就是这样,但是您需要使用 @Configuration@EnableWebMvc 等来配置 web 应用程序.

That's it for the web.xml thing, but you need to configure the webapp using @Configuration, @EnableWebMvc etc..

这篇关于Spring MVC 和 Servlets 3.0 - 你还需要 web.xml 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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