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

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

问题描述

在一个典型的Spring MVC的Web应用程序,你会声明 DispatcherServlet的的web.xml 像这样

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>

随着监听器,滤波器等。

Along with listeners, filters, etc.

使用Servlet的API 3.0,你可以用注解声明的 @WebServlet ,而不是把它们添加到您的 web.xml中小服务程序。春季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. 创建一个实现<一的自定义类href=\"http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContainerInitializer.html\">ServletContainerInitializer (即 com.foo.FooServletContainer

  2. 创建名为您的 META-INF /服务文件夹中的文件 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)

春季3捆绑命名为 SpringServletContainerInitializer A类实现上述(所以你不需要自己创建的文件元的东西-INF /服务,这个类只是调用的<一个实施href=\"http://static.springsource.org/spring/docs/current/javadoc-api/org/springframework/web/WebApplicationInitializer.html\"><$c$c>WebApplicationInitializer.所以,你只需要提供一个类在类路径中实现它(以下code从上面的文档拍摄)。

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 的事情,但你需要使用配置Web应用程序 @Configuration @EnableWebMvc

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

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

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