diff --git a/grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersAutoConfiguration.java b/grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersAutoConfiguration.java index 8e6519111c..9a1ffe3abf 100644 --- a/grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersAutoConfiguration.java +++ b/grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersAutoConfiguration.java @@ -1,23 +1,28 @@ package org.grails.plugins.web.controllers; import grails.config.Settings; +import grails.core.GrailsApplication; import jakarta.servlet.DispatcherType; import jakarta.servlet.Filter; import jakarta.servlet.MultipartConfigElement; import org.grails.web.config.http.GrailsFilters; import org.grails.web.filters.HiddenHttpMethodFilter; +import org.grails.web.servlet.mvc.GrailsDispatcherServlet; import org.grails.web.servlet.mvc.GrailsWebRequestFilter; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean; import org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.filter.OrderedCharacterEncodingFilter; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; +import org.springframework.util.ClassUtils; import org.springframework.web.filter.CharacterEncodingFilter; +import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -54,6 +59,9 @@ public class ControllersAutoConfiguration { @Value("${" + Settings.CONTROLLERS_UPLOAD_FILE_SIZE_THRESHOLD + ":0}") private int fileSizeThreshold; + @Value("${" + Settings.WEB_SERVLET_PATH + ":#{null}}") + String grailsServletPath; + @Bean @ConditionalOnMissingBean(CharacterEncodingFilter.class) public CharacterEncodingFilter characterEncodingFilter() { @@ -100,6 +108,24 @@ public MultipartConfigElement multipartConfigElement() { return new MultipartConfigElement(uploadTmpDir, maxFileSize, maxRequestSize, fileSizeThreshold); } + @Bean + public DispatcherServlet dispatcherServlet() { + return new GrailsDispatcherServlet(); + } + + @Bean + public DispatcherServletRegistrationBean dispatcherServletRegistration(GrailsApplication application, DispatcherServlet dispatcherServlet, MultipartConfigElement multipartConfigElement) { + if (grailsServletPath == null) { + boolean isTomcat = ClassUtils.isPresent("org.apache.catalina.startup.Tomcat", application.getClassLoader()); + grailsServletPath = isTomcat ? Settings.DEFAULT_TOMCAT_SERVLET_PATH : Settings.DEFAULT_WEB_SERVLET_PATH; + } + DispatcherServletRegistrationBean dispatcherServletRegistration = new DispatcherServletRegistrationBean(dispatcherServlet, grailsServletPath); + dispatcherServletRegistration.setLoadOnStartup(2); + dispatcherServletRegistration.setAsyncSupported(true); + dispatcherServletRegistration.setMultipartConfig(multipartConfigElement); + return dispatcherServletRegistration; + } + @Bean public GrailsWebMvcConfigurer webMvcConfig() { return new GrailsWebMvcConfigurer(resourcesCachePeriod, resourcesEnabled, resourcesPattern); diff --git a/grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersGrailsPlugin.groovy b/grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersGrailsPlugin.groovy index e73928df14..75bb581b33 100644 --- a/grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersGrailsPlugin.groovy +++ b/grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersGrailsPlugin.groovy @@ -23,16 +23,12 @@ import groovy.util.logging.Slf4j import org.grails.core.artefact.ControllerArtefactHandler import org.grails.plugins.web.servlet.context.BootStrapClassRunner import org.grails.web.errors.GrailsExceptionResolver -import org.grails.web.servlet.mvc.GrailsDispatcherServlet import org.grails.web.servlet.mvc.TokenResponseActionResultTransformer import org.grails.web.servlet.view.CompositeViewResolver import org.springframework.beans.factory.support.AbstractBeanDefinition -import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean import org.springframework.context.ApplicationContext -import org.springframework.util.ClassUtils import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping -import jakarta.servlet.MultipartConfigElement /** * Handles the configuration of controllers for Grails. @@ -57,8 +53,6 @@ class ControllersGrailsPlugin extends Plugin { def config = application.config boolean useJsessionId = config.getProperty(Settings.GRAILS_VIEWS_ENABLE_JSESSIONID, Boolean, false) - boolean isTomcat = ClassUtils.isPresent("org.apache.catalina.startup.Tomcat", application.classLoader) - String grailsServletPath = config.getProperty(Settings.WEB_SERVLET_PATH, isTomcat ? Settings.DEFAULT_TOMCAT_SERVLET_PATH : Settings.DEFAULT_WEB_SERVLET_PATH) if (!Boolean.parseBoolean(System.getProperty(Settings.SETTING_SKIP_BOOTSTRAP))) { bootStrapClassRunner(BootStrapClassRunner) @@ -80,14 +74,6 @@ class ControllersGrailsPlugin extends Plugin { annotationHandlerMapping(RequestMappingHandlerMapping, interceptorsClosure) annotationHandlerAdapter(RequestMappingHandlerAdapter) - // add the dispatcher servlet - dispatcherServlet(GrailsDispatcherServlet) - dispatcherServletRegistration(DispatcherServletRegistrationBean, ref("dispatcherServlet"), grailsServletPath) { - loadOnStartup = 2 - asyncSupported = true - multipartConfig = ref('multipartConfigElement') - } - for (controller in application.getArtefacts(ControllerArtefactHandler.TYPE)) { log.debug "Configuring controller $controller.fullName" if (controller.available) {