小谈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;
    }

注意:
1.application-dev.yml访问路径配置

server:
  servlet:
    context-path: "/"

因为前端静态资源的访问一般采用绝对定位,个人对相对定位不是很喜欢:
eg:

<img src="/images/login-img.png"/>

那么问题来了我们如何解决这个问题呢?
1.采用绝对路径(http://127.0.0.1:8081/test/js/test.js)
2.修改路径,将href="…/static/xxx" 改成href=“static/xxx”
3.使用spring thymeleaf的th:src或者th:href属性改变标签的链接路径(如上)
4.

<link rel="stylesheet"  href="../static/pace/themes/blue/pace-theme-flash.css"  th:href="@{/pace/themes/blue/pace-theme-flash.css}">
<base href="XXX/">
<link rel="stylesheet"  href="../static/pace/themes/blue/pace-theme-flash.css">

希望以上代码对大家有所帮助,如果有错误或者代码不详细的大家可以再评论区留言。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值