31
31
import org .apache .commons .logging .Log ;
32
32
import org .apache .commons .logging .LogFactory ;
33
33
import org .springframework .util .Assert ;
34
+ import org .springframework .util .ObjectUtils ;
34
35
35
36
/**
36
37
* A {@link ServletContextInitializer} to register {@link Servlet}s in a Servlet 3.0+
40
41
* <p>
41
42
* The {@link #setServlet(Servlet) servlet} must be specified before calling
42
43
* {@link #onStartup}. URL mapping can be configured used {@link #setUrlMappings} or
43
- * omitted when mapping to '/*'. The servlet name will be deduced if not specified.
44
+ * omitted when mapping to '/*' (unless
45
+ * {@link #ServletRegistrationBean(Servlet, boolean, String...) alwaysMapUrl} is set to
46
+ * {@code false}). The servlet name will be deduced if not specified.
44
47
*
45
48
* @author Phillip Webb
46
49
* @see ServletContextInitializer
@@ -56,6 +59,8 @@ public class ServletRegistrationBean extends RegistrationBean {
56
59
57
60
private Set <String > urlMappings = new LinkedHashSet <String >();
58
61
62
+ private boolean alwaysMapUrl = true ;
63
+
59
64
private int loadOnStartup = -1 ;
60
65
61
66
private MultipartConfigElement multipartConfig ;
@@ -73,9 +78,22 @@ public ServletRegistrationBean() {
73
78
* @param urlMappings the URLs being mapped
74
79
*/
75
80
public ServletRegistrationBean (Servlet servlet , String ... urlMappings ) {
81
+ this (servlet , true , urlMappings );
82
+ }
83
+
84
+ /**
85
+ * Create a new {@link ServletRegistrationBean} instance with the specified
86
+ * {@link Servlet} and URL mappings.
87
+ * @param servlet the servlet being mapped
88
+ * @param alwaysMapUrl if omitted URL mappings should be replaced with '/*'
89
+ * @param urlMappings the URLs being mapped
90
+ */
91
+ public ServletRegistrationBean (Servlet servlet , boolean alwaysMapUrl ,
92
+ String ... urlMappings ) {
76
93
Assert .notNull (servlet , "Servlet must not be null" );
77
94
Assert .notNull (urlMappings , "UrlMappings must not be null" );
78
95
this .servlet = servlet ;
96
+ this .alwaysMapUrl = alwaysMapUrl ;
79
97
this .urlMappings .addAll (Arrays .asList (urlMappings ));
80
98
}
81
99
@@ -164,7 +182,7 @@ public void onStartup(ServletContext servletContext) throws ServletException {
164
182
Assert .notNull (this .servlet , "Servlet must not be null" );
165
183
String name = getServletName ();
166
184
if (!isEnabled ()) {
167
- logger .info ("Filter " + name + " was not registered (disabled)" );
185
+ logger .info ("Servlet " + name + " was not registered (disabled)" );
168
186
return ;
169
187
}
170
188
logger .info ("Mapping servlet: '" + name + "' to " + this .urlMappings );
@@ -186,10 +204,12 @@ protected void configure(ServletRegistration.Dynamic registration) {
186
204
super .configure (registration );
187
205
String [] urlMapping = this .urlMappings
188
206
.toArray (new String [this .urlMappings .size ()]);
189
- if (urlMapping .length == 0 ) {
207
+ if (urlMapping .length == 0 && this . alwaysMapUrl ) {
190
208
urlMapping = DEFAULT_MAPPINGS ;
191
209
}
192
- registration .addMapping (urlMapping );
210
+ if (!ObjectUtils .isEmpty (urlMapping )) {
211
+ registration .addMapping (urlMapping );
212
+ }
193
213
registration .setLoadOnStartup (this .loadOnStartup );
194
214
if (this .multipartConfig != null ) {
195
215
registration .setMultipartConfig (this .multipartConfig );
0 commit comments