Java 在 Spring 应用程序中使用多个调度程序 servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23049736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Working with multiple dispatcher servlets in a spring application
提问by Kleber Mota
In my spring application, I have the following configuration classes for the spring environment:
在我的 spring 应用程序中,我有以下 spring 环境的配置类:
WebAppInitializer.java
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
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;
}
}
I want add others dispatcher servlet to my application. My first idea was ass the following code to the classes above:
我想将其他调度程序 servlet 添加到我的应用程序中。我的第一个想法是将以下代码添加到上面的类中:
In WebAppInitializer.java
在 WebAppInitializer.java 中
A new block like this, changing the names in the proper places:
像这样的新块,在适当的位置更改名称:
// 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("/");
And add a new class like DispatcherConfig.java, with the name chosen in the code above.
并添加一个新类,如 DispatcherConfig.java,其名称在上面的代码中选择。
My questions are:
我的问题是:
1) First of all, my approach is the right way to add a new dispatcher servlet?
1)首先,我的方法是添加新的调度程序servlet的正确方法吗?
2) Second, if the answer for question 1 is 'yes', which names I should change in the WebAppInitializer?
2) 其次,如果问题 1 的答案是“是”,我应该在 WebAppInitializer 中更改哪些名称?
3) In my controller(s), how I sinalize for which dispatcher servlet my requisition should go? My controllers use methods like the following for call a view:
3)在我的控制器中,我如何将我的请求应该去哪个调度程序servlet进行sinalize?我的控制器使用如下方法调用视图:
@RequestMapping(value="view_mapping")
public method() {
ModelAndView mav = new ModelAndView()
mav.setViewName("view_name");
return mav;
}
采纳答案by M. Deinum
You can have as many DispatcherServlets
as you want. Basically what you need to do is duplicate the configuration and give the servlet a different name (else it will overwrite the previous one), and have some separate configuration classes (or xml files) for it.
您可以拥有任意数量的DispatcherServlets
。基本上,您需要做的是复制配置并为 servlet 指定一个不同的名称(否则它将覆盖前一个名称),并为其提供一些单独的配置类(或 xml 文件)。
Your controllers shouldn't care in which DispatcherServlet
they run neither should you include code to detect that (what if you add another, and another you would need to keep modifying your controllers to fix that).
您的控制器不应该关心DispatcherServlet
它们在何处运行,您也不应该包含代码来检测它(如果您添加另一个,另一个您将需要不断修改您的控制器以解决该问题)。
However while you can have multiple servlets in general there isn't much need for multiple servlets and you can handle it with a single instance of the DispatcherServlet
.
然而,虽然您通常可以拥有多个 servlet,但并不需要多个 servlet,您可以使用DispatcherServlet
.
回答by Jekin Kalariya
If you are using spring 3.2 or above you can go with below code.
如果您使用的是 spring 3.2 或更高版本,则可以使用以下代码。
Make different class for all the dispacher servlet
with overriding getServletName()
method, to avoid same name conflicts.
为所有dispacher servlet
具有覆盖getServletName()
方法的类创建不同的类,以避免相同的名称冲突。
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return new Class<?>[] { RootConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return new Class<?>[] { WebConfig.class };
}
@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return new String[] { "/config1/*" };
}
}
public class WebAppInitializer2 extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return new Class<?>[] { RootConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return new Class<?>[] { WebConfig2.class };
}
@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return new String[] { "/config2/*" };
}
@Override
protected String getServletName() {
// TODO Auto-generated method stub
return "config2";
}
}
回答by wonder
We can have to multiple Dispatcher Servlets, like we can have 2(or more) DispatcherServlet with 2( or more) servlets name.So the D1 and D2 could map to different URL path.Example:-
我们可以拥有多个 Dispatcher Servlet,就像我们可以拥有 2 个(或更多)DispatcherServlet 和 2 个(或更多)servlet 名称一样。因此 D1 和 D2 可以映射到不同的 URL 路径。例如:-
<!-- configured by WEB-INF/mac-servlet.xml -->
<servlet>
<servlet-name>mac</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- configured by WEB-INF/windows-servlet.xml -->
<servlet>
<servlet-name>windows</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
URL path could be mapped like :-
URL 路径可以映射为:-
<servlet-mapping>
<servlet-name>mac</servlet-name>
<url-pattern>/mac/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>windows</servlet-name>
<url-pattern>/windows/*</url-pattern>
</servlet-mapping>