使用 java 配置类时如何正确配置 Spring MVC Web 应用程序?

发布于 2025-01-08 04:54:07 字数 1204 浏览 0 评论 0原文

我通过 @Configuration 类使用 Spring MVC:

@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter {
    // more stuff
}

在我的 web.xml 中,我创建了 ApplicationContext:

<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>my.package.WebConfiguration</param-value>
</context-param>

我还创建了一个 DispatcherServlet,如下所示:

<servlet>
    <servlet-name>mywebapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

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

为了让 DispatcherServlet 工作,我现在需要一个 mywebapp-servlet.xml,它是空的。我真的需要 mywebapp-servlet.xml 文件吗?

I'm using Spring MVC via a @Configuration class:

@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter {
    // more stuff
}

In my web.xml I create the ApplicationContext:

<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>my.package.WebConfiguration</param-value>
</context-param>

I also create a DispatcherServlet, as follows:

<servlet>
    <servlet-name>mywebapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

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

In order to get the dispatcherServlet to work I need a mywebapp-servlet.xml Right now, it's empty. Do I actually need the mywebapp-servlet.xml file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱々樱花落 2025-01-15 04:54:07

您不需要任何 XML 文件。但您必须告诉 Dispatcher 不要查找默认文件:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>my.pack.WebConfiguration</param-value>
</context-param>

<servlet>
    <servlet-name>mywebapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

仅供参考:事实上,在 Servlet 3 中也不再需要 web.xml。

You don't need any XML file. But you must tell the Dispatcher to not look for the default file:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>my.pack.WebConfiguration</param-value>
</context-param>

<servlet>
    <servlet-name>mywebapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

Just for your information: in fact, in Servlet 3 web.xml is no longer required, too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文