1、添加依赖:pom.xml
<!--freemarker依赖,模板引擎--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
2、配置文件:application.properties
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mysql-boot
spring.datasource.username=root
spring.datasource.password=123123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.freemarker.suffix=.ftl
spring.freemarker.templateEncoding=UTF-8
spring.freemarker.templateLoaderPath=classpath:/templates/
spring.freemarker.content-type=text/html
spring.freemarker.request-context-attribute=request
3、controller
@RequestMapping("getStudents/{name}") public String getStudents(ModelMap map, @PathVariable String name){ ..... return "views/student"; }
4、ftl文件
<!--获取项目路径springMacroRequestContext或${request.contextPath}--> <#assign path= springMacroRequestContext.getContextPath()/> <script src="${path}/js/jquery.js" type="text/javascript"></script> 或 <script src="${request.contextPath}/js/jquery.js" type="text/javascript"></script>