How does DispatcherServlet work if we have multiple XML Configuration file?

前端 未结 2 1674
庸人自扰
庸人自扰 2020-12-25 08:32

Questions

How does DispatcherServlet work if we have multiple XML Configuration file so how does Spring Application Context loads them and acts

相关标签:
2条回答
  • 2020-12-25 09:27

    The web.xml file controls which context file DispatcherServlet is using. If you configure web.xml to have a DispatcherServlet with name em, then by default it uses em-servlet.xml to load the web context.

    Your question is a bit confusing as to what you would actually like to do - do you want all "versions" to be available in the same instance of the application?

    If so, the method you describe sounds unorthodox for how to present multiple languages / globalizing your application. Traditionally you would just have a single instance of the application and all the controllers/instances, and then handle translating user-visible messages at the display level. Spring has excellent support for this.

    If your goal is to have a single instance of the application serve requests for all these languages/locales, then it sounds like you could do away with a lot of this redundancy.

    0 讨论(0)
  • 2020-12-25 09:28

    The web.xml file can configure multiple DispatcherServlet instances, each having its own configuration. Each DispatcherServlet instance configures a WebApplicationContext separate from other DispatcherServlet instances, so you can use the same bean names without affecting the other application context.

    <!-- configured by WEB-INF/ap-servlet.xml -->
    <servlet>
        <servlet-name>ap</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <!-- configured by WEB-INF/em-servlet.xml -->
    <servlet>
        <servlet-name>em</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    

    You must also configure web.xml to map requests to the appropriate DispatcherServlet. For example, each region could have a different URL path.

    <servlet-mapping>
        <servlet-name>ap</servlet-name>
        <url-pattern>/ap/*</url-pattern>
    </servlet-mapping>
    
    <servlet-mapping>
        <servlet-name>em</servlet-name>
        <url-pattern>/em/*</url-pattern>
    </servlet-mapping>
    
    0 讨论(0)
提交回复
热议问题