Kle*_*ota 6 java spring spring-mvc
在我的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("/");
}
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我想在我的应用程序中添加其他调度程序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("/");
Run Code Online (Sandbox Code Playgroud)
并添加一个新的类,如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;
}
Run Code Online (Sandbox Code Playgroud)
M. *_*num 12
你可以拥有任意数量的DispatcherServlets东西.基本上你需要做的是复制配置并给servlet一个不同的名称(否则它将覆盖前一个),并为它提供一些单独的配置类(或xml文件).
你的控制器不应该关心DispatcherServlet它们在哪个运行时你是否应该包含检测它的代码(如果你添加另一个,那么你需要继续修改你的控制器以解决这个问题).
但是,虽然通常可以有多个servlet,但是不需要多个servlet,并且可以使用单个实例处理它DispatcherServlet.
| 归档时间: |
|
| 查看次数: |
21834 次 |
| 最近记录: |