概述

旧web项目没有采用maven管理依赖包,jsp页面,xml配置环境,mybatis数据库交互,现在改造为springboot项目,最终打包为war包,java命令直接启动,不依赖tomcat容器

开发环境 idea

###idea新建springboot项目
选择web支持

生成的项目src/main下有java,resource两个文件夹

新建第三个文件夹webapp,和之上两个同级,最终

jsp依赖配置(同时适配本地运行和打包)

注意有些依赖改为<scope>compile</scope>, 参考maven的scope解释(test,runtime,provide,complie)
这样通过idea的application或者打包都不会有问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--用于编译jsp-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>compile</scope>
</dependency>

jsp映射路径

1
2
3
4
5
6
spring:
profiles: dev
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp

serverlet支持

1
2
3
4
5
6
7
8
@SpringBootApplication
public class BootlabbookmanagerApplication extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(BootlabbookmanagerApplication.class);
}
.......
}

###拦截器,资源映射,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@Configuration
@Component
public class WebMVCConfig extends WebMvcConfigurerAdapter
{


@Bean
public HandlerInterceptor getLoginInterceptor(){
return new LoginInterceptor();
}
/**
* * <mvc:mapping path="/**" />
<mvc:exclude-mapping path="/assets/**" />
<mvc:exclude-mapping path="/images/**" />
<mvc:exclude-mapping path="/upload/**" />
<mvc:exclude-mapping path="/login/**" />
<mvc:exclude-mapping path="/register/**" />
<mvc:exclude-mapping path="/forgetPassword/**" />
<mvc:exclude-mapping path="/check/**" />
<mvc:exclude-mapping path="/exists/**" />
<mvc:exclude-mapping path="/code/**" />
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(getLoginInterceptor()).addPathPatterns("/**");
registry.addInterceptor(getLoginInterceptor()).excludePathPatterns("/assets/**","/images/**","/upload/**","/login/**","/register/**","/forgetPassword/**","/check/**","/exists/**","/code/**");

}

/**
* <mvc:resources location="/WEB-INF/assets/" mapping="/assets/**" />
<mvc:resources location="/WEB-INF/images/" mapping="/images/**" />
<mvc:resources location="/upload/" mapping="/upload/**" />
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/WEB-INF/assets/");
registry.addResourceHandler("/images/**").addResourceLocations("classpath:/WEB-INF/images/");
registry.addResourceHandler("/upload/**").addResourceLocations("classpath:/upload");
super.addResourceHandlers(registry);
}

注释对象旧web项目中的设置,注意拦截器之中引用service或其他需要注入的变量时

配置拦截器不能写成以下形式

1
2
3
4
5
6

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor().addPathPatterns("/**");

}

必须要提前注入,否则LoginInterceptor中变量注入失败

1
2
3
4
@Bean
public HandlerInterceptor getLoginInterceptor(){
return new LoginInterceptor();
}

默认error页面

Application中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 @Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {

return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {

// ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/error");
// ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");

container.addErrorPages( error404Page);
}
};
}

全局错误处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@ControllerAdvice
public class GlobalExceptionHandler {


public static final String DEFAULT_ERROR_VIEW = "error";
public static final String DEFAULT_NOT_LOG = "login";
public static final String DEFAULT_INDEX = "index";


@ExceptionHandler(value = UserNotLoginException.class)
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
ModelAndView mav = new ModelAndView();
mav.setViewName(DEFAULT_NOT_LOG);
return mav;
}
}

其他

数据库源,mybatis,redis之类的就很常规了