spring boot 渲染md格式模板

写在前面

主要技术点:使用thymeleaf+flexmark 渲染模板渲染markdown文件 ,highlight渲染代码高亮
thymeleaf 模板负责渲染页面
flexmark是java语言编写,负责将数据库中的markdown语法文本转为html文本
highlight负责渲染code部分的代码,是一款js+css库

首先定义一个controller 和 requstmapping

package com.kikt.web.ctl.blog;

import com.kikt.service.markdown.MarkdownService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;

/**
 * Created by cai on 2017/8/25.
 */
@Controller
@RequestMapping("/md")
public class MarkdownCtl {

    @Autowired
    private MarkdownService service;

    @RequestMapping(value = "/parse", method = RequestMethod.POST)
    public String parseMarkDown(Model model, @RequestParam("md") String markdownText) {
        model.addAttribute("md", service.parseMarkdownString(markdownText));
        return "test";
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        model.addAttribute("inputName", "md");
        return "markdown";
    }

}

在static/templates下定义一个文件,markdown.html

这样当访问 http://localhost:8080/md/index的时候,就会跳转到index页面中




    
    
    
    
    Title