Spring 5在WildFly Server 20中的正确配置

huangapple go评论76阅读模式
英文:

Spring 5 correct configuration in WildFly Server 20

问题

我正在一个新的简单项目中使用 Spring 5WildFly 20,我通过注解以编程方式配置了 WebApplicationInitializer 接口的实现和一个 WebConfig 类,但当我运行项目时,我的Web浏览器显示 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 只是一个包含 "Hello world" 的简单 index.jsp

英文:

I'm workin in a new simple project with Spring 5 and WildFly 20, I configured programmatically (via annotations) the implementation of WebApplicationInitializer interface and a WebConfig class, and when I run the project it shows: 404 - Not Found in my web browser. Anyone can help me please?, What am I doing wrong?. This is my code:

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;
    }
}

I changed the servlet.addMapping("/*"); from "/" to "/*" but it still doesn't working. My JSP is just a simple index.jsp with "Helo world".

答案1

得分: 0

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

英文:

If you use spring-boot, check jasper and jstl in the list of dependencies.

&lt;dependency&gt;
    &lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
    &lt;artifactId&gt;tomcat-embed-jasper&lt;/artifactId&gt;
    &lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
    
&lt;dependency&gt;
    &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
    &lt;artifactId&gt;jstl&lt;/artifactId&gt;
&lt;/dependency&gt;

And using this view class to enable explicit JSTL support.

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

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

答案2

得分: 0

在几天后,我意识到WildFly与Tomcat的工作方式不同(我知道这是显而易见的事情),在Eclipse中,Tomcat会将项目名称分配给上下文应用程序,类似于:localhost:8080/<project-name>,而WildFly会从pom.xml文件中分配<artifactId><version>,类似于:localhost:8080/<artifactId><version>

我之前没有注意到这一点,因为当我右键单击项目,然后选择“以服务器方式运行”时,Eclipse会自动打开一个嵌入式Web浏览器,地址类似于localhost:8080/<project-name>

解决方法是在我的pom.xml文件中添加标签<warName>springapp</warName>,就这样。我知道这很简单,但我之前没有注意到。

英文:

After a few days, I realized that WildFly doesn't work like Tomcat (I know it's something obvious), in Eclipse Tomcat assign the project name to the Context Application, something like: localhost:8080/&lt;project-name&gt; while WildFly assign the &lt;artifactId&gt; and &lt;version&gt;from pom.xml file, something like: localhost:8080/&lt;artifactId&gt;&lt;version&gt;.

I couldn't see that because when a ran the project by right click on project > Run As > Run on Server, automatically Eclipse opens an embebed web explorer like localhost:8080/&lt;project-name&gt;.

The solution was add the tag &lt;warName&gt;springapp&lt;/warName&gt; in my pom.xml file and that's all. I know that is something easy but I had not noticed.

huangapple
  • 本文由 发表于 2020年8月14日 07:17:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63404442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定