Spring 5 WildFly Server 20 中的正确配置

Spring 5 correct configuration in WildFly Server 20

我正在使用 Spring 5WildFly 20 进行一个新的简单项目,我以编程方式配置(通过注解) WebApplicationInitializer接口的实现和WebConfigclass,当我运行它显示的项目时:404 - 在我的网络浏览器中找不到 。任何人都可以帮助我吗?,我做错了什么?。这是我的代码:

WebApplicationInitializer :

public class WebAppInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext sctx) throws ServletException {
        AnnotationConfigWebApplicationContext contexto = new AnnotationConfigWebApplicationContext();

        contexto.register(WebConfig.class);
        contexto.setServletContext(sctx);

        ServletRegistration.Dynamic servlet = sctx.addServlet("dispatcherServlet", new 
        DispatcherServlet(contexto));

        servlet.setLoadOnStartup(1);
        servlet.addMapping("/*");
    }
}

WebConfig:

@Configuration
@EnableWebMvc
@ComponentScan({ "com.test.config" })
public class WebConfig {

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".jsp");

        return resolver;
    }
}

我将 servlet.addMapping("/*");"/" 更改为 "/*",但它仍然无法正常工作。我的 JSP 只是一个带有“Helo world”的简单 index.jsp

如果你使用spring-boot,检查依赖项列表中的jasper和jstl。

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>
    
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

并使用此视图 class 启用显式 JSTL 支持。

@Bean
public ViewResolver internalResourceViewResolver() {
    InternalResourceViewResolver bean = new InternalResourceViewResolver();
    bean.setViewClass(JstlView.class);
    bean.setPrefix("/WEB-INF/view/");
    bean.setSuffix(".jsp");
    return bean;
}

https://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/

几天后,我意识到 WildFly 不像 Tomcat 那样工作(我知道这很明显 ), 在 Eclipse Tomcat 中将项目名称分配给上下文应用程序,例如:localhost:8080/<project-name> 而 WildFly 从 pom.xml 文件中分配 <artifactId><version>,诸如此类喜欢:localhost:8080/<artifactId><version>.

我看不到,因为当 运行 通过 右键单击​​项目 > 运行 As > 运行 在 Server 上,Eclipse 会自动打开一个嵌入的 Web 浏览器,如 localhost:8080/<project-name>.

解决方案是在我的 pom.xml 文件中添加标签 <warName>springapp</warName>,仅此而已。我知道这很容易,但我没有注意到。