程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用基于纯Java的配置来配置Spring MVC?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何使用基于纯Java的配置来配置Spring MVC??

开发过程中遇到如何使用基于纯Java的配置来配置Spring MVC?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何使用基于纯Java的配置来配置Spring MVC?的解决方法建议,希望对你解决如何使用基于纯Java的配置来配置Spring MVC?有所启发或帮助;

你需要对进行以下更改,web.xml以支持基于Java的配置。这将告诉你dispatcherServlet使用基于注释的Java配置加载配置AnnotationConfigWebApplicationContext。你只需要将Java配置文件的位置传递给contextConfigLOCATIOnparam,如下所示

<servlet>
  <servlet-name>springdispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.dispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
   </init-param>
   <init-param>
    <param-name>contextConfigLOCATIOn</param-name>
    <param-value>/*path to your WebSpringConfig*/ </param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>

更新:在不更改web.xml的情况下进行相同操作

你甚至可以在没有web.xmlServlet规范3.0 web.xml可选的情况下执行此操作。你只需要实现/配置WebApplicationInitializer接口来配置ServletContext它将允许你以dispatcherServlet编程方式创建,配置和执行注册。好处是可以WebApplicationInitializer自动检测到。

总而言之,一个需要实现的WebApplicationInitializer摆脱方法web.xml

 public class MyWebAppInitializer implements WebApplicationInitializer {

 @OverrIDe
 public voID onStartup(ServletContext container) {
  // Create the 'root' Spring application context
  AnnotationConfigWebApplicationContext rootContext =
                       new AnnotationConfigWebApplicationContext();
  rootContext.register(WebSpringConfig.class);

  // Manage the lifecycle of the root application context
  container.addListener(new ContextLoaderListener(rootContext));

  // Create the dispatcher servlet's Spring application context
  AnnotationConfigWebApplicationContext dispatcherContext =
                     new AnnotationConfigWebApplicationContext();
  dispatcherContext.register(dispatcherConfig.class);

  // Register and map the dispatcher servlet
  ServletRegistration.Dynamic dispatcher =
    container.addServlet("dispatcher", new dispatcherServlet(dispatcherContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapPing("/");
  }
}

解决方法

我有一个我认为非常简单的Spring MVC设置。我的applicationContext.xml是这样的

<mvc:Annotation-driven />
<mvc:resources mapping="/css/**" LOCATIOn="/css/" />
<context:property-placeholder LOCATIOn="classpath:controller-test.properties" />
<bean class="org.springframework.web.servlet.view.InternalresourceViewResolver"
    p:prefix="/WEB-INF/views/" p:suffix=".jsp" />

我的web.xml当前是这样的

  <servlet>
   <servlet-name>springDispatcherServlet</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
      <param-name>contextConfigLOCATIOn</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- Map all requests to the DispatcherServlet for handling -->
  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

我正在尝试将此设置转换为基于Java的纯配置。我已经搜索了Web,到目前为止,我已经提出了一些东西(这些东西可以解释)(如何做),但是没有解释如何在环境(即Web上下文)中注册该Java配置。

到目前为止,我对@Configuration的了解是:

 @Configuration
 @EnableWebMvc
 @Propertysource("classpath:controller.properties")
 @ComponentScan("com.project.web")
 public class WebSpringConfig extends WebMvcConfigurerAdapter {

 @Override
 public void addresourceHandlers(resourceHandlerRegistry registry) {
    registry.addresourceHandler("/css/**").addresourceLOCATIOns("/css/");
 }

 @Bean
 public ViewResolver configureViewResolver() {
     InternalresourceViewResolver viewResolve = new InternalresourceViewResolver();
     viewResolve.setPrefix("/WEB-INF/views/");
     viewResolve.setSuffix(".jsp");

     return viewResolve;
 }

 @Override
 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
   configurer.enable();
 }
}

如何在Web容器中注册?我正在使用最新的Spring(4.02)。

谢谢!

@H_772_70@ @H_772_70@
@H_772_70@

大佬总结

以上是大佬教程为你收集整理的如何使用基于纯Java的配置来配置Spring MVC?全部内容,希望文章能够帮你解决如何使用基于纯Java的配置来配置Spring MVC?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。