英文:
Spring 5 correct configuration in WildFly Server 20
问题
我正在一个新的简单项目中使用 Spring 5 和 WildFly 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.
<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>
And using this view class to enable explicit JSTL support.
@Bean
public ViewResolver internalResourceViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/view/");
bean.setSuffix(".jsp");
return bean;
}
答案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/<project-name>
while WildFly assign the <artifactId>
and <version>
from pom.xml
file, something like: localhost:8080/<artifactId><version>
.
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/<project-name>
.
The solution was add the tag <warName>springapp</warName>
in my pom.xml
file and that's all. I know that is something easy but I had not noticed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论