前臺 JSP 頁面
blogList.jsp 博客展示頁
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form
action="${pageContext.request.contextPath}/sy/freemarkerBlog_list.action"
method="post">
博客標題:<input type="text" name="title"> <input type="submit"
value="確定">
</form>
<br>
<a href="${pageContext.request.contextPath}/blogAdd.jsp">添加</a>
<a id="refresh" href="${pageContext.request.contextPath}/sy/freemarkerBlog_refreshIndex.action">刷新全局索引</a>
<br>
<table border="1" width="100%" cellspacing="0">
<tr>
<td>編號</td>
<td>標題</td>
<td>內容</td>
<td>操作</td>
</tr>
<c:forEach items="${blogList }" var="blog">
<tr>
<td>${blog.bid }</td>
<td>${blog.title }</td>
<!-- 使用網頁靜態化 這裏配置了tomcat 和本地盤符的映射路徑
如果你那邊有問題,請改一下映射路徑
-->
<td><a target="_blank" href="${pageContext.request.contextPath}/blog/${blog.bid }.html">${blog.summary}</a></td>
<td>
<a href="${pageContext.request.contextPath}/sy/freemarkerBlog_perEidt.action?bid=${blog.bid}">修改</a>
<a href="${pageContext.request.contextPath}/sy/freemarkerBlog_del.action?bid=${blog.bid}">刪除</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
blogAdd.jsp 添加博客頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
添加博客:
<form action="${pageContext.request.contextPath}/sy/freemarkerBlog_add.action" method="get">
title:<input name="title"><br/>
summyar:<input name="summary"><br/>
clickHit:<input name="clickHit"><br/>
replyHit:<input name="replyHit"><br/>
content:<input name="content"><br/>
btid:<input name="btid"><br/>
keyWord:<input name="keyWord"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
blogEdit.jsp 修改博客頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
修改博客:
<form action="${pageContext.request.contextPath}/sy/freemarkerBlog_edit.action" method="get">
<input type="hidden" name="bid" value="${map.bid}">
title:<input name="title" value="${map.title}"><br/>
summary:<input name="summary" value="${map.summary}" ><br/>
clickHit:<input name="clickHit" value="${map.clickHit}" ><br/>
replyHit:<input name="replyHit" value="${map.replyHit}"><br/>
content:<input name="content" value="${map.content}"><br/>
btid:<input name="btid" value="${map.btid}"><br/>
keyWord:<input name="keyWord" value="${map.keyWord}"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
freemark 模板
blogDetail.ftl
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Python Django 模板層(AJAX)-javaxl-曉碼閣-博客專欄</title>
<link rel="stylesheet" href="./freemarkerDemo_files/bootstrap.min.css">
<link rel="stylesheet"
href="./freemarkerDemo_files/bootstrap-theme.min.css">
<link rel="stylesheet" href="./freemarkerDemo_files/blog.css">
<script src="./freemarkerDemo_files/jquery-1.11.2.min.js.下載"></script>
<script src="./freemarkerDemo_files/bootstrap.min.js.下載"></script>
<style type="text/css">
body {
padding-top: 10px;
padding-bottom: 40px;
}
</style>
</head>
<body>
<div class="container">
<script type="text/javascript">
function checkData() {
var q = document.getElementById("q").value.trim();
if (q == null || q == "") {
alert("請輸入您要查詢的關鍵字!");
return false;
} else {
return true;
}
}
</script>
<div class="row">
<div class="col-md-12" style="padding-top: 10px">
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed"
data-toggle="collapse"
data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">展開</span> <span class="icon-bar"></span> <span
class="icon-bar"></span> <span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="http://www.javaxl.com/index"><b
style="color: black"><strong>首頁</strong></b></a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse"
id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="http://122.114.92.43/p4/index" target="_blank"><b
color="black"><strong>美食</strong></b></a></li>
<li><a href="http://59.110.212.17/p5/index" target="_blank"><b
color="black"><strong>知識庫</strong></b></a></li>
<li><a href="http://www.javaxl.com/abortMe" target="_blank"><b
color="black"><strong>關於本站</strong></b></a></li>
</ul>
<form action="http://www.javaxl.com/admin/blog/q"
class="navbar-form navbar-right" role="search" method="post"
onsubmit="return checkData()">
<div class="form-group">
<input type="text" id="q" name="q" value="" class="form-control"
placeholder="請輸入要查詢的關鍵字...">
</div>
<button type="submit" class="btn btn-default">搜索</button>
</form>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid --> </nav>
</div>
</div>
<div class="row" style="margin-top: 50px;">
<!--
真正展示博客內容的地方,
所有博客鏈接的列表->list.jsp
單條博客詳情->view.jsp
呼和條件的博客詳情列表->result.jsp
-->
<div class="col-md-9">
<script type="text/javascript"
src="./freemarkerDemo_files/shCore.js.下載"></script>
<link rel="stylesheet"
href="./freemarkerDemo_files/shCoreDefault.css">
<script type="text/javascript">
/*本頁面用於查看博客鏈接中的詳細內容*/
SyntaxHighlighter.all();
function showOtherComment() {
$(".otherComment").show();
}
function loadimage() {
document.getElementById("randImage").src = "/static/system/jsp/common/image.jsp?"
+ Math.random();
}
function submitData() {
var content = $("#content").val();
var imageCode = $("#imageCode").val();
if (content == null || content == "") {
alert("請輸入評論內容!");
} else if (imageCode == null || imageCode == "") {
alert("請填寫驗證碼!");
} else {
$.post("/comment/save.do", {
"content" : content,
'imageCode' : imageCode,
'blog.bid' : '323'
}, function(result) {
if (result.success) {
window.location.reload();
alert("評論已提成成功,審覈通過後顯示!");
} else {
alert(result.errorInfo);
}
}, "json");
}
}
</script>
<div class="data_list">
<div class="data_list_title">
<img
src="./freemarkerDemo_files/blog_show_icon.png" />
博客信息
</div>
<div>
<div class="blog_title">
<h3>
<strong>${blog.title }</strong>
</h3>
</div>
<div class="blog_share">
<!-- <div class="bshare-custom"><a title="分享到QQ空間" class="bshare-qzone"></a><a title="分享到新浪微博" class="bshare-sinaminiblog"></a><a title="分享到人人網" class="bshare-renren"></a><a title="分享到騰訊微博" class="bshare-qqmb"></a><a title="分享到網易微博" class="bshare-neteasemb"></a><a title="更多平臺" class="bshare-more bshare-more-icon more-style-addthis"></a><span class="BSHARE_COUNT bshare-share-count">0</span></div><script type="text/javascript" charset="utf-8" src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=2&lang=zh"></script><script type="text/javascript" charset="utf-8" src="http://static.bshare.cn/b/bshareC0.js"></script> -->
</div>
<div class="blog_info">發佈時間:『${blog.releaseDate?datetime }』 博客類別:${blog.btid } 閱讀(${blog.clickHit })
</div>
<div class="blog_content">${blog.content }</div>
</div>
</div>
</div>
<!-- 右側佈局 -->
<div class="col-md-3">
<div class="data_list">
<div class="data_list_title">博主信息</div>
<div class="user_image">
<img src="./freemarkerDemo_files/index_1.jpg">
</div>
<div class="nickName">javaxl_小李飛刀</div>
<div class="userSign">(知識改變命運,教育改變生活)</div>
</div>
<div class="data_list">
<div class="data_list_title">
<img src="./freemarkerDemo_files/byType_icon.png"> 按日誌類別
</div>
<div class="datas">
<ul>
<li><span><a
href="http://www.javaxl.com/index.html?btid=9">java基礎(71)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?btid=1">自定義mvc(8)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?btid=2">前端框架(39)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?btid=4">索引框架(7)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?btid=5">SSH(14)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?btid=6">SSM(9)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?btid=7">服務器部署(5)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?btid=8">項目異常歸總(5)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?btid=10">安全框架(4)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?btid=11">SpringBoot(15)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?btid=12">程序人生(3)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?btid=13">其他框架(5)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?btid=14">開發工具(6)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?btid=15">解決方案(7)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?btid=16">23種設計模式(25)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?btid=17">SpringCloud(14)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?btid=18">第三方(4)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?btid=19">Python(67)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?btid=20">底層探究(1)</a></span></li>
</ul>
</div>
</div>
<div class="data_list">
<div class="data_list_title">
<img src="./freemarkerDemo_files/byDate_icon.png"> 按日誌日期
</div>
<div class="datas">
<ul>
<li><span><a
href="http://www.javaxl.com/index.html?releaseDateStr=2019%E5%B9%B408%E6%9C%88">2019年08月(36)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?releaseDateStr=2019%E5%B9%B407%E6%9C%88">2019年07月(27)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?releaseDateStr=2019%E5%B9%B406%E6%9C%88">2019年06月(47)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?releaseDateStr=2019%E5%B9%B405%E6%9C%88">2019年05月(61)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?releaseDateStr=2019%E5%B9%B404%E6%9C%88">2019年04月(18)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?releaseDateStr=2019%E5%B9%B403%E6%9C%88">2019年03月(50)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?releaseDateStr=2019%E5%B9%B402%E6%9C%88">2019年02月(57)</a></span></li>
<li><span><a
href="http://www.javaxl.com/index.html?releaseDateStr=2019%E5%B9%B401%E6%9C%88">2019年01月(13)</a></span></li>
</ul>
</div>
</div>
<div class="data_list">
<div class="data_list_title">
<img src="./freemarkerDemo_files/link_icon.png"> 友情鏈接
</div>
<div class="datas">
<ul>
<li><span><a href="http://www.baidu.com/"
target="_blank">百度</a></span></li>
<li><span><a href="http://122.114.92.43/p4/index"
target="_blank">曉碼閣-美食網</a></span></li>
<li><span><a href="http://59.110.212.17/p5/index"
target="_blank">曉碼閣-知識庫</a></span></li>
</ul>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div align="center" style="padding: 20px">
<span style="float: left;">Powered by <a
href="http://www.javaxl.com/" target="_blank">博客首頁</a> V1.0
</span> Copyright © 2018-2019 javaxl曉碼閣 版權所有
</div>
</div>
</div>
</div>
</body>
</html>
後臺代碼
FreemarkerBlogAction.java
package com.cpc.blog.web;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cpc.servlet.http.HttpServletRequest;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Formatter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.Parameter.Request;
import com.javaxl.blog.dao.BlogDao;
import com.javaxl.blog.util.JsonUtils;
import com.javaxl.blog.util.LuceneUtil;
import com.javaxl.blog.util.PropertiesUtil;
import com.javaxl.blog.util.StringUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class FreemarkerBlogAction {
private String title;
private String bid;
private BlogDao blogDao = new BlogDao();
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBid() {
return bid;
}
public void setBid(String bid) {
this.bid = bid;
}
/**
* 查詢
*
* @return
*/
public String list() {
try {
HttpServletRequest request = ServletActionContext.getRequest();
if (StringUtils.isBlank(title)) {
List<Map<String, Object>> blogList = this.blogDao.freemarker_list(title, null);
request.setAttribute("blogList", blogList);
} else {
SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer();
IndexReader indexReader = DirectoryReader
.open(FSDirectory.open(Paths.get(PropertiesUtil.getValue("indexPath"))));
IndexSearcher searcher = new IndexSearcher(indexReader);
// 拿一句話到索引目中的索引文件中的詞庫進行關鍵詞碰撞
Query query = new QueryParser("title", analyzer).parse(title);
TopDocs topDocs = searcher.search(query, 100);
// 將碰撞出來的關鍵詞給點亮
QueryScorer queryScorer = new QueryScorer(query);
// 以什麼形式點亮關鍵詞
Formatter formatter = new SimpleHTMLFormatter("<span style='color:red;'><b>", "</span></b>");
Highlighter highlighter = new Highlighter(formatter, queryScorer);
List<Map<String, Object>> blogList = new ArrayList<>();
Map<String, Object> map = null;
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
map = new HashMap<>();
Document doc = searcher.doc(scoreDoc.doc);
map.put("bid", doc.get("bid"));
map.put("summary", doc.get("summary"));
String titleHighlighter = doc.get("title");
if (StringUtils.isNotBlank(titleHighlighter)) {
titleHighlighter = highlighter.getBestFragment(analyzer, "title", titleHighlighter);
}
map.put("title", titleHighlighter);
blogList.add(map);
}
indexReader.close();
request.setAttribute("blogList", blogList);
}
} catch (Exception e) {
e.printStackTrace();
}
return "blogList";
}
/**
* 這是刷新全局索引調用的方法
*
* @return
*/
public String refreshIndex() {
IndexWriterConfig conf = new IndexWriterConfig(new SmartChineseAnalyzer());
Directory d;
IndexWriter indexWriter = null;
try {
// 先將索引庫中的索引文件清空
delTempChild(new File(PropertiesUtil.getValue("indexPath")));
d = FSDirectory.open(Paths.get(PropertiesUtil.getValue("indexPath")));
indexWriter = new IndexWriter(d, conf);
List<Map<String, Object>> list = blogDao.freemarker_list(null, null);
for (Map<String, Object> map : list) {
Document doc = new Document();
doc.add(new StringField("id", String.valueOf(map.get("bid")), Field.Store.YES));
doc.add(new StringField("bid", String.valueOf(map.get("bid")), Field.Store.YES));
doc.add(new TextField("title", (String) map.get("title"), Field.Store.YES));
doc.add(new TextField("summary", (String) map.get("summary"), Field.Store.YES));
indexWriter.addDocument(doc);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (indexWriter != null) {
indexWriter.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "blogList";
}
public void delTempChild(File file) {
if (file.isDirectory()) {
String[] children = file.list();// 獲取文件夾下所有子文件夾
// 遞歸刪除目錄中的子目錄下
for (int i = 0; i < children.length; i++) {
delTempChild(new File(file, children[i]));
}
}
// 目錄空了,進行刪除
file.delete();
}
/**
* 修改前期
*
* @return
*/
public String perEidt() {
HttpServletRequest request = ServletActionContext.getRequest();
try {
Map<String, Object> map = this.blogDao.getBlogById(bid);
request.setAttribute("map", map);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "blogEdit";
}
/**
* 添加博客
*
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws SecurityException
* @throws NoSuchFieldException
*/
public String add() {
HttpServletRequest request = ServletActionContext.getRequest();
Map parameterMap = request.getParameterMap();
try {
/////////////// 將博客添加到數據庫中 /////////////////////////
this.blogDao.add(parameterMap);
// 獲取當前博客的id
int maxId = this.blogDao.maxId();
// 添加到lucene 索引庫中
addIndex(maxId + "", parameterMap);
// 將這篇博客進行網頁靜態化
addStaticPage(maxId + "", parameterMap);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return "blogList";
}
/**
* 這是刪除博客的方法
*/
public String del() {
try {
// 數據庫中刪除博客
this.blogDao.del(bid);
// 刪除lucene中對應的文檔
IndexWriter indexWriter = getIndexWriter();
indexWriter.deleteDocuments(new Term("id", bid));
indexWriter.forceMergeDeletes(); // 強制刪除
indexWriter.commit();
indexWriter.close();
// 刪除頁面
new File("D:\\code\\code_eclipse\\臨時項目\\javaxl_lunece_freemarker\\src\\main\\webapp\\freemarker\\" + bid
+ ".html").delete();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return "blogList";
}
/**
* 這是修改的方法
*
* @return
*/
public String edit() {
HttpServletRequest request = ServletActionContext.getRequest();
Map parameterMap = request.getParameterMap();
try {
// 修改數據庫中的值
this.blogDao.edit(request.getParameterMap());
// 修改lucene中的文檔值
IndexWriter writer = getIndexWriter();
Document doc = new Document();
doc.add(new StringField("id", JsonUtils.getParamVal(parameterMap, "bid"), Field.Store.YES));
doc.add(new StringField("bid", JsonUtils.getParamVal(parameterMap, "bid"), Field.Store.YES));
doc.add(new TextField("title", JsonUtils.getParamVal(parameterMap, "title"), Field.Store.YES));
doc.add(new TextField("summary", JsonUtils.getParamVal(parameterMap, "summary"), Field.Store.YES));
writer.updateDocument(new Term("id", JsonUtils.getParamVal(parameterMap, "bid")), doc);
writer.close();
// 修改靜態頁(相同id會之間覆蓋)
addStaticPage(JsonUtils.getParamVal(parameterMap, "bid"), parameterMap);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return "blogList";
}
/**
* 獲取寫入對象
*
* @return
* @throws IOException
*/
public IndexWriter getIndexWriter() throws IOException {
IndexWriterConfig conf = new IndexWriterConfig(new SmartChineseAnalyzer());
Directory d = FSDirectory.open(Paths.get(PropertiesUtil.getValue("indexPath")));
return new IndexWriter(d, conf);
}
/**
* 添加索引文件
*
* @param id
* @param parameterMap
* @throws IOException
*/
private void addIndex(String id, Map parameterMap) throws IOException {
IndexWriter indexWriter = getIndexWriter();
Document doc = new Document();
doc.add(new StringField("id", id, Field.Store.YES));
doc.add(new StringField("bid", id, Field.Store.YES));
doc.add(new TextField("title", JsonUtils.getParamVal(parameterMap, "title"), Field.Store.YES));
doc.add(new TextField("summary", JsonUtils.getParamVal(parameterMap, "summary"), Field.Store.YES));
indexWriter.addDocument(doc);
indexWriter.close();
}
/**
* 這是添加靜態頁
*
* @throws IOException
* @throws TemplateException
*/
public void addStaticPage(String id, Map parameterMap) throws IOException, TemplateException {
// 1.創建配置類
Configuration configuration = new Configuration(Configuration.getVersion());
// 2.設置模板所在的目錄
configuration.setDirectoryForTemplateLoading(
new File("D:\\code\\code_eclipse\\臨時項目\\javaxl_lunece_freemarker\\src\\main\\webapp\\freemarker"));
// 3.設置字符集
configuration.setDefaultEncoding("utf-8");
// 4.加載模板 (這是在 剛剛設置好的 目錄下面去找)
Template template = configuration.getTemplate("blogDetail.ftl");
Map map = new HashMap<>();
Map<String, Object> blog = new HashMap<>();
blog.put("bid", id);
blog.put("title", JsonUtils.getParamVal(parameterMap, "title"));
blog.put("releaseDate", new Date());
blog.put("btid", JsonUtils.getParamVal(parameterMap, "btid"));
blog.put("clickHit", JsonUtils.getParamVal(parameterMap, "clickHit"));
blog.put("content", JsonUtils.getParamVal(parameterMap, "content"));
map.put("blog", blog);
// // 6.創建Writer對象
Writer out = new FileWriter(
new File("D:\\code\\code_eclipse\\臨時項目\\javaxl_lunece_freemarker\\src\\main\\webapp\\freemarker\\" + id
+ ".html"));
// 7.輸出
template.process(map, out);
// 8.關閉Writer對象
out.close();
}
}
BlogDao.java
package com.javaxl.blog.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.javaxl.blog.util.JsonBaseDao;
import com.javaxl.blog.util.PageBean;
import com.javaxl.blog.util.StringUtils;
public class BlogDao extends JsonBaseDao{
public List<Map<String,Object>> list(String title, PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql = "select * from t_lucene_crawler_blog where 1=1";
if(StringUtils.isNotBlank(title)) {
sql += " and title like '%"+title+"%'";
}
return super.executeQuery(sql, pageBean);
}
public List<Map<String,Object>> freemarker_list(String title, PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql = "select * from t_lucene_freemarker_blog where 1=1 ";
return super.executeQuery(sql, pageBean);
}
public List<Map<String,Object>> freemarker_show(String bid) throws InstantiationException, IllegalAccessException, SQLException{
String sql = "select * from t_lucene_freemarker_blog where bid = "+bid;
return super.executeQuery(sql, null);
}
/**
* 這是添加博客
* @param paMap
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int add(Map<String,String[]> paMap) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql = "INSERT INTO `t_lucene_freemarker_blog`(`title`, `summary`, `releaseDate`, `content`, `btid`, `keyWord`) VALUES (?, ?, SYSDATE(), ?, ?, ?)";
return super.executeUpdate(sql, new String[] {"title", "summary", "content", "btid", "keyWord"}, paMap);
}
/**
* 這是修改博客
* @param paMap
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int edit(Map<String,String[]> paMap) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql = "update t_lucene_freemarker_blog set title = ?, summary = ?, content = ?, btid=?, keyWord=? where bid = ?";
return super.executeUpdate(sql, new String[] {"title", "summary", "content", "btid", "keyWord", "bid"}, paMap);
}
/**
* 這是刪除博客的方法
* @param bid
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int del(String bid) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql = "delete from t_lucene_freemarker_blog where bid = " + bid;
return super.executeUpdate(sql);
}
/**
* 這是根據id查詢單個的方法 一般用於修改
* @param bid
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public Map<String, Object> getBlogById(String bid) throws InstantiationException, IllegalAccessException, SQLException{
return super.executeQuery("select * from t_lucene_freemarker_blog where bid = " + bid, null).get(0);
}
/**
* 獲取最大id 一般用於添加使用
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public int maxId() throws InstantiationException, IllegalAccessException, SQLException {
return super.executeQueryByInt("select max(bid) from t_lucene_freemarker_blog");
}
}