一键静态化【一键生成静态网页】
项目首页 HTML页 需要全屏轮播展示,要求通过管理平台能够选择展示的图片。开始时使用 Ajax 获取数据库表中图片地址展示,结果只能轮播一次无法循环轮播,最后通过一键静态化实现此功能
一,案例展示
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 5.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>模板页</title> <script src="../Scripts/jquery-1.9.1.min.js"></script> <script src="../Scripts/fullplay.js"></script> <script type="text/javascript"> var widthint = 1920; var heightint = 980; $(document).ready(function (e) { $("#bodyPage").fullImages({ ImgWidth: widthint, ImgHeight: heightint, autoplay: 3500, //自动播放 fadeTime: 1500 //褪色 时间 }); }); </script> <style type="text/css"> html { margin: 0; padding: 0; } body { margin: 0; padding: 0; } #bodyPage { width: 100%; min-height: 100%; height: auto !important; height: 100%; position: fixed; top: 0; left: 0; } img.bg { position: absolute; top: 0px; left: 0px; z-index: 1; display: none; height: 100%; width: 100%; } footer { position: fixed; bottom: 0px; height: 10%; width: 100%; z-index: 9999; } footer marquee { font: 2.5vw "华文行楷"; color: #f00; } </style> </head> <body> <section id="bodyBox"> <div id="bodyPage"> {$imgList$} </div> </section> <footer> <article> <marquee scrollamount="10"> XXXXXXX 系统 </marquee> </article> </footer> </body> </html> <script type="text/javascript"> $("body").click(function () { window.location.href = "NavigationInfo.html"; }); </script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 5.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>模板页</title> <script src="../Scripts/jquery-1.9.1.min.js"></script> <script src="../Scripts/fullplay.js"></script> <script type="text/javascript"> var widthint = 1920; var heightint = 980; $(document).ready(function (e) { $("#bodyPage").fullImages({ ImgWidth: widthint, ImgHeight: heightint, autoplay: 3500, //自动播放 fadeTime: 1500 //褪色 时间 }); }); </script> <style type="text/css"> html { margin: 0; padding: 0; } body { margin: 0; padding: 0; } #bodyPage { width: 100%; min-height: 100%; height: auto !important; height: 100%; position: fixed; top: 0; left: 0; } img.bg { position: absolute; top: 0px; left: 0px; z-index: 1; display: none; height: 100%; width: 100%; } footer { position: fixed; bottom: 0px; height: 10%; width: 100%; z-index: 9999; } footer marquee { font: 2.5vw "华文行楷"; color: #f00; } </style> </head> <body> <section id="bodyBox"> <div id="bodyPage"> <img class="bg" src="/UpLoadFiles/20180307/2018030716220622.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716223467.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716224437.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716225115.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716225737.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716230370.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716230949.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716221741.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716222305.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716222856.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716211971.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716234059.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716235859.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716240816.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716250642.png" /><img class="bg" src="/UpLoadFiles/20180307/2018030716341533.png" /> </div> </section> <footer> <article> <marquee scrollamount="10"> XXXXXXX 系统</marquee> </article> </footer> </body> </html> <script type="text/javascript"> $("body").click(function () { window.location.href = "NavigationInfo.html"; }); </script>
使用 .aspx 作为网站的后台管理
<asp:Button ID="show" runat="server" Text="生成展示页" />
//一键静态化 void show_Click(object sender, EventArgs e) { //替换掉模板中的特征字符 \..\showcasing\Webpages\ScreenInfo_Temp.html string mbPath = Server.MapPath("../showcasing/Webpages/ScreenInfo_Temp.html"); Encoding code = Encoding.GetEncoding("UTF-8"); StreamReader sr = null; StreamWriter sw = null; string str = null; //字符串为空 //读取 try { sr = new StreamReader(mbPath, code); str = sr.ReadToEnd(); //注意:这里是‘tempstr’而不是‘str’,不要混了 } catch (Exception ex) { throw ex; } finally { sr.Close(); } string fileName = "../showcasing/Webpages/ScreenInfo.html"; string imglistStr = GetListStr(); str = str.Replace("{$imgList$}", imglistStr); //生成静态文件 try { sw = new StreamWriter(Server.MapPath("~/Webpages/") + fileName, false, code); sw.Write(str); sw.Flush(); } catch (Exception ex) { throw ex; } finally { sw.Close(); JscriptPrint("生产成功啦!", "ShowimagesList.aspx?" + CombUrlTxt(this.channelId, this.keywords) + "page=0", "Success"); } }
private String GetListStr() { string listStr = string.Empty; ShowImagesBLL ShowImagesBLL = new ShowImagesBLL(); ShowImagesEntity ShowImagesEntity = new ShowImagesEntity(); DataTable dt = ShowImagesBLL.GetList("Islook>0 order by Islook asc"); foreach (DataRow dr in dt.Rows) { ShowImagesEntity = new ShowImagesEntity(dr); listStr += "<img class=\"bg\" src=\"" + ShowImagesEntity.ImageURL + "\" />"; } return listStr; }
二、实例说明 《转》
一,先建立一个模版页
为了适应实际应用功能,一个网站往往需要最终用户自定义静态化的模版,本文对这方面不作过多描述,主要只是增删改查功能,此章仅作抛砖引玉用!!!
前端界面如下
aspx.cs页面代码也只简单实现对模版的保存
- /// <summary>
- /// Saves the data.
- /// </summary>
- /// <param name="id">The id.</param>
- private void SaveData(int id)
- {
- Model.C_Template model = GetData(id);
- if (model != null)
- {
- long x = 0;
- if (id <= 0)
- {
- x = model.id = _BLL.Add(model);
- }
- else
- {
- x = _BLL.Update(model) ? 1 : 0;
- }
- //other code
- //show message
- if (x > 0)
- {
- MessageBox.Show(string.Format(_jsonResult, "true", "保存成功", _returnUrl, id.ToString()), MessageEnum.Error);
- }
- else
- {
- MessageBox.Show(string.Format(_jsonResult, "false", "保存失败,请稍候再试...", "", id.ToString()), MessageEnum.Error);
- }
- }
- else
- {
- //有错误
- MessageBox.Show(string.Format(_jsonResult, "false", _pageMsg, "", id.ToString()), MessageEnum.Error);
- }
- }
二,根据对应模版,静态出一个include页面
ok,好吧,其实我这里取巧了,先跳过,后面总结里再详说。为了使本例简单,现在先假设模版页已经生成了html格式。代码如下
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>{$title} - {$classcName} - <!--#include file="/include/common/siteTitle.html" --></title>
- <meta name="description" content="{$seoDescription},<!--#include file="/include/common/siteDescription.html" -->" />
- <meta name="keywords" content="{$seoKeyWords},<!--#include file="/include/common/siteKeyWords.html" -->" />
- <link href="/css/layout.css" rel="stylesheet" type="text/css" />
- <link href="/css/layout_sub.css" rel="stylesheet" type="text/css" />
- <script src="/js/jquery/jquery.js" type="text/javascript"></script>
- <script src="/js/jquery/myJS.js" type="text/javascript"></script>
- <!--[if lte IE 6]>
- <script type="text/javascript" src="/js/ie6png.js"></script>
- <![endif]-->
- <script type="text/javascript">
- $(function () {
- $(".hotSchool .tabs").tabs(".hotSchool div.contentC", { event: 'click', effect: 'fade' });
- });
- </script>
- </head>
- <body>
- <div id="bgImgWrap">
- <img src="/images/back1.jpg" alt="" />
- </div>
- <!--#include file="/include/common/picStory.html" -->
- <div class="bigWrap">
- <div class="sideWrapBg png"></div>
- <div class="sideWrap png">
- <div class="logo noTxt clearfix"></div>
- <!--#include file="/include/common/sideNav.html" -->
- </div>
- <div class="mainWrap png">
- <!-- top search /-->
- <!--#include file="/include/common/head.html" -->
- <!-- 频道页顶部导航 /-->
- <div class="cWrap subTop clearfix">
- <div class="left subTitle">{$channelname}</div>
- <div class="left subNav">
- <!--{$subNav}-->
- </div>
- </div>
- <!-- sub wrap -->
- <div class="cWrap subWrap pie png clearfix">
- <!-- 文章详细 /-->
- <div class="detailWrap newsListWrap newsDetailWrap clearfix">
- <div class="subNavWrap clearfix">
- <div class="subNav left">{$locationget}</div>
- </div>
- <h1>{$title}</h1>
- <div class="infoWrap clearfix">
- <div class="shareWrap">
- <div id="bdshare" class="bdshare_t bds_tools get-codes-bdshare">
- <span class="left">分享本页:</span>
- <a class="bds_qzone"></a>
- <a class="bds_tsina"></a>
- <a class="bds_tqq"></a>
- <a class="bds_renren"></a>
- <a class="bds_douban"></a>
- <span class="bds_more"></span>
- </div>
- </div>
- <div class="dateWrap">发表:{$addDate} </div>
- <div class="readWrap">阅读:<span id="viewNums">...</span></div>
- </div>
- <div class="clearfix"></div>
- <div class="subHR clearfix png"></div>
- <!-- Intro /-->
- <div class="introWrap clearfix">
- {$intro}
- </div>
- <!-- 正文 /-->
- <div class="txtWrap clearfix minArticle">
- {$content}
- </div>
- <!-- 相关文章 /-->
- <div>
- </div>
- </div>
- </div>
- <!--/ sub wrap -->
- <!-- footer /-->
- <!--#include file="/include/common/foot.html" -->
- </div>
- </div>
- </body>
- </html>
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="include.aspx.cs" validateRequest="false" Inherits="Admin_CMS_include" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>文章输入页面</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- 文章标题:<asp:TextBox ID="txtTitle" runat="server" Height="26px" Width="350px"></asp:TextBox><br />
- 文章摘要:<asp:TextBox ID="txtIntro" runat="server" Height="26px" Width="350px"></asp:TextBox><br />
- 文章内容:<asp:TextBox ID="txtContent" runat="server" Height="179px" TextMode="MultiLine" Width="350px"></asp:TextBox><br />
- <br />
- <asp:Button ID="btnInclude" runat="server" OnClick="btnInclude_Click" Text="生成静态页" />
- </div>
- </form>
- </body>
- </html>
- using System.IO;
- using System.Text;
- protected void btnInclude_Click(object sender, EventArgs e)
- {
- //other code;
- //Get date into dataSource;
- string mbPath = Server.MapPath("~/Web/Template/CS1.html");
- Encoding code = Encoding.GetEncoding("UTF-8");
- StreamReader sr = null;
- StreamWriter sw = null;
- string str = null;
- try
- {
- sr = new StreamReader(mbPath, code);
- str = sr.ReadToEnd();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- sr.Close();
- }
- string fileName = DateTime.Now.ToString("MMddHHmm") + ".html";//保存静态页面的扩展名
- //整章的关键点,替换掉相应字符串
- str = str.Replace("{$Title}", txtTitle.Text);
- str = str.Replace("{$intro}", txtIntro.Text);
- str = str.Replace("{$content}", txtContent.Text);
- //生成静态文件
- try
- {
- sw = new StreamWriter(Server.MapPath("~/html/") + fileName, false, code);
- sw.Write(str);
- sw.Flush();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- sw.Close();
- Response.Write("<a href=/Web/html/" + fileName + " mce_href=" + fileName + " target=_blank>" + fileName + "</a>已经生成!");
- }
- }
三,总结
ok,原理已经明白了。那么,从效益出发,实际应用中应该是怎样的呢?一:每先建一个静态页面模版,就相应生成一个静态模版页;
二:或者直接从数据库中读取模版的数据流,然后相应替换;
三:确定保存文章的时候,立刻生成一个相应的静态文章页;
四:在网站后台管理中,嵌入“一键生成静态页面”中,重新检索生成漏掉或者有更改的静态页面。
那么,其它比如是否细分类别来实现对应的静态化功能,就以自己实际项目来考虑了最后,附最后的效果图。。。。