【问题标题】:Spring 5 correct configuration in WildFly Server 20WildFly Server 20 中的 Spring 5 正确配置
【发布时间】:2020-12-03 20:24:22
【问题描述】:

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

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

【问题讨论】:

    标签: java spring model-view-controller wildfly


    【解决方案1】:

    如果你使用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>
    

    并使用此视图类来启用显式 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/

    【讨论】:

    • 不,我使用的是 Spring 5 和 WildFly 而不是 Spring Boot。
    • 检查 contextPath,在服务器上运行您的项目。
    • 我认为你错过了一个非常小的。观看教程并再次缓慢设置。 :)
    • 感谢@0gam 但它仍然无法正常工作,我找到了解决方案,可能我会发布它。 :)
    【解决方案2】:

    几天后,我意识到 WildFly 不像 Tomcat 那样工作我知道这很明显),在 Eclipse Tomcat 中将项目名称分配给 Context应用程序,例如:localhost:8080/&lt;project-name&gt;,而 WildFly 分配 &lt;artifactId&gt;&lt;version&gt;from pom.xml 文件,例如:localhost:8080/&lt;artifactId&gt;&lt;version&gt;

    我看不到,因为当通过右键单击项目 > 运行方式 > 在服务器上运行自动运行项目时Eclipse 会打开一个嵌入式 Web 浏览器,例如 localhost:8080/&lt;project-name&gt;

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

    【讨论】:

      猜你喜欢
      • 2018-08-07
      • 2021-01-30
      • 2011-08-05
      • 1970-01-01
      • 2015-08-09
      • 1970-01-01
      • 2018-03-22
      • 2021-04-12
      • 2018-07-28
      相关资源
      最近更新 更多