在我的spring应用程序中,我有以下Spring环境配置类:
WebAppInitializer.java
@Order(value=1)
public class WebAppInitializer implements WebApplicationInitializer {
@SuppressWarnings("resource")
@Override
public void onStartup(ServletContext container) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(WebAppConfig.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("/");
}
}
WebAppConfig.java
@EnableWebMvc
@EnableTransactionManagement(mode=AdviceMode.PROXY,proxyTargetClass=true)
@ComponentScan(value="spring.webapp.lojavirtual")
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/bootstrap/**").addResourceLocations("/bootstrap/").setCachePeriod(31556926);
registry.addResourceHandler("/extras/**").addResourceLocations("/extras/").setCachePeriod(31556926);
registry.addResourceHandler("/jquery/**").addResourceLocations("/jquery/").setCachePeriod(31556926);
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
DispatcherConfig.java
@Controller
@Import(WebAppConfig.class)
public class DispatcherConfig {
@Bean
public ViewResolver jspResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
我想在我的应用程序中添加其他调度程序servlet.我的第一个想法是将以下代码添加到上面的类中:
在WebAppInitializer.java中
像这样的新块,在适当的位置更改名称:
// 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("/");
并添加一个新的类,如DispatcherConfig.java,其名称在上面的代码中选择.
我的问题是:
1)首先,我的方法是添加新的调度程序servlet的正确方法?
2)其次,如果问题1的答案为“是”,我应该在WebAppInitializer中更改哪些名称?
3)在我的控制器中,我如何确定我的请求应该去哪个调度程序servlet?我的控制器使用如下方法调用视图:
@RequestMapping(value="view_mapping")
public method() {
ModelAndView mav = new ModelAndView()
mav.setViewName("view_name");
return mav;
}
最佳答案
原文链接:https://www.f2er.com/spring/432689.html