一、案例
1.1 pom.xml
<dependencies>
<!-- 除去logback支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<span style="color: #0000ff"><</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>org.springframework.boot<span style="color: #0000ff"></</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>spring-boot-starter-test<span style="color: #0000ff"></</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">scope</span><span style="color: #0000ff">></span>test<span style="color: #0000ff"></</span><span style="color: #800000">scope</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> 使用log4j2,该包paren标签中已经依赖 </span><span style="color: #008000">--></span>
<span style="color: #0000ff"><</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>org.springframework.boot<span style="color: #0000ff"></</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>spring-boot-starter-log4j2<span style="color: #0000ff"></</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>org.springframework.boot<span style="color: #0000ff"></</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>spring-boot-devtools<span style="color: #0000ff"></</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> 引入freemarker包 </span><span style="color: #008000">--></span>
<span style="color: #0000ff"><</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>org.springframework.boot<span style="color: #0000ff"></</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>spring-boot-starter-freemarker<span style="color: #0000ff"></</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></</span><span style="color: #800000">dependencies</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">build</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">plugins</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">plugin</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>org.springframework.boot<span style="color: #0000ff"></</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>spring-boot-maven-plugin<span style="color: #0000ff"></</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></</span><span style="color: #800000">plugin</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></</span><span style="color: #800000">plugins</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></</span><span style="color: #800000">build</span><span style="color: #0000ff">></span></pre>
1.2 application.properties
#主配置文件,配置了这个会优先读取里面的属性覆盖主配置文件的属性
spring.profiles.active=dev
server.port=8888
logging.config=classpath:log4j2-dev.xml
1.3 编写控制器
package com.shyroke.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value = “/index”)
public class IndexController {
@RequestMapping(value </span>= "/login"<span style="color: #000000">)
</span><span style="color: #0000ff">public</span><span style="color: #000000"> ModelAndView index(ModelAndView modelAndView) {
modelAndView.setViewName(</span>"index"<span style="color: #000000">);
List</span><String> userList=<span style="color: #0000ff">new</span> ArrayList<String><span style="color: #000000">();
userList.add(</span>"admin"<span style="color: #000000">);
userList.add(</span>"user1"<span style="color: #000000">);
userList.add(</span>"user2"<span style="color: #000000">);
modelAndView.addObject(</span>"userList"<span style="color: #000000">, userList);
</span><span style="color: #0000ff">return</span><span style="color: #000000"> modelAndView;
}
}
1.5 编写index.ftl
<!DOCTYPE html>
<html>
<head lang="en">
<title>Spring Boot Demo - FreeMarker</title>
<link href="/css/index.css" rel="stylesheet">
<script type="text/javascript" src="/jars/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="/js/index.js"></script>
</head>
<body>
<h2>首页<h2>
<span style="color: #0000ff"><</span><span style="color: #800000">font</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">#list </span><span style="color: #ff0000">userList as item</span><span style="color: #0000ff">></span><span style="color: #000000">
${item!}</span><span style="color: #0000ff"><</span><span style="color: #800000">br </span><span style="color: #0000ff">/></span>
<span style="color: #0000ff"></</span><span style="color: #800000">#list</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></</span><span style="color: #800000">font</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">button </span><span style="color: #ff0000">class</span><span style="color: #0000ff">="a"</span><span style="color: #0000ff">></span> click me<span style="color: #0000ff"></</span><span style="color: #800000">button</span><span style="color: #0000ff">></span>
</body>
</html>
1.6 index.js
$(function() {
$('.a').click(function() {
alert('hello world');
});
})
1.7 index.css
1.8 目录结构