SpringBoot基礎系列-整合SpringMVC


原創(chuàng)文章,轉載請標注出處:《SpringBoot基礎系列-整合SpringMVC》


步驟

第一步:添加必要依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

第二步:添加必要的配置

第三步:添加必要的配置類

SpringBoot整合SpringMVC沒有必需的配置類蒲障,只有在想要自定義的時候添加一些實現(xiàn)了WebMvcConfigurer接口的配置類

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    // 添加針對swagger的處理,避免swagger404
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
    }
    //...自定義實現(xiàn)WebMvcConfigurer中的若干默認方法
}

第四步:整合模板引擎

整合Freemarker

1. 添加必要的依賴
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2. 添加必要的設置(重點)
#Freemarker-config
# 設置模板前后綴名
#spring.freemarker.prefix=
spring.freemarker.suffix=.ftl
spring.freemarker.enabled=true
# 設置文檔類型
spring.freemarker.content-type=text/html
spring.freemarker.request-context-attribute=request
# 設置ftl文件路徑
spring.freemarker.template-loader-path=classpath:/templates/
# 設置頁面編碼格式
spring.freemarker.charset=UTF-8
# 設置頁面緩存
spring.freemarker.cache=false
3. 添加必要的配置類

4. 添加控制器和動態(tài)頁面
@Controller
@RequestMapping("base")
@Log4j2
@Api(hidden = true)
public class Base {
    @RequestMapping("/book")
    @ApiOperation(value = "測試",hidden = true)
    public String toBookIndexPage(ModelMap model){
        log.info("進來啦g允堋M扪适揉!");
        model.put("name","浩哥");
        return "/book/index";
    }
}

resources/book/index.ftl

<#assign base = request.contextPath/>
<!DOCTYPE HTML>
<HTML>
<HEAD>
    <TITLE>測試首頁</TITLE>
    <base id="base" href="${base}">
    <link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css" />
    <script src="/webjars/jquery/3.1.1/jquery.min.js"></script>
    <script src="/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js"></script>
</HEAD>
<BODY>
${name}
<a class="getBook" onclick="dianji()">點擊</a><br/>
<button onclick="dianji()">點擊</button>
</BODY>
<SCRIPT>
    function dianji() {
        $.ajax({
            url: "/account/g/1",
            type: "GET",
            success: function (data) {
                alert(data);
            }
        })
    }
    var base = document.getElementById("base").href;
    // 與后臺交互
    _send = function(async,url, value, success, error) {
        $.ajax({
            async : async,
            url : base + '/' + url,
            contentType : "application/x-www-form-urlencoded; charset=utf-8",
            data : value,
            dataType : 'json',
            type : 'post',
            success : function(data) {
                success(data);
            },
            error : function(data) {
                error(data);
            }
        });
    };

</SCRIPT>
</HTML>

整合Thymeleaf

1. 添加必要的jar包
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2. 添加必要的配置
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.enabled=true
spring.thymeleaf.mode=HTML
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.servlet.content-type=text/html

以上配置中除了第一個之外黔牵,其余皆可不配置聪轿,上面的值也是默認值,需要修改的時候再進行配置猾浦。

3. 添加必要配置類

4. 添加控制器和動態(tài)頁面
@Controller
public class BaseController {
    @RequestMapping("index")
    public String toIndex(ModelMap model){
        model.put("name","首頁啊");
        return "index";
    }
}

resources/index.html

<!Doctype html>
<html>
<head>
    <title>下一頁</title>
</head>
<body>
<h1 th:text="${name}">Hello World</h1>
</body>
</html>

整合WebJar

1. 添加必要的jar包
<!--導入bootstrap-->
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>bootstrap</artifactId>
    <version>3.3.7-1</version>
</dependency>
<!--導入Jquery-->
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.1.1</version>
</dependency>
2. 使用WebJar開發(fā)前端頁面
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Dalaoyang</title>
    <link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css" />
    <script src="/webjars/jquery/3.1.1/jquery.min.js"></script>
    <script src="/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container"><br/>
    <div class="alert alert-success">
        <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
        Hello, <strong>Dalaoyang!</strong>
    </div>
</div>
</body>
</html>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末陆错,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子跃巡,更是在濱河造成了極大的恐慌危号,老刑警劉巖牧愁,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件素邪,死亡現(xiàn)場離奇詭異,居然都是意外死亡猪半,警方通過查閱死者的電腦和手機兔朦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來磨确,“玉大人沽甥,你說我怎么就攤上這事》Π拢” “怎么了摆舟?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我恨诱,道長媳瞪,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任照宝,我火速辦了婚禮蛇受,結果婚禮上,老公的妹妹穿的比我還像新娘厕鹃。我一直安慰自己兢仰,他們只是感情好,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布剂碴。 她就那樣靜靜地躺著把将,像睡著了一般。 火紅的嫁衣襯著肌膚如雪汗茄。 梳的紋絲不亂的頭發(fā)上秸弛,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天,我揣著相機與錄音洪碳,去河邊找鬼递览。 笑死,一個胖子當著我的面吹牛瞳腌,可吹牛的內容都是我干的绞铃。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼嫂侍,長吁一口氣:“原來是場噩夢啊……” “哼儿捧!你這毒婦竟也來了?” 一聲冷哼從身側響起挑宠,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤菲盾,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后各淀,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體懒鉴,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年碎浇,在試婚紗的時候發(fā)現(xiàn)自己被綠了临谱。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡奴璃,死狀恐怖悉默,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情苟穆,我是刑警寧澤抄课,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布唱星,位于F島的核電站,受9級特大地震影響跟磨,放射性物質發(fā)生泄漏魏颓。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一吱晒、第九天 我趴在偏房一處隱蔽的房頂上張望甸饱。 院中可真熱鬧,春花似錦仑濒、人聲如沸叹话。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽驼壶。三九已至,卻和暖如春喉酌,著一層夾襖步出監(jiān)牢的瞬間热凹,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工泪电, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留般妙,地道東北人。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓相速,卻偏偏與公主長得像碟渺,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子突诬,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355