日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

在Spring中使用Asciidoctor:使用Spring MVC渲染Asciidoc文档

發(fā)布時(shí)間:2023/12/3 javascript 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在Spring中使用Asciidoctor:使用Spring MVC渲染Asciidoc文档 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Asciidoc是一種基于文本的文檔格式,因此如果要將文檔提交到版本控制系統(tǒng)中并跟蹤不同版本之間的更改,它非常有用。 這使Asciidoc成為編寫書籍,技術(shù)文檔,常見問題解答或用戶手冊的理想工具。

創(chuàng)建Asciidoc文檔后,很可能要發(fā)布該文檔,而做到這一點(diǎn)的一種方法是將該文檔發(fā)布到我們的網(wǎng)站上。 今天,我們將學(xué)習(xí)如何使用AsciidoctorJ將Asciidoc文檔轉(zhuǎn)換為HTML,以及如何使用Spring MVC渲染創(chuàng)建HTML。

我們的應(yīng)用程序的要求是:

  • 它必須支持從類路徑中找到的Asciidoc文檔。
  • 它必須支持作為String對象給出的Asciidoc標(biāo)記。
  • 它必須將Asciidoc文檔轉(zhuǎn)換為HTML并呈現(xiàn)創(chuàng)建HTML。
  • 它必須將創(chuàng)建HTML“嵌入”到我們的應(yīng)用程序布局中。

讓我們從獲取Maven所需的依賴關(guān)系開始。

使用Maven獲取所需的依賴關(guān)系

