小谈springboot web开发
作者:互联网
背景
从出道以来两年半许,记得从在学校学习的JSP,再到前后端分离,再到现在公司自己想的的html混合开发。从技术栈角度分析:JSP:用servlet或者SSH以及SSM构建项目。前后端分离:后端—SSM 前端—VUE。现在自己构建的一个考试系统架构:后端----SSM 前端—js(请求用axios)。
接下来从编译的角度简单分析一下:JSP-----实际编译为servlet文件执行,可直接在页面写Java代码,可直接在服务器上修改生效。前后端分离-----前端通过HTTP调用接口,一般采用token验证。好处是可拓展性以及并发性等较优,各人职责明确,各思所长。HTML----后端以及数据绑定甚至页面设计都由后台人员完成。html浏览器可直接读取,如果再采用异步访问接口,那么效果上跟前端分离差不多。
废话不多说博主直接码代码
springboot+thymeleaf对比springboot+springmvc
springboot+thymeleaf
pom.xml
<!-- springboot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!--排除默认的tomcat-jdbc-->
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
application.yml
thymeleaf:
prefix: classpath:/static/
suffix: .html
check-template-location: true
encoding: utf-8
servlet:
content-type: text/html
cache: false
注意:classpath:/static/—此处必须要classpath:/定位
如果涉及过滤以及静态资源加载:
@Configuration
public class SysInterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration interceptorRegistration = registry.addInterceptor(new SessionHandlerInterceptor());
interceptorRegistration.excludePathPatterns("/**");
interceptorRegistration.excludePathPatterns("/test/*","/css/**","/js/**","/images/**","/plugins/**");
interceptorRegistration.addPathPatterns("/**");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
}
private class SessionHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
String auth = request.getHeader("Authorization");
System.out.println("auth:"+auth);
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
//controller方法处理完毕后,调用此方法
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
//页面渲染完毕后调用此方法
}
}
}
用上述代码写前端需要注意:
1.必须在html头添加:
<html xmlns:th="http://www.thymeleaf.org">
2.css,js,image的引用:
<link th:href="@{/css/login.css}" rel="stylesheet" type="text/css"/>
3.需要采用同步加载才能使用thymeleaf的很多标签
springboot+springmvc
A方案:
pom.xml 只需要springboot的web包即可
application.yml
spring:
mvc:
view:
prefix: /static/
suffix: .html
注意:prefix后面的路径没有classpath:/(博主踩坑过)
B方案:
在springboot的启动类中添加代码:
@Bean
protected InternalResourceViewResolver addViewControllers() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/static/");
resolver.setSuffix(".html");
return resolver;
}
希望以上代码对大家有所帮助,如果有错误或者代码不详细的大家可以再评论区留言。
标签:web,springboot,小谈,classpath,html,static,registry,public 来源: https://blog.csdn.net/fly_west/article/details/97618540