我正在研究現有的Spring MVC 3項目,同時查看Spring和上下文配置文件,我感到困惑,請清除它或建議我如果出現錯誤。需要幫助瞭解彈簧配置文件
UPADTE root-context.xml
文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:messages</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
<bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource">
<property name="basenamePrefix" value="detailtheme-" />
</bean>
<bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver">
<property name="defaultThemeName" value="en" />
</bean>
<!-- Helper bean to load all properties files -->
<bean id="LoadPropertiesFiles" class="org.commons.utilities.LoadPropertiesFileHelper"
init-method="loadPropertiesFileMethod" lazy-init="false" />
</beans>
在這裏,我不明白什麼是lang
?我知道這是一個可靠的名字,其價值被分配到paramName
(DEFAULT_PARAM_NAME
),但我不明白價值是如何分配到lang
,因爲我沒有找到任何單個位置,其中一些值(如en,hi..etc )已設置。
<mvc:interceptors>
<beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<beans:property name="paramName" value="lang" />
</beans:bean>
...
</mvc:interceptors>
爲什麼兩個bean同一類的定義,這是錯的:
最令人困惑的事情與同級別多一個豆在servlet-context.xml
的定義?如果不是,那麼什麼是root-context.xml
和servlet-context.xml
中定義的bean的工作?
下面是web.xml
供參考:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Listener to prevent class loader leaks -->
<listener>
<listener-class>se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventor</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<jsp-config>
<taglib>
<taglib-uri>/tagTld</taglib-uri>
<taglib-location>/resources/tld/EnumTag.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
所以這就像是'LocaleChangeInterceptor'只會攔截具有查詢參數的名稱爲'lang'的請求?或者它會攔截所有請求並檢查查詢參數? – Amogh
它會攔截所有請求,但只有當lang參數存在時纔會採取行動 –
如果請求是「POST」,該怎麼辦?我想改變它的語言環境? – Amogh