通過執(zhí)行以下步驟,我們可以使用Maven獲得所需的依賴關(guān)系:

  • 啟用Spring IO平臺 。
  • 配置所需的依賴項(xiàng)。
  • 首先 ,我們可以通過將以下代碼段添加到我們的POM文件中來啟用Spring IO平臺:

    <dependencyManagement><dependencies><dependency><groupId>io.spring.platform</groupId><artifactId>platform-bom</artifactId><version>1.0.2.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement>

    其次 ,我們可以按照以下步驟配置所需的依賴項(xiàng):

  • 在pom.xml文件中配置日志記錄依賴性。
  • 將spring-webmvc依賴項(xiàng)添加到pom.xml文件。
  • 將Servlet API依賴項(xiàng)添加到POM文件中。
  • 在POM文件中配置Sitemesh(版本3.0.0)依賴性。 Sitemesh確保我們應(yīng)用程序的每個(gè)頁面使用一致的外觀。
  • 將asciidoctorj依賴性(版本1.5.0)添加到pom.xml文件。 AsciidoctorJ是Asciidoctor的Java API,我們使用它來將Asciidoc文檔轉(zhuǎn)換為HTML。
  • pom.xml文件的相關(guān)部分如下所示:

    <dependencies><!-- Logging --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></dependency><!-- Spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId></dependency><!-- Java EE --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><scope>provided</scope></dependency><!-- Sitemesh --><dependency><groupId>org.sitemesh</groupId><artifactId>sitemesh</artifactId><version>3.0.0</version></dependency><!-- AsciidoctorJ --><dependency><groupId>org.asciidoctor</groupId><artifactId>asciidoctorj</artifactId><version>1.5.0</version></dependency> </dependencies>

    因?yàn)槲覀兪褂肧pring IO Platform,所以不必指定Spring IO Platform中的工件的依賴版本。

    讓我們繼續(xù)并開始實(shí)現(xiàn)我們的應(yīng)用程序。

    使用Spring MVC渲染Asciidoc文檔

    通過執(zhí)行以下步驟,我們可以滿足應(yīng)用程序的要求:

  • 配置我們的Web應(yīng)用程序和Sitemesh過濾器。
  • 實(shí)現(xiàn)視圖類,這些視圖類負(fù)責(zé)將Asciidoc文檔轉(zhuǎn)換為HTML并呈現(xiàn)創(chuàng)建HTML。
  • 實(shí)現(xiàn)使用創(chuàng)建的視圖類的控制器方法。
  • 讓我們開始吧。

    配置Sitemesh

    我們要做的第一件事是配置Sitemesh。 我們可以按照以下三個(gè)步驟配置Sitemesh:

  • 在Web應(yīng)用程序配置中配置Sitemesh過濾器。
  • 創(chuàng)建用于為我們的應(yīng)用程序創(chuàng)建一致外觀的裝飾器。
  • 配置Sitemesh過濾器使用的裝飾器。
  • 首先 ,我們必須在Web應(yīng)用程序配置中配置Sitemesh過濾器。 我們可以按照以下步驟配置Web應(yīng)用程序:

  • 創(chuàng)建一個(gè)實(shí)現(xiàn)WebApplicationInitializer接口的WebAppConfig類。
  • 通過執(zhí)行以下步驟來實(shí)現(xiàn)WebApplicationInitializer接口的onStartup()方法 :
  • 創(chuàng)建一個(gè)AnnotationConfigWebApplicationContext對象并將其配置為處理我們的應(yīng)用程序上下文配置類。
  • 配置調(diào)度程序servlet 。
  • 配置Sitemesh過濾器以處理應(yīng)用程序的JSP頁面和使用url模式'/ asciidoctor / *'的所有控制器方法返回HTML
  • 將新的ContextLoaderListener對象添加到ServletContext中 。 ContextLoaderListener負(fù)責(zé)啟動和關(guān)閉Spring WebApplicationContext 。
  • WebAppConfig類的源代碼如下所示(突出顯示了Sitemesh配置):

    import org.sitemesh.config.ConfigurableSiteMeshFilter; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet;import javax.servlet.DispatcherType; import javax.servlet.FilterRegistration; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; import java.util.EnumSet;public class WebAppConfig implements WebApplicationInitializer {private static final String DISPATCHER_SERVLET_NAME = "dispatcher";private static final String SITEMESH3_FILTER_NAME = "sitemesh";private static final String[] SITEMESH3_FILTER_URL_PATTERNS = {"*.jsp", "/asciidoctor/*"};@Overridepublic void onStartup(ServletContext servletContext) throws ServletException {AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();rootContext.register(WebAppContext.class);configureDispatcherServlet(servletContext, rootContext);configureSitemesh3Filter(servletContext);servletContext.addListener(new ContextLoaderListener(rootContext));}private void configureDispatcherServlet(ServletContext servletContext, WebApplicationContext rootContext) {ServletRegistration.Dynamic dispatcher = servletContext.addServlet(DISPATCHER_SERVLET_NAME,new DispatcherServlet(rootContext));dispatcher.setLoadOnStartup(1);dispatcher.addMapping("/");}private void configureSitemesh3Filter(ServletContext servletContext) {FilterRegistration.Dynamic sitemesh = servletContext.addFilter(SITEMESH3_FILTER_NAME, new ConfigurableSiteMeshFilter());EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);sitemesh.addMappingForUrlPatterns(dispatcherTypes, true, SITEMESH3_FILTER_URL_PATTERNS);} }
    • 如果要看一下示例應(yīng)用程序的應(yīng)用程序上下文配置類,可以從Github中獲得它 。

    其次 ,我們必須創(chuàng)建為我們的應(yīng)用程序提供一致外觀的裝飾器。 我們可以按照以下步驟進(jìn)行操作:

  • 將裝飾器文件創(chuàng)建到src / main / webapp / WEB-INF目錄。 我們的示例應(yīng)用程序的裝飾器文件稱為layout.jsp 。
  • 將提供一致外觀HTML添加到創(chuàng)建的裝飾器文件中。
  • 確保Sitemesh將從返回HTML中找到的標(biāo)題添加到Web瀏覽器呈現(xiàn)HTML中。
  • 配置Sitemesh,將從返回HTML的開頭找到HTML元素添加到呈現(xiàn)HTML的開頭。
  • 確保Sitemesh將在返回HTML中找到的正文添加到顯示給用戶HTML中。
  • 裝飾器文件( layout.jsp )的源代碼如下所示(與Sitemesh相關(guān)的部分已突出顯示):

    <!doctype html> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title><sitemesh:write property="title"/></title><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" type="text/css" href="${contextPath}/static/css/bootstrap.css"/><link rel="stylesheet" type="text/css" href="${contextPath}/static/css/bootstrap-theme.css"/><script type="text/javascript" src="${contextPath}/static/js/jquery-2.1.1.js"></script><script type="text/javascript" src="${contextPath}/static/js/bootstrap.js"></script><sitemesh:write property="head"/> </head> <body> <nav class="navbar navbar-inverse" role="navigation"><div class="container-fluid"><!-- Brand and toggle get grouped for better mobile display --><div class="navbar-header"><button type="button" class="navbar-toggle" data-toggle="collapse"data-target="#bs-example-navbar-collapse-1"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button></div><div class="collapse navbar-collapse"><ul class="nav navbar-nav"><li><a href="${contextPath}/">Document list</a></li></ul></div></div> </nav> <div class="container-fluid"><sitemesh:write property="body"/> </div> </body> </html>

    第三 ,我們必須配置Sitemesh以使用在第二步中創(chuàng)建的裝飾器文件。 我們可以按照以下步驟進(jìn)行操作:

  • 在src / main / webapp / WEB-INF目錄中創(chuàng)建一個(gè)sitemesh3.xml文件。
  • 將Sitemesh配置為使用我們的裝飾器處理Sitemesh過濾器處理的所有請求。
  • sitemesh3.xml文件如下所示:

    <sitemesh><mapping path="/*" decorator="/WEB-INF/layout/layout.jsp"/> </sitemesh>

    這就對了。 現(xiàn)在,我們已經(jīng)配置了Sitemesh,以為我們的應(yīng)用程序提供一致的外觀。 讓我們繼續(xù)前進(jìn),了解如何實(shí)現(xiàn)將Asciidoc標(biāo)記轉(zhuǎn)換為HTML并呈現(xiàn)所創(chuàng)建HTML的視圖類。

    實(shí)施視圖類

    在開始實(shí)現(xiàn)將Asciidoc標(biāo)記轉(zhuǎn)換為HTML并呈現(xiàn)創(chuàng)建HTML的視圖類之前,我們必須快速了解一下我們的需求。 與該步驟相關(guān)的要求是:

    • 我們的解決方案必須支持從類路徑中找到的Asciidoc文檔。
    • 我們的解決方案必須支持作為String對象給出的Asciidoc標(biāo)記。
    • 我們的解決方案必須將Asciidoc文檔轉(zhuǎn)換為HTML并呈現(xiàn)創(chuàng)建HTML。

    這些要求建議我們應(yīng)該創(chuàng)建三個(gè)視圖類。 這些視圖類如下所述:

    • 我們應(yīng)該創(chuàng)建一個(gè)抽象基類,該基類包含將Asciidoc標(biāo)記轉(zhuǎn)換為HTML并呈現(xiàn)創(chuàng)建HTML的邏輯。
    • 我們應(yīng)該創(chuàng)建一個(gè)視圖類,該類可以從類路徑中找到的文件中讀取Asciidoc標(biāo)記。
    • 我們應(yīng)該創(chuàng)建一個(gè)可以從String對象讀取Asciidoc標(biāo)記的視圖類。

    換句話說,我們必須創(chuàng)建以下類結(jié)構(gòu):

    首先 ,我們必須實(shí)現(xiàn)AbstractAsciidoctorHtmlView類。 此類是一個(gè)抽象基類,可將Asciidoc標(biāo)記轉(zhuǎn)換為HTML并呈現(xiàn)創(chuàng)建HTML。 我們可以按照以下步驟實(shí)現(xiàn)此類:

  • 創(chuàng)建AbstractAsciidoctorHtmlView類并擴(kuò)展AbstractView類。
  • 在創(chuàng)建的類中添加一個(gè)構(gòu)造函數(shù),并將視圖的內(nèi)容類型設(shè)置為“ text / html”。
  • 將一個(gè)受保護(hù)的抽象方法getAsciidocMarkupReader()添加到創(chuàng)建的類,并將其返回類型設(shè)置為Reader 。 此抽象類的子類必須實(shí)現(xiàn)此方法,并且此方法的實(shí)現(xiàn)必須返回一個(gè)Reader對象,該對象可用于讀取呈現(xiàn)的Asciidoc標(biāo)記。
  • 將私有的getAsciidoctorOptions()方法添加到創(chuàng)建的類中,并通過返回Asciidoctor的配置選項(xiàng)來實(shí)現(xiàn)它。
  • 重寫AbstractView類的renderMergedOutputModel()方法 ,并通過將Asciidoc文檔轉(zhuǎn)換為HTML并呈現(xiàn)創(chuàng)建HTML來實(shí)現(xiàn)它。
  • AbstractAsciidoctorHtmlView類的源代碼如下所示:

    import org.asciidoctor.Asciidoctor; import org.asciidoctor.Options; import org.springframework.http.MediaType; import org.springframework.web.servlet.view.AbstractView;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.Reader; import java.io.Writer; import java.util.Map;public abstract class AbstractAsciidoctorHtmlView extends AbstractView {public AbstractAsciidoctorHtmlView() {super.setContentType(MediaType.TEXT_HTML_VALUE);}protected abstract Reader getAsciidocMarkupReader();@Overrideprotected void renderMergedOutputModel(Map<String, Object> model,HttpServletRequest request,HttpServletResponse response) throws Exception {//Set the content type of the response to 'text/html'response.setContentType(super.getContentType());Asciidoctor asciidoctor = Asciidoctor.Factory.create();Options asciidoctorOptions = getAsciidoctorOptions();try (//Get the reader that reads the rendered Asciidoc document//and the writer that writes the HTML markup to the request bodyReader asciidoctorMarkupReader = getAsciidocMarkupReader();Writer responseWriter = response.getWriter();) {//Transform Asciidoc markup into HTML and write the created HTML //to the response bodyasciidoctor.render(asciidoctorMarkupReader, responseWriter, asciidoctorOptions);}}private Options getAsciidoctorOptions() {Options asciiDoctorOptions = new Options();//Ensure that Asciidoctor includes both the header and the footer of the Asciidoc //document when it is transformed into HTML.asciiDoctorOptions.setHeaderFooter(true);return asciiDoctorOptions;} }

    其次 ,我們必須實(shí)現(xiàn)ClasspathFileAsciidoctorHtmlView類。 此類可以從類路徑中找到的文件中讀取Asciidoc標(biāo)記。 我們可以按照以下步驟實(shí)現(xiàn)此類:

  • 創(chuàng)建ClasspathFileAsciidoctorHtmlView類,并擴(kuò)展AbstractAsciidoctorHtmlView類。
  • 在創(chuàng)建的類中添加一個(gè)名為asciidocFileLocation的私有String字段。 此字段包含轉(zhuǎn)換為HTML的Asciidoc文件的位置。 必須以Class類的getResourceAsStream()方法可以理解的格式指定此位置。
  • 創(chuàng)建一個(gè)構(gòu)造器,該構(gòu)造器將呈現(xiàn)的Asciidoc文件的位置作為構(gòu)造器參數(shù)。 通過調(diào)用超類的構(gòu)造函數(shù)并將呈現(xiàn)的Asciidoc文件的位置存儲到asciidocFileLocation字段中來實(shí)現(xiàn)構(gòu)造函數(shù) 。
  • 重寫getAsciidocMarkupReader()方法并通過返回一個(gè)新的InputStreamReader對象來實(shí)現(xiàn)它,該對象用于讀取從類路徑中找到的Asciidoc文件。
  • ClasspathFileAsciidoctorHtmlView類的源代碼如下所示:

    import java.io.InputStreamReader; import java.io.Reader;public class ClasspathFileAsciidoctorHtmlView extends AbstractAsciidoctorHtmlView {private final String asciidocFileLocation;public ClasspathFileAsciidoctorHtmlView(String asciidocFileLocation) {super();this.asciidocFileLocation = asciidocFileLocation;}@Overrideprotected Reader getAsciidocMarkupReader() {return new InputStreamReader(this.getClass().getResourceAsStream(asciidocFileLocation));} }

    第三 ,我們必須實(shí)現(xiàn)StringAsciidoctorHtmlView類,該類可以從String對象讀取Asciidoc標(biāo)記。 我們可以按照以下步驟實(shí)現(xiàn)此類:

  • 創(chuàng)建StringAsciidoctorHtmlView類,并擴(kuò)展AbstractAsciidoctorHtmlView類。
  • 在創(chuàng)建的類中添加一個(gè)名為asciidocMarkup的私有String字段。 此字段包含已轉(zhuǎn)換為HTML的Asciidoc標(biāo)記。
  • 創(chuàng)建一個(gè)將呈現(xiàn)的Asciidoc標(biāo)記用作構(gòu)造函數(shù)參數(shù)的構(gòu)造函數(shù)。 通過調(diào)用超類的構(gòu)造函數(shù)并將呈現(xiàn)的Asciidoc標(biāo)記設(shè)置為asciidocMarkup字段來實(shí)現(xiàn)此構(gòu)造函數(shù)。
  • 重寫getAsciidocMarkupReader()方法,并通過返回一個(gè)新的StringReader對象來實(shí)現(xiàn)它,該對象用于讀取存儲在asciidocMarkup字段中的Asciidoc標(biāo)記。
  • StringAsciidoctorHtmlView的源代碼如下所示:

    import java.io.Reader; import java.io.StringReader;public class StringAsciidoctorHtmlView extends AbstractAsciidoctorHtmlView {private final String asciidocMarkup;public StringAsciidoctorHtmlView(String asciidocMarkup) {super();this.asciidocMarkup = asciidocMarkup;}@Overrideprotected Reader getAsciidocMarkupReader() {return new StringReader(asciidocMarkup);} }

    現(xiàn)在,我們已經(jīng)創(chuàng)建了所需的視圖類。 讓我們繼續(xù)研究如何在Spring MVC Web應(yīng)用程序中使用這些類。

    使用創(chuàng)建的視圖類

    我們的最后一步是創(chuàng)建使用創(chuàng)建的視圖類的控制器方法。

    我們必須實(shí)現(xiàn)以下描述的兩種控制器方法:

    • renderAsciidocDocument()方法處理發(fā)送到url'/ asciidoctor / document'的GET請求,然后將Asciidoc文檔轉(zhuǎn)換為HTML并呈現(xiàn)創(chuàng)建HTML。
    • renderAsciidocString()方法處理發(fā)送到url'/ asciidoctor / string'的GET獲取請求,然后將Asciidoc 字符串轉(zhuǎn)換為HTML并呈現(xiàn)創(chuàng)建HTML。

    AsciidoctorController類的源代碼如下所示:

    import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView;@Controller public class AsciidoctorController {private static final String ASCIIDOC_FILE_LOCATION = "/asciidoctor/document.adoc";private static final String ASCIIDOC_STRING = "= Hello, AsciiDoc (String)!\n" +"Doc Writer <doc@example.com>\n" +"\n" +"An introduction to http://asciidoc.org[AsciiDoc].\n" +"\n" +"== First Section\n" +"\n" +"* item 1\n" +"* item 2\n" +"\n" +"1\n" +"puts \"Hello, World!\"";@RequestMapping(value = "/asciidoctor/document", method = RequestMethod.GET)public ModelAndView renderAsciidocDocument() {//Create the view that transforms an Asciidoc document into HTML and//renders the created HTML.ClasspathFileAsciidoctorHtmlView docView = new ClasspathFileAsciidoctorHtmlView(ASCIIDOC_FILE_LOCATION);return new ModelAndView(docView);}@RequestMapping(value = "/asciidoctor/string", method = RequestMethod.GET)public ModelAndView renderAsciidocString() {//Create the view that transforms an Asciidoc String into HTML and//renders the created HTML.StringAsciidoctorHtmlView stringView = new StringAsciidoctorHtmlView(ASCIIDOC_STRING);return new ModelAndView(stringView);} }

    附加信息:

    • @Controller批注的Javadoc
    • @RequestMapping注釋的Javadoc
    • ModelAndView類的Javadoc

    現(xiàn)在,我們已經(jīng)創(chuàng)建了使用我們的視圖類的控制器方法。 當(dāng)我們的應(yīng)用程序的用戶調(diào)用url'/ asciidoctor / document'的GET請求時(shí),呈現(xiàn)HTML頁面的源代碼如下所示(由Asciidoctor創(chuàng)建的部分被突出顯示):

    <!doctype html><html> <head><title>Hello, AsciiDoc (File)!</title><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" type="text/css" href="/static/css/bootstrap.css"/><link rel="stylesheet" type="text/css" href="/static/css/bootstrap-theme.css"/><script type="text/javascript" src="/static/js/jquery-2.1.1.js"></script><script type="text/javascript" src="/static/js/bootstrap.js"></script><meta charset="UTF-8"> <!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]--> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="generator" content="Asciidoctor 1.5.0"> <meta name="author" content="Doc Writer"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic|Noto+Serif:400,400italic,700,700italic|Droid+Sans+Mono:400"> <link rel="stylesheet" href="./asciidoctor.css"></head> <body> <nav class="navbar navbar-inverse" role="navigation"><div class="container-fluid"><!-- Brand and toggle get grouped for better mobile display --><div class="navbar-header"><button type="button" class="navbar-toggle" data-toggle="collapse"data-target="#bs-example-navbar-collapse-1"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button></div><div class="collapse navbar-collapse"><ul class="nav navbar-nav"><li><a href="/">Document list</a></li></ul></div></div> </nav> <div class="container-fluid"><div id="header"> <h1>Hello, AsciiDoc (File)!</h1> <div class="details"> <span id="author" class="author">Doc Writer</span><br> <span id="email" class="email"><a href="mailto:doc@example.com">doc@example.com</a></span><br> </div> </div> <div id="content"> <div id="preamble"> <div class="sectionbody"> <div class="paragraph"> <p>An introduction to <a href="http://asciidoc.org">AsciiDoc</a>.</p> </div> </div> </div> <div class="sect1"> <h2 id="_first_section">First Section</h2> <div class="sectionbody"> <div class="ulist"> <ul> <li> <p>item 1</p> </li> <li> <p>item 2</p> </li> </ul> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-ruby" data-lang="ruby">puts "Hello, World!"</code></pre> </div> </div> </div> </div> </div> <div id="footer"> <div id="footer-text"> Last updated 2014-09-21 14:21:59 EEST </div> </div></div> </body> </html>

    如我們所見,由Asciidoctor創(chuàng)建HTML嵌入到我們的布局中,這為我們的應(yīng)用程序用戶提供了一致的用戶體驗(yàn)。

    讓我們繼續(xù)并評估此解決方案的優(yōu)缺點(diǎn)。

    利弊

    我們解決方案的優(yōu)點(diǎn)是:

    • 呈現(xiàn)HTML文檔與我們應(yīng)用程序的其他頁面具有相同的外觀。 這意味著我們可以為應(yīng)用程序的用戶提供一致的用戶體驗(yàn)。
    • 我們可以呈現(xiàn)靜態(tài)文件和可以從數(shù)據(jù)庫加載的字符串。

    我們解決方案的缺點(diǎn)是:

    • 我們的簡單應(yīng)用程序的war文件很大(51.9 MB)。 原因是,即使Asciidoctor具有Java API,它也是用Ruby編寫的。 因此,我們的應(yīng)用程序需要兩個(gè)大的jar文件:
      • asciidoctorj-1.5.0.jar文件的大小為27.5MB。
    • 當(dāng)用戶請求時(shí),我們的應(yīng)用程序?qū)sciidoc文檔轉(zhuǎn)換為HTML。 這對我們的控制器方法的響應(yīng)時(shí)間有負(fù)面影響,因?yàn)槲臋n越大,處理該文檔所花費(fèi)的時(shí)間就越長。
    • 將Asciidoc文檔呈現(xiàn)為HTML的第一個(gè)請求比下一個(gè)請求慢4-5倍。 我沒有剖析該應(yīng)用程序,但我認(rèn)為JRuby與此有關(guān)。
    • 目前,如果我們要將Asciidoc文檔轉(zhuǎn)換為PDF文檔,則無法使用此技術(shù)。

    讓我們繼續(xù)并總結(jié)從這篇博客文章中學(xué)到的知識。

    摘要

    這篇博客文章教會了我們?nèi)?#xff1a;

    • 我們了解了如何配置Sitemesh,以為我們的應(yīng)用程序提供一致的外觀。
    • 我們學(xué)習(xí)了如何創(chuàng)建將Asciidoc文檔轉(zhuǎn)換為HTML并呈現(xiàn)所創(chuàng)建HTML的視圖類。
    • 即使我們的解決方案有效,它也有很多缺點(diǎn),可能使其在現(xiàn)實(shí)生活中無法使用。

    本教程的下一部分描述了如何解決此解決方案的性能問題。

    PS:如果您想體驗(yàn)本博客文章的示例應(yīng)用程序,可以從Github獲得 。

    翻譯自: https://www.javacodegeeks.com/2014/10/using-asciidoctor-with-spring-rendering-asciidoc-documents-with-spring-mvc.html

    總結(jié)

    以上是生活随笔為你收集整理的在Spring中使用Asciidoctor:使用Spring MVC渲染Asciidoc文档的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。