idea+springboot+freemarker+devtools - java - 方帮信
当前位置: 首页 >  > 技术 > 软件开发 > java

idea+springboot+freemarker+devtools

2020/7/1 0:16:33 浏览

一整套技术栈,四个元素包括:idea、springboot、freemarker、devtools。idea:开发工具springboot:开发框架freemarker:模板引擎devtools:热部署组件在使用spring boot创建mvc项目的时候,自然需要明确的是 m、v和c。那么在view层如何使用controller和template中的模板…

一整套技术栈,四个元素包括:idea、springboot、freemarker、devtools。

idea:开发工具

springboot:开发框架

freemarker:模板引擎

devtools:热部署组件


在使用spring boot创建mvc项目的时候,自然需要明确的是 m、v和c。

那么在view层如何使用controller和template中的模板关联上呢?


controller:

package com.cyd.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/test")
public class CJFXController {
   @RequestMapping("/index")
   public ModelAndView index(ModelAndView mv) {
       mv.setViewName("index");
       return mv;
   }
}


view层级关系:

resource
template
 index.html


紧接着需要解决的关键点是在index.html中对于css、js、以及图片等文件的引入,需要使用基本路径如下图这样:


image-20200630225253570.png


配置拦截器:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.Arrays;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Autowired
PathInterceptor pathInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {  registry.addInterceptor(pathInterceptor).addPathPatterns("/**");
}
}


PathInterceptor

@Component
public class PathInterceptor implements HandlerInterceptor {
   @Override
   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
           throws Exception {
       String path = request.getContextPath();
       String scheme = request.getScheme();
       String serverName = request.getServerName();
       int port = request.getServerPort();
       String basePath = scheme + "://" + serverName + ":" + port + path;
       request.setAttribute("basePath", basePath);//通过这种方式的拦截器的配置,才能够在html中使用basePath,否则就会导致模板报错,无法正常进行
       return true;
   }
   @Override
   public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                          ModelAndView modelAndView) throws Exception {
   }
   @Override
   public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
           throws Exception {
   }


上面是从controller到view以及view中的静态文件引入的几个关键点。


但是如果你开始着手做这个流程的时候会发现,每次修改后都需要手动重启调试,这样过于浪费时间。那么我们就想办法配置自动运行的热部署环境。


①配置pom.xml

 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <!-- optional=true,依赖不会往下传递,如果有项目依赖本项目,并且想要使用devtools,需要重新引入 -->
   <optional>true</optional>
<scope>runtime</scope>
</dependency>

②配置自动编译项目

File->Settings->Build,Execution,Deployment->Complier->Build project automatically

image-20200630230245648.png


③关键是registry的设置,有的同学找不到,下面告诉你这个东西它在哪儿?


image-20200630102839107.png


image-20200630103021486.png


转自:https://mp.weixin.qq.com/s/n_IyUYsHppYHcPselsA1ZA


来源:问问计算机(微信:wenwenjisuanji,邮箱:changyandou@126.com),欢迎分享!