1. 改造成maven项目
2. 增加pom依赖
统一Springboot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
</parent>
增加对SpringBoot的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
增加对jsp的支持
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>7.0.59</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
增加对静态资源的过滤
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/WebContent</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
3. 增加主启动类
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).run(args); }
}
4. 注册Servlet、Filter、Listener
若项目通过@WebServlet、@WebFilter、@WebListener注册Servlet、Filter、Listener
则可以通过在主启动类上增加注解@ServletComponentScan,自动注册Servlet、Filter、Listener
否则则需要通过spring的ServletRegistrationBean注册,这个bean是由springboot提供专门来注册servlet的,可以像注册bean一样去配置servlet。
@Configuration
public class ServletRegist {
/**
* 1、通过@Bean注解注册实例
* 2、返回类型为ServletRegistrationBean
* 3、new KaptchaServlet() 第三方依赖包的对象,对应web.xml 中的<servlet-class>
* 4、设置相关拦截器参数initParams,对应web.xml 中的<init-param>
* 5、必须指定一个别名,如果不指定则会覆盖springboot提供的DispatcherServlet,对应web.xml 中的<servlet-name>
* 5、设置映射的url;setUrlMappings,多个url的话可以放到list,对应web.xml中的<url-pattern>
* 6、setLoadOnStartup 设置启动级别,对应web.xml 中的<load-on-startup>
* @return
*/
@Bean
public ServletRegistrationBean kaptchaServletBean(){
ServletRegistrationBean bean = new ServletRegistrationBean(new KaptchaServlet());
Map<String,String> initParams = new HashMap<>();
initParams.put("kaptcha.textproducer.font.names","Arial");
bean.setInitParameters(initParams);
bean.setName("Kaptcha");
ArrayList urlList = new ArrayList();
urlList.add("/Kaptcha");
bean.setUrlMappings(urlList);
bean.setLoadOnStartup(1);
return bean;
}
}
注册Filter
通过FilterRegistrationBean 注册
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new MyFilter());
registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
return registrationBean;
}
注册Listener
通过ServletListenerRegistrationBean 注册
@Bean
public ServletListenerRegistrationBean myListener(){
ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
return registrationBean;
}
5. 根目录映射index.jsp
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
/**
* 拦截某个请求跳转固定位置
*
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index.jsp");
// registry.addViewController("/index").setViewName("index.jsp");
}
}
Comments | NOTHING