gpt4 book ai didi

java - 什么时候我们需要多个Dispatcher Servlet?

转载 作者:行者123 更新时间:2023-11-30 05:53:35 24 4
gpt4 key购买 nike

在哪些场景下,我们需要多个Dispatcher-Servlet
谁能告诉我多个 Dispatcher-Servlet 的用例。
我认为每个用例都可以通过使用 single Dispatcher Servlet 来解决。

最佳答案

来自文档

A web application can define any number of DispatcherServlets. Each servlet will operate in its own namespace, loading its own application context with mappings, handlers, etc. Only the root application context as loaded by ContextLoaderListener, if any, will be shared.

多个调度程序 servlet 的优点 为什么我们需要多个调度程序 servlet? 我们什么时候需要多个调度程序 servlet?

简单的答案是 DispatcherServlet's多种形式的功能

调度程序 servlet 功能

<小时/>

<小时/>我将尝试解释 DispatcherServlet

提供的一些功能

声明多个调度程序 servlet
考虑我们有两个调度程序 servlet (DS),其中 DS1、DS2 配置有不同的 url 模式(**.simple, **.beanName),并且它们使用不同的调度程序 servlet 配置,如下所示。

DispatcherServlet     - simpleUrlHandlerDispatcherServlet
contextConfigLocation - /WEB-INF/simpleUrlHandlerMapping.xml
<url-pattern>*.simple</url-pattern>

DispatcherServlet - beanNameUrlHandlerDispatcherServlet
contextConfigLocation - /WEB-INF/beanNameUrlHandlerMapping.xml
<url-pattern>*.beanName</url-pattern>

优点1:我们可以为不同的URL集设置不同的HandlerMapping

DS1 bean 名称 url 处理程序映射配置

<bean name="/hello.beanName" class="com.pvn.mvc.HelloController" />
<bean name="/hi.beanName" class="com.pvn.mvc.HiController" />

DS2 简单 URL 处理程序映射配置

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.simple">simpleHello</prop>
<prop key="/hi.simple">simpleHi</prop>
</props>
</property>
</bean>

优点 2: 我们可以为不同的 URL 集使用不同的 View 解析器。

DS1 的InternalResourceViewResolver
它仅处理前缀+返回的字符串+后缀
<强> TilesViewResolver DS2
它的实现由 apachetiles 提供,这是一个基于布局/骨架的插件高级功能,如下所示。
enter image description here或者,如果我们对不同的 URL 集使用不同布局的 TilesViewResolver
匿名用户 - 不同的布局
登录用户 - 不同的布局

优点3:我们可以为不同的URL集使用不同的主题解析器。
这些解析器持续监视 cookie/ session 来解析主题并提供合格的样式表/主题(如下图所示)。下面仅给出 CookieThemeResolver 结果的示例.
注意:这不是关于主题配置,而是关于主题解析器配置。

enter image description here

优点 4: 我们可以为不同的 URL 集使用不同的区域设置解析器。
这些解析器持续监视 cookie/session/accept-header 以解析区域设置并加载合格的应用程序消息(如下图所示)。下面仅给出 CookieLocaleResolver 结果的示例.
注意:这不是关于区域设置配置,而是关于区域设置解析器配置。
enter image description here

关于java - 什么时候我们需要多个Dispatcher Servlet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53533321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com