Spring boot 如何在我的新Grails3应用程序中使用现有的web.xml?

Spring boot 如何在我的新Grails3应用程序中使用现有的web.xml?,spring-boot,grails,grails3,Spring Boot,Grails,Grails3,根据文件,它说: 如果您有一个修改过的web.xml模板,那么您需要将其迁移到Spring,因为Grails 3.x不使用web.xml(尽管仍然可以在src/main/webapp/web-INF/web.xml中使用on) 我的解释是,如果我合并了一个第三方专有库,它有一个web.xml,那么我可以将它放在src/main/webapp/web-INF中,不做任何更改(以及放在tomcat webapp目录中的所有其他内容),grails将加载它。这个解释正确吗?这个答案似乎暗示了这一点 我

根据文件,它说:

如果您有一个修改过的web.xml模板,那么您需要将其迁移到Spring,因为Grails 3.x不使用web.xml(尽管仍然可以在src/main/webapp/web-INF/web.xml中使用on)

我的解释是,如果我合并了一个第三方专有库,它有一个
web.xml
,那么我可以将它放在
src/main/webapp/web-INF
中,不做任何更改(以及放在tomcat webapp目录中的所有其他内容),grails将加载它。这个解释正确吗?这个答案似乎暗示了这一点

我用
react
配置文件启动了一个Grails3应用程序(我也尝试了
web
配置文件)和一个带有servlet调用的网页。然而,虽然可以在
webapp
中找到一个html文件,但servlet调用本身返回
404
,我不知道为什么。如果我构建一个war文件并部署在一个独立的tomcat上,servlet调用可以工作,但是当我这样运行时:

./gradlew server:bootRun --debug
然后就没有了,我也没有看到任何有趣的东西被打印到控制台上

application.yml
中是否有一些URL映射需要操作

在web.xml中,被调用的servlet如下所示(这是它的一小部分,是吗):


它似乎在起作用。。。尽管如此,如果可能的话,我仍然对只使用
web.xml
的方法感兴趣,这是我最初的问题。

作为从Grails 2.x升级到Grails 3.3的一部分,我以前一直使用web.xml来定义第三方servlet

为了使这些servlet可用(并在启动时加载),我采取的方法是通过一个自定义类。因此,您可以在src/java中定义自定义类,如下所示:

import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ServletRegistrations {

    @Bean
    public ServletRegistrationBean fileServlet(){        
        ServletRegistrationBean registration = new ServletRegistrationBean(new FileServlet(), "/files/*");

        // to load add startup uncomment the line below
        //registration.setLoadOnStartup(1);

        // define init param
        registration.addInitParameter("basePath","/WEB-INF/resources");
        return registration;
    }
}

因此,虽然您不需要在一个XML文件中定义所有内容,但是您仍然可以在这个类中定义所有servlet,所以这不是一个大的变化,我现在已经讨论过一次,我更希望能够在代码中而不是XML中进行定义。希望有帮助

谢谢你的例子,我想我今天要做一些测试。您是否有关于如何将web.xml转换为
Bean
s的文档?听起来,从web.xml迁移可能是一个不错的举措,但我想确认,阻止我的web.xml正常工作的不是一些配置/错误配置……我相信这是Grails内部使用Spring Boot的变化。
import org.springframework.boot.web.servlet.ServletRegistrationBean
// Place your Spring DSL code here
beans = {
    DataSourceLoader(ServletRegistrationBean) { bean ->
         servlet = new com.isomorphic.servlet.DataSourceLoader()
         urlMappings = ['/isomorphic/DataSourceLoader']
    }        
}
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ServletRegistrations {

    @Bean
    public ServletRegistrationBean fileServlet(){        
        ServletRegistrationBean registration = new ServletRegistrationBean(new FileServlet(), "/files/*");

        // to load add startup uncomment the line below
        //registration.setLoadOnStartup(1);

        // define init param
        registration.addInitParameter("basePath","/WEB-INF/resources");
        return registration;
    }
}