parent
9cfece0d54
commit
b8c51a2767
|
@ -206,18 +206,17 @@ another file or files. The following example shows how to do so:
|
|||
<beans>
|
||||
<import resource="services.xml"/>
|
||||
<import resource="resources/messageSource.xml"/>
|
||||
<import resource="/resources/themeSource.xml"/>
|
||||
|
||||
<bean id="bean1" class="..."/>
|
||||
<bean id="bean2" class="..."/>
|
||||
</beans>
|
||||
----
|
||||
|
||||
In the preceding example, external bean definitions are loaded from three files:
|
||||
`services.xml`, `messageSource.xml`, and `themeSource.xml`. All location paths are
|
||||
In the preceding example, external bean definitions are loaded from the files
|
||||
`services.xml` and `messageSource.xml`. All location paths are
|
||||
relative to the definition file doing the importing, so `services.xml` must be in the
|
||||
same directory or classpath location as the file doing the importing, while
|
||||
`messageSource.xml` and `themeSource.xml` must be in a `resources` location below the
|
||||
`messageSource.xml` must be in a `resources` location below the
|
||||
location of the importing file. As you can see, a leading slash is ignored. However, given
|
||||
that these paths are relative, it is better form not to use the slash at all. The
|
||||
contents of the files being imported, including the top level `<beans/>` element, must
|
||||
|
|
|
@ -96,9 +96,7 @@ Kotlin::
|
|||
The following code snippet is similar to the one we saw earlier for a request-scoped
|
||||
bean. However, this time, the `userService` bean has a dependency on a session-scoped
|
||||
`userPreferences` bean. Note that the `UserPreferences` bean is instantiated by using a
|
||||
SpEL expression that retrieves the theme from the current HTTP session. In our test, we
|
||||
need to configure a theme in the mock session managed by the TestContext framework. The
|
||||
following example shows how to do so:
|
||||
SpEL expression that retrieves an attribute from the current HTTP session.
|
||||
|
||||
.Session-scoped bean configuration
|
||||
[source,xml,indent=0,subs="verbatim,quotes"]
|
||||
|
|
|
@ -11,8 +11,6 @@ The `DispatcherServlet` processes requests as follows:
|
|||
* The locale resolver is bound to the request to let elements in the process
|
||||
resolve the locale to use when processing the request (rendering the view, preparing
|
||||
data, and so on). If you do not need locale resolving, you do not need the locale resolver.
|
||||
* The theme resolver is bound to the request to let elements such as views determine
|
||||
which theme to use. If you do not use themes, you can ignore it.
|
||||
* If you specify a multipart file resolver, the request is inspected for multiparts. If
|
||||
multiparts are found, the request is wrapped in a `MultipartHttpServletRequest` for
|
||||
further processing by other elements in the process. See xref:web/webmvc/mvc-servlet/multipart.adoc[Multipart Resolver] for further
|
||||
|
|
|
@ -14,14 +14,13 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
@file:Suppress("DEPRECATION")
|
||||
package org.springframework.docs.web.webmvc.mvcconfig.mvcconfiginterceptors
|
||||
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
|
||||
import org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor
|
||||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor
|
||||
import org.springframework.web.servlet.theme.ThemeChangeInterceptor
|
||||
|
||||
// tag::snippet[]
|
||||
@Configuration
|
||||
|
@ -29,7 +28,7 @@ class WebConfiguration : WebMvcConfigurer {
|
|||
|
||||
override fun addInterceptors(registry: InterceptorRegistry) {
|
||||
registry.addInterceptor(LocaleChangeInterceptor())
|
||||
registry.addInterceptor(ThemeChangeInterceptor()).addPathPatterns("/**").excludePathPatterns("/admin/**")
|
||||
registry.addInterceptor(UserRoleAuthorizationInterceptor()).addPathPatterns("/**").excludePathPatterns("/admin/**")
|
||||
}
|
||||
}
|
||||
// end::snippet[]
|
|
@ -14,7 +14,9 @@
|
|||
<mvc:interceptor>
|
||||
<mvc:mapping path="/**"/>
|
||||
<mvc:exclude-mapping path="/admin/**"/>
|
||||
<bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"/>
|
||||
<bean class="org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor">
|
||||
<property name="authorizedRoles" value="ROLE_USER"/>
|
||||
</bean>
|
||||
</mvc:interceptor>
|
||||
</mvc:interceptors>
|
||||
<!-- end::snippet[] -->
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
* Copyright 2002-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -31,7 +31,7 @@ import org.springframework.web.method.HandlerMethod;
|
|||
* <p>A HandlerInterceptor gets called before the appropriate HandlerAdapter
|
||||
* triggers the execution of the handler itself. This mechanism can be used
|
||||
* for a large field of preprocessing aspects, or common handler behavior
|
||||
* like locale or theme changes. Its main purpose is to allow for factoring
|
||||
* like locale changes. Its main purpose is to allow for factoring
|
||||
* out repetitive handler code.
|
||||
*
|
||||
* <p>In an asynchronous processing scenario, the handler may be executed in a
|
||||
|
@ -75,7 +75,6 @@ import org.springframework.web.method.HandlerMethod;
|
|||
* @see org.springframework.web.servlet.handler.AbstractHandlerMapping#setInterceptors
|
||||
* @see org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor
|
||||
* @see org.springframework.web.servlet.i18n.LocaleChangeInterceptor
|
||||
* @see org.springframework.web.servlet.theme.ThemeChangeInterceptor
|
||||
* @see jakarta.servlet.Filter
|
||||
*/
|
||||
public interface HandlerInterceptor {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -38,7 +38,7 @@ import org.springframework.web.servlet.ModelAndView;
|
|||
* <h3><a name="workflow">Workflow</a></h3>
|
||||
*
|
||||
* <p>After a {@code DispatcherServlet} has received a request and has
|
||||
* done its work to resolve locales, themes, and suchlike, it then tries
|
||||
* done its work to resolve locales, and suchlike, it then tries
|
||||
* to resolve a Controller, using a
|
||||
* {@link org.springframework.web.servlet.HandlerMapping HandlerMapping}.
|
||||
* When a Controller has been found to handle the request, the
|
||||
|
|
|
@ -54,7 +54,7 @@ import org.springframework.web.util.pattern.PathPatternParser;
|
|||
|
||||
/**
|
||||
* Context holder for request-specific state, like current web application context, current locale,
|
||||
* current theme, and potential binding errors. Provides easy access to localized messages and
|
||||
* and potential binding errors. Provides easy access to localized messages and
|
||||
* Errors instances.
|
||||
*
|
||||
* <p>Suitable for exposition to views, and usage within JSP's "useBean" tag, JSP scriptlets, JSTL EL,
|
||||
|
|
|
@ -46,7 +46,7 @@ import org.springframework.web.util.UriComponentsBuilder;
|
|||
* set by the {@link org.springframework.web.servlet.DispatcherServlet}.
|
||||
*
|
||||
* <p>Supports lookup of current WebApplicationContext, LocaleResolver,
|
||||
* Locale, ThemeResolver, Theme, and MultipartResolver.
|
||||
* Locale, and MultipartResolver.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Rossen Stoyanchev
|
||||
|
|
|
@ -22,7 +22,7 @@ import org.jspecify.annotations.Nullable;
|
|||
|
||||
/**
|
||||
* The {@code <argument>} tag is based on the JSTL {@code fmt:param} tag.
|
||||
* The purpose is to support arguments inside the message and theme tags.
|
||||
* The purpose is to support arguments inside the message tags.
|
||||
*
|
||||
* <p>This tag must be nested under an argument aware tag.
|
||||
*
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -34,8 +34,7 @@ import org.springframework.web.servlet.support.RequestContext;
|
|||
* <p>The {@code RequestContext} instance provides easy access
|
||||
* to current state like the
|
||||
* {@link org.springframework.web.context.WebApplicationContext},
|
||||
* the {@link java.util.Locale}, the
|
||||
* {@link org.springframework.ui.context.Theme}, etc.
|
||||
* the {@link java.util.Locale}, etc.
|
||||
*
|
||||
* <p>Mainly intended for
|
||||
* {@link org.springframework.web.servlet.DispatcherServlet} requests;
|
||||
|
|
|
@ -130,7 +130,7 @@
|
|||
|
||||
<tag>
|
||||
<description>Argument tag based on the JSTL fmt:param tag. The purpose is to
|
||||
support arguments inside the spring:message and spring:theme tags.</description>
|
||||
support arguments inside the spring:message tags.</description>
|
||||
<name>argument</name>
|
||||
<tag-class>org.springframework.web.servlet.tags.ArgumentTag</tag-class>
|
||||
<body-content>JSP</body-content>
|
||||
|
|
|
@ -50,36 +50,6 @@
|
|||
-->
|
||||
<#macro messageArgsText code, args, text>${springMacroRequestContext.getMessage(code, args, text)?no_esc}</#macro>
|
||||
|
||||
<#--
|
||||
* theme
|
||||
*
|
||||
* Macro to translate a theme message code into a message.
|
||||
-->
|
||||
<#macro theme code>${springMacroRequestContext.getThemeMessage(code)?no_esc}</#macro>
|
||||
|
||||
<#--
|
||||
* themeText
|
||||
*
|
||||
* Macro to translate a theme message code into a message,
|
||||
* using the given default text if no message found.
|
||||
-->
|
||||
<#macro themeText code, text>${springMacroRequestContext.getThemeMessage(code, text)?no_esc}</#macro>
|
||||
|
||||
<#--
|
||||
* themeArgs
|
||||
*
|
||||
* Macro to translate a theme message code with arguments into a message.
|
||||
-->
|
||||
<#macro themeArgs code, args>${springMacroRequestContext.getThemeMessage(code, args)?no_esc}</#macro>
|
||||
|
||||
<#--
|
||||
* themeArgsText
|
||||
*
|
||||
* Macro to translate a theme message code with arguments into a message,
|
||||
* using the given default text if no message found.
|
||||
-->
|
||||
<#macro themeArgsText code, args, text>${springMacroRequestContext.getThemeMessage(code, args, text)?no_esc}</#macro>
|
||||
|
||||
<#--
|
||||
* url
|
||||
*
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -40,8 +40,6 @@ public class DummyMacroRequestContext {
|
|||
|
||||
private Map<String, String> messageMap;
|
||||
|
||||
private Map<String, String> themeMessageMap;
|
||||
|
||||
private String contextPath;
|
||||
|
||||
|
||||
|
@ -54,10 +52,6 @@ public class DummyMacroRequestContext {
|
|||
this.messageMap = messageMap;
|
||||
}
|
||||
|
||||
public void setThemeMessageMap(Map<String, String> themeMessageMap) {
|
||||
this.themeMessageMap = themeMessageMap;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @see org.springframework.web.servlet.support.RequestContext#getMessage(String)
|
||||
|
@ -89,36 +83,6 @@ public class DummyMacroRequestContext {
|
|||
return (msg != null ? msg + args : defaultMsg);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.web.servlet.support.RequestContext#getThemeMessage(String)
|
||||
*/
|
||||
public String getThemeMessage(String code) {
|
||||
return this.themeMessageMap.get(code);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.web.servlet.support.RequestContext#getThemeMessage(String, String)
|
||||
*/
|
||||
public String getThemeMessage(String code, String defaultMsg) {
|
||||
String msg = this.themeMessageMap.get(code);
|
||||
return (msg != null ? msg : defaultMsg);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.web.servlet.support.RequestContext#getThemeMessage(String, List)
|
||||
*/
|
||||
public String getThemeMessage(String code, List<?> args) {
|
||||
return this.themeMessageMap.get(code) + args;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.web.servlet.support.RequestContext#getThemeMessage(String, List, String)
|
||||
*/
|
||||
public String getThemeMessage(String code, List<?> args, String defaultMsg) {
|
||||
String msg = this.themeMessageMap.get(code);
|
||||
return (msg != null ? msg + args : defaultMsg);
|
||||
}
|
||||
|
||||
public void setContextPath(String contextPath) {
|
||||
this.contextPath = contextPath;
|
||||
}
|
||||
|
|
|
@ -171,26 +171,6 @@ public class FreeMarkerMacroTests {
|
|||
assertThat(getMacroOutput("MESSAGEARGSWITHDEFAULTMESSAGE")).isEqualTo("Hi");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTheme() throws Exception {
|
||||
assertThat(getMacroOutput("THEME")).isEqualTo("Howdy! Mundo!");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDefaultTheme() throws Exception {
|
||||
assertThat(getMacroOutput("DEFAULTTHEME")).isEqualTo("hi! planet!");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testThemeArgs() throws Exception {
|
||||
assertThat(getMacroOutput("THEMEARGS")).isEqualTo("Howdy![World]");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testThemeArgsWithDefaultMessage() throws Exception {
|
||||
assertThat(getMacroOutput("THEMEARGSWITHDEFAULTMESSAGE")).isEqualTo("Hi!");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUrl() throws Exception {
|
||||
assertThat(getMacroOutput("URL")).isEqualTo("/springtest/aftercontext.html");
|
||||
|
@ -291,10 +271,6 @@ public class FreeMarkerMacroTests {
|
|||
msgMap.put("hello", "Howdy");
|
||||
msgMap.put("world", "Mundo");
|
||||
rc.setMessageMap(msgMap);
|
||||
Map<String, String> themeMsgMap = new HashMap<>();
|
||||
themeMsgMap.put("hello", "Howdy!");
|
||||
themeMsgMap.put("world", "Mundo!");
|
||||
rc.setThemeMessageMap(themeMsgMap);
|
||||
rc.setContextPath("/springtest");
|
||||
|
||||
TestBean darren = new TestBean("Darren", 99);
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
useCodeAsDefaultMessage=false
|
||||
message-file=context-messages
|
||||
objectName=test:service=myservice
|
||||
theme-base=org/springframework/web/context/WEB-INF/
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
theme.example2=test-message2
|
|
@ -1,2 +0,0 @@
|
|||
theme.example1=This is a test message in the theme message catalog with no args.
|
||||
theme.example2=message2
|
|
@ -1 +0,0 @@
|
|||
theme.example1=This is a test message in the theme message catalog.
|
|
@ -21,18 +21,6 @@ MESSAGEARGS
|
|||
MESSAGEARGSWITHDEFAULTMESSAGE
|
||||
<@spring.messageArgsText "no.such.code", msgArgs, "Hi"/>
|
||||
|
||||
THEME
|
||||
<@spring.theme "hello"/> <@spring.theme "world"/>
|
||||
|
||||
DEFAULTTHEME
|
||||
<@spring.themeText "no.such.code", "hi!"/> <@spring.themeText "no.such.code", "planet!"/>
|
||||
|
||||
THEMEARGS
|
||||
<@spring.themeArgs "hello", msgArgs/>
|
||||
|
||||
THEMEARGSWITHDEFAULTMESSAGE
|
||||
<@spring.themeArgsText "no.such.code", msgArgs, "Hi!"/>
|
||||
|
||||
URL
|
||||
<@spring.url "/aftercontext.html"/>
|
||||
|
||||
|
|
Loading…
Reference in New Issue