如何在同一屏幕上针对不同的应用程序上下文获取不同的 Spring 标签消息
How to get different Spring label message on the same screen for different context of application
我在一个应用程序中有不同的上下文(比如 A 和 B),这两个上下文使用相同的屏幕,但我在屏幕字段上显示了不同的标签名称。
资源包如下
对于上下文 A,sample.code=Text From Application A
进入 sampleA.properties
对于上下文 B,sample.code=Text From Application B
进入 sampleB.properties
而且,我可以从会话属性中区分上下文。在那种情况下,我如何覆盖 Spring MessageTag
并阅读关于上下文集的消息?
JSP: <spring:message code="sample.code" />
感谢您的帮助。
为不同的 servlet 上下文配置单独注册 ResourceBundleMessageSource
例如。在你的 AbstractAnnotationConfigDispatcherServletInitializer
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// root context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(RootConfig.class); // configuration class for root context
servletContext.addListener(new ContextLoaderListener(rootContext));
// dispatcher servlet 1
AnnotationConfigWebApplicationContext webContext1 =
new AnnotationConfigWebApplicationContext();
webContext1.setParent(rootContext);
webContext1.register(WebConfig1.class); // configuration class for servlet 1
ServletRegistration.Dynamic dispatcher1 =
servletContext.addServlet("dispatcher1", new DispatcherServlet(webContext1));
dispatcher1.setLoadOnStartup(1);
dispatcher1.addMapping("/subcontext1");
// dispatcher servlet 2
AnnotationConfigWebApplicationContext webContext2 =
new AnnotationConfigWebApplicationContext();
webContext1.setParent(rootContext);
webContext1.register(WebConfig2.class); // configuration class for servlet 1
ServletRegistration.Dynamic dispatcher2 =
servletContext.addServlet("dispatcher2", new DispatcherServlet(webContext2));
dispatcher2.setLoadOnStartup(1);
dispatcher2.addMapping("/subcontext1");
}
WebConfig1
@Bean
public ResourceBundleMessageSource configureResourceBundleMessageSource() {
ResourceBundleMessageSource resource = new ResourceBundleMessageSource();
resource.setBasename("sampleA");
return resource;
}
WebConfig2
@Bean
public ResourceBundleMessageSource configureResourceBundleMessageSource() {
ResourceBundleMessageSource resource = new ResourceBundleMessageSource();
resource.setBasename("sampleB");
return resource;
}
我在一个应用程序中有不同的上下文(比如 A 和 B),这两个上下文使用相同的屏幕,但我在屏幕字段上显示了不同的标签名称。
资源包如下
对于上下文 A,sample.code=Text From Application A
进入 sampleA.properties
对于上下文 B,sample.code=Text From Application B
进入 sampleB.properties
而且,我可以从会话属性中区分上下文。在那种情况下,我如何覆盖 Spring MessageTag
并阅读关于上下文集的消息?
JSP: <spring:message code="sample.code" />
感谢您的帮助。
为不同的 servlet 上下文配置单独注册 ResourceBundleMessageSource
例如。在你的 AbstractAnnotationConfigDispatcherServletInitializer
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// root context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(RootConfig.class); // configuration class for root context
servletContext.addListener(new ContextLoaderListener(rootContext));
// dispatcher servlet 1
AnnotationConfigWebApplicationContext webContext1 =
new AnnotationConfigWebApplicationContext();
webContext1.setParent(rootContext);
webContext1.register(WebConfig1.class); // configuration class for servlet 1
ServletRegistration.Dynamic dispatcher1 =
servletContext.addServlet("dispatcher1", new DispatcherServlet(webContext1));
dispatcher1.setLoadOnStartup(1);
dispatcher1.addMapping("/subcontext1");
// dispatcher servlet 2
AnnotationConfigWebApplicationContext webContext2 =
new AnnotationConfigWebApplicationContext();
webContext1.setParent(rootContext);
webContext1.register(WebConfig2.class); // configuration class for servlet 1
ServletRegistration.Dynamic dispatcher2 =
servletContext.addServlet("dispatcher2", new DispatcherServlet(webContext2));
dispatcher2.setLoadOnStartup(1);
dispatcher2.addMapping("/subcontext1");
}
WebConfig1
@Bean
public ResourceBundleMessageSource configureResourceBundleMessageSource() {
ResourceBundleMessageSource resource = new ResourceBundleMessageSource();
resource.setBasename("sampleA");
return resource;
}
WebConfig2
@Bean
public ResourceBundleMessageSource configureResourceBundleMessageSource() {
ResourceBundleMessageSource resource = new ResourceBundleMessageSource();
resource.setBasename("sampleB");
return resource;
}