简体   繁体   English

Spring MVC 主题支持究竟是如何工作的? (对提出的具体例子的一些疑问)

[英]How exactly works the Spring MVC themes support? (some doubts about a proposed concrete example)

I am pretty new in Spring MVC and I am actually studying the theming support.我在 Spring MVC 中很新,我实际上正在研究主题支持。

So I am working on an example application that have the following characteristics:所以我正在开发一个具有以下特征的示例应用程序:

1) Into the src/webapp/resources/css are defined 3 CSS theme files, respectivelly named: theme-black.css , theme-chocolate.css and theme-dafault.css . 1) 在src/webapp/resources/css中定义了 3 个 CSS 主题文件,分别命名为: theme-black.css , theme-chocolate.csstheme-dafault.css So these files represents the CSS settings associated to a theme that have to be chosen by the user.因此,这些文件表示与必须由用户选择的主题相关联的 CSS 设置。

2) Into the servlet-context.xml configuration file that should be the file that configure the MVC support there are defined the following beans related to the theming support: 2) 在servlet-context.xml配置文件中,该文件应该是配置 MVC 支持的文件,其中定义了以下与主题支持相关的 bean:

<beans:bean id="themeSource"
    class="org.springframework.ui.context.support.ResourceBundleThemeSource">
    <beans:property name="basenamePrefix" value="META-INF.theme-" />
</beans:bean>


<beans:bean id="themeChangeInterceptor"
    class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
    <beans:property name="paramName" value="theme" />
</beans:bean>


<mvc:interceptors>
    <beans:ref bean="themeChangeInterceptor" />
</mvc:interceptors> 

<beans:bean id="themeResolver"
    class="org.springframework.web.servlet.theme.CookieThemeResolver">
    <beans:property name="defaultThemeName" value="default" />
</beans:bean>

And into the META-INF application folder there are the following 3 properties file related to the previous CSS theme files:META-INF应用程序文件夹中,有以下 3 个与之前的 CSS 主题文件相关的属性文件:

theme-black.properties :主题black.properties

css=resources/css/theme-black.css

theme-chocolate.properties :主题巧克力.properties :

css=resources/css/theme-chocolate.css

theme-default.properties :主题-default.properties

css=resources/css/theme-default.css

And now I need your help to understand what these beans exactly does.现在我需要你的帮助来理解这些豆子到底做了什么。

From what I have understand the Spring MVC theming support works in the following way:据我了解,Spring MVC 主题支持的工作方式如下:

The web application have a default theme at the beginning (is it the one named theme-default.css by default? So if I use the theme-default.css for a theme it is loaded as default theme or am I missing something?) Web 应用程序在开始时有一个默认主题(默认情况下它是名为theme-default.css主题吗?所以如果我将theme-default.css用于主题,它会作为默认主题加载还是我错过了什么?)

I need to provide to the user a set of theme from which to choose the theme that he want to use.我需要向用户提供一组主题,从中选择他想要使用的主题。

When the user choose a theme the application send a cookie to the user browser and this cookie contain the information about the chosed theme.当用户选择一个主题时,应用程序会向用户浏览器发送一个cookie ,这个 cookie 包含有关所选主题的信息。

Then, any time that the user make any HTTP request to the application (handled by a controller) there must be something that intercept these request and that read the cookie for the chosed theme identification and then use this theme for the response.然后,每当用户向应用程序发出任何 HTTP 请求(由控制器处理)时,都必须有一些东西可以拦截这些请求并读取所选主题标识的 cookie,然后使用该主题进行响应。

So I think that the previous declared beans works in the following way:所以我认为之前声明的 beans 是通过以下方式工作的:

1) The first class is the theme source : 1)第一类是主题源

<beans:bean id="themeSource"
    class="org.springframework.ui.context.support.ResourceBundleThemeSource">
    <beans:property name="basenamePrefix" value="META-INF.theme-" />
</beans:bean>

and take as input parameter a property named basenamePrefix that identify the previous properties file that contains the concrete themes path ( theme-black.properties , theme-chocolate.properties and theme-default.properties ).并将名为basenamePrefix的属性作为输入参数,该属性标识包含具体主题路径( theme-black.propertiestheme-chocolate.propertiestheme-default.properties )的先前属性文件。

So this bean should accomplish the first requirement of the theme support (identify and access to the list of themes)所以这个bean应该完成主题支持的第一个要求(识别和访问主题列表)

2) The second class is the theme interceptor : 2)第二个类是主题拦截器

<beans:bean id="themeChangeInterceptor"
    class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
    <beans:property name="paramName" value="theme" />
</beans:bean>

