使用 java 配置类时如何正确配置 Spring MVC Web 应用程序?
我通过 @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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要任何 XML 文件。但您必须告诉 Dispatcher 不要查找默认文件:
仅供参考:事实上,在 Servlet 3 中也不再需要 web.xml。
You don't need any XML file. But you must tell the Dispatcher to not look for the default file:
Just for your information: in fact, in Servlet 3 web.xml is no longer required, too.