1、配置类
1.1、创建Mvc 项目之后创建 MyWebApplicationInitializer 类
实现接口 WebApplicationInitializer
public class MyWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
//关联AppConfig配置类
AnnotationConfigWebApplicationContext context =
new AnnotationConfigWebApplicationContext();
context.register(AppConfig.class);
//前端控制器
DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
//添加前端控制器到web容器
ServletRegistration.Dynamic dynamic =
servletContext.addServlet("springmvc", dispatcherServlet);
//设置该servlet启动时机 (web容器启动时,实列化servlet)
dynamic.setLoadOnStartup(1);
//设置servlet的匹配路径
dynamic.addMapping("/");
//添加编码过滤器
servletContext.addFilter("encodingFilter",new CharacterEncodingFilter("UTF-8"));
}
}
1.2、创建 AppConfig 类
@Configuration
@ComponentScan("com.cxd")
public class AppConfig {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver =
new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
这样就可以访问页面了
2、用到的jar包
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.1.11</version>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
</dependency>
</dependencies>
3、常用的jar包以及注解
https://blog.csdn.net/or77iu_N/article/details/141324019?spm=1001.2014.3001.5501
标签:xml,SpringMvc,AppConfig,dynamic,viewResolver,代替,new,servlet,public From: https://blog.csdn.net/or77iu_N/article/details/141429404