java - 如何在同一屏幕上针对不同的应用程序上下文获取不同的 Spring 标签消息

标签 java spring spring-mvc resourcebundle

我在一个应用程序中有不同的上下文(例如 A 和 B),该应用程序对两个上下文使用相同的屏幕,但我在屏幕字段上显示不同的标签名称。

资源包如下。

对于上下文 A,sample.code=Text From Application A进入sampleA.properties

对于上下文 B,sample.code=Text From Application B进入sampleB.properties

而且,我可以区分上下文和 session 属性。在这种情况下,我如何覆盖 Spring MessageTag并阅读有关上下文集的消息?

JSP:<spring:message code="sample.code" />

感谢您的帮助。

最佳答案

单独注册ResourceBundleMessageSource针对不同的servlet上下文配置

例如。在 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;
}

关于java - 如何在同一屏幕上针对不同的应用程序上下文获取不同的 Spring 标签消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40460020/

相关文章:

java - 如何在gradle build中编写依赖项

java - Spring:发布请求未到达 Controller 类

java - 在 Spring Boot 2.0.4.RELEASE 中的 RestController 中捕获 ArithmeticException

Java JTree 节点是一个可点击的 URL 链接

java - 结果集未正确定位

c# - 使用依赖注入(inject)的现实世界解决方案

java - Spring MVC 一种输入字段形式

java - 如何通过 Java 中的条件锁定实例化?

JavaFX WebEngine 卡在 "Running"状态

java - 如何在 openshift.com 上部署 Spring MVC 项目