that is a class that I can use to intercept any request made by user and do pre action and post action and from what I have understand it define the URL parameter name that let the user to change the theme.这是一个类,我可以用它来拦截用户发出的任何请求,并执行前操作后操作,据我所知,它定义了让用户更改主题的 URL 参数名称。

So in this example the parameter name is theme so when the user call an URL like:所以在这个例子中,参数名称是theme所以当用户调用一个 URL 时:

http://localhost:8080/springchocolatestore/?theme=chocolate

it is chosing the chocolate theme that is related to the theme-chocolate.properties so the resources/css/theme-chocolate.css file will be used.它正在选择与theme-chocolate.properties相关的巧克力主题,因此将使用resources/css/theme-chocolate.css文件。

So my doubt is: the preaction is to check the precence of the ?theme=chocolate in the URL and apply the style?所以我的疑问是:preaction 是检查 URL 中?theme=chocolate的优先级并应用样式?

3) The third bean is the theme resolver : 3)第三个bean是主题解析器

<beans:bean id="themeResolver"
    class="org.springframework.web.servlet.theme.CookieThemeResolver">
    <beans:property name="defaultThemeName" value="default" />
</beans:bean>

So this resolver use cookie to identify the chosed theme.所以这个解析器使用cookie来识别选择的主题。

So, for example, if I chose the chocolate theme (by the request http://localhost:8080/springchocolatestore/?theme=chocolate ) the web app send to the browser a cookie that store the information that specify that the chocolate theme have to be used.因此,例如,如果我选择巧克力主题(通过请求http://localhost:8080/springchocolatestore/?theme=chocolate ),Web 应用程序会向浏览器发送一个 cookie,该 cookie 存储指定巧克力主题具有的信息要使用的。

Then if I reload again the application the page still use the chocolate theme (and not the default theme).然后,如果我再次重新加载应用程序,页面仍然使用巧克力主题(而不是默认主题)。

It means that the CookieThemeResolver had access to this cookie, read it and use this information to select the theme to use?这意味着CookieThemeResolver可以访问此 cookie,读取它并使用此信息来选择要使用的主题?

So I have the following doubts: this cookie is on my file system and the web application is on a remote application server.所以我有以下疑问:这个cookie在我的文件系统上,而web应用在远程应用服务器上。 How can it access to the cookie on my file system?它如何访问我的文件系统上的 cookie? Where this cookie is into my file system and what is its name ?这个 cookie 在我的文件系统中的什么地方,它的名字是什么? (I want to open it with text editor to examinate it) (我想用文本编辑器打开查看)

Finnally it is also declared this configuration:最后还声明了这个配置:

<mvc:interceptors>
    <beans:ref bean="themeChangeInterceptor" />
</mvc:interceptors> 

What exactly does this configuration?这个配置到底是做什么的?

Tnx Tnx

org.springframework.ui.context.support.ResourceBundleThemeSource

Loads properties files from the specified base usually classpath.From spring api docs ResourceBundleThemeSource is a ThemeSource implementation that looks up an individual ResourceBundle per theme.通常从指定的基类路径加载属性文件。来自 spring api docs ResourceBundleThemeSource是一个 ThemeSource 实现,它查找每个主题的单个 ResourceBundle。 The theme name gets interpreted as ResourceBundle basename, supporting a common basename prefix for all themes.主题名称被解释为 ResourceBundle 基本名称,支持所有主题的通用基本名称前缀。

org.springframework.web.servlet.theme.ThemeChangeInterceptor

Interceptor that allows for changing the current theme on every request, via a configurable request parameter (default parameter name: "theme").允许通过可配置的请求参数(默认参数名称:“theme”)更改每个请求的当前主题的拦截器。 More information can be found in spring api docs as well更多信息也可以在spring api 文档中找到

org.springframework.web.servlet.theme.CookieThemeResolver

ThemeResolver implementation that uses a cookie sent back to the user in case of a custom setting, with a fallback to the default theme. ThemeResolver 实现,在自定义设置的情况下使用发送回用户的 cookie,并回退到默认主题。 This is particularly useful for stateless applications without user sessions.这对于没有用户会话的无状态应用程序特别有用。 Again more information here再次在此处获得更多信息

this cookie is on my file system and the web application is on a remote application server.此 cookie 在我的文件系统上,而 Web 应用程序在远程应用程序服务器上。 How can it access to the cookie on my file system?它如何访问我的文件系统上的 cookie? Where this cookie is into my file system and what is its name ?这个 cookie 在我的文件系统中的什么地方,它的名字是什么?

What's an HTTP cookie in the first place?首先什么是 HTTP cookie?

To check cookies sent by any website use firebug if you use Firefox or developer tools if you use chrome要检查任何网站发送的 cookie,如果您使用 Firefox,请使用firebug;如果您使用 chrome,请使用开发人员工具

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM