当前位置: 首页 > news >正文

伊春seo公司/百度首页排名优化哪家专业

伊春seo公司,百度首页排名优化哪家专业,国内erp软件公司排名,厦门网站建设案例java里生成HTML的静态文件也是经常要用到的,今小结之,这里用的是spring mvc,其他的框架都差不都的思路. 1 新闻模版,用freemarker实现. <html> <head> <title>${news.newstitle}</title> </head> <body> <table width"100%&quo…

   java里生成HTML的静态文件也是经常要用到的,今小结之,这里用的是spring mvc,其他的框架都差不都的思路.

 

1 新闻模版,用freemarker实现.
   <html>
<head>
<title>${news.newstitle}</title>
</head>
<body>
<table width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center"><table width="778" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td>
          <table cellspacing="0" cellpadding="0" width="100%">
            <tr>
              <td class="news_title" align="center" bgcolor="#F8F8F8">${news.newstitle}</td>
            </tr>
            <tr>
              <td height="1" bgcolor="#DDDDDD"></td>
            </tr>
            <tr>
              <td height="30" align="center" bgcolor="#F8F8F8"><font color="#FF3300">${news.newsfrom}</font>&nbsp;&nbsp;&nbsp;&nbsp;${news.newsdate}&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">${news.newsauthor}</font></td>
            </tr>
            <tr>
              <td valign="top" class="p_content" bgcolor="#F8F8F8">${news.newscontent}</td>
            </tr>
          </table>
          </td>
        </tr>
      </table></td>
  </tr>
</table>
</body>
</html>

 

2 我们考察其dao层的insert 方法如下
   public void insertNews(News news) throws DataAccessException {
  TemplateParam templateParam = SetTemplateParam(news);
  // 生成静态新闻
  news.setFilename(new MakeFile().make(news, templateParam));
  this.getHibernateTemplate().save(news);

 

/**
  * 修改新闻
  *
  * @param news
  *
  */
 public void updateNews(News news) throws DataAccessException {
  TemplateParam templateParam = SetTemplateParam(news);
  // 更新原来的静态新闻
  new MakeFile().update(news, templateParam);
  this.getHibernateTemplate().update(news);
 }

 

private TemplateParam SetTemplateParam(News news) {
  // 设置模板参数
  TemplateParam templateParam = new TemplateParam();
  templateParam.setRealPath(news.getRealPath());
  templateParam.setTemplateName("news.ftl");
  return templateParam;
 }

 

这个TemplateParam类的定义如下,主要是保存每个一些其他附加属性,比如模版名,模版所在的路径,新闻要保存的目录,

 

public class TemplateParam {
 private String realPath;

 private String templatePath;

 private String saveDirectory;

 private String filePostfix;
 
 private String templateName;

 

   之后, public void insertNews(News news) throws DataAccessException {
  TemplateParam templateParam = SetTemplateParam(news);
  // 生成静态新闻
  news.setFilename(new MakeFile().make(news, templateParam));
  中,先调用makefile()方法,把生成的静态文件先保存到磁盘上,然后在用DAO入库news实体的其他属性.

 

再看makefile函数

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

 

public String make(News news, TemplateParam templateParam) {
  Configuration cfg = new Configuration();
  // 项目真实路径
  String realPath = templateParam.getRealPath();
  // 新闻模板路径
  String templatePath = realPath + Constant.NEWSTEMPLATEPATH;

  //存放在news目录下的以yyyymmdd表示的子目录中,即按天数来存放
  String cDateStr = "news/" + PureUtil.getDateFormatStr("yyyyMMdd");

//文件后缀
  String filePostfix = ".html";


  // 静态新闻生成文件存放路径
  String path = realPath + cDateStr;

  String fileTimeName = PureUtil.getDateFormatStr("yyyyMMddhhmmss");
  // 写到数据库地路径
  String returnFileName = cDateStr + "/" + fileTimeName + filePostfix;
  String fileName = "";
  File newsDir = new File(path);
  fileName = path + "/" + fileTimeName + filePostfix;
  if (!newsDir.exists()) {
   newsDir.mkdirs();
   fileName = path + "/" + fileTimeName + filePostfix;
  }
  try {
   cfg.setDirectoryForTemplateLoading(new File(templatePath));
   cfg.setObjectWrapper(new DefaultObjectWrapper());
   Template newsTemplate = cfg.getTemplate(templateParam.getTemplateName());
   newsTemplate.setEncoding("GBK");

   Map root = new HashMap();
   root.put("news", news);
   Writer out = new OutputStreamWriter(new FileOutputStream(fileName));
   try {
    newsTemplate.process(root, out);
   } catch (TemplateException e) {
    e.printStackTrace();
   }
   out.flush();
   out.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return returnFileName;
 }

 /**
  * 重新生成html文件,使用原来的文件名
  *
  * @param news
  * @param templateParam
  */
 public void update(News news, TemplateParam templateParam) {
  Configuration cfg = new Configuration();
  // 项目真实路径
  String realPath = templateParam.getRealPath();
  // 新闻模板路径
  String templatePath = realPath + Constant.NEWSTEMPLATEPATH;
  String fileName = news.getFilename();
  // 原来的路径,当删除原来的目录时,重建目录
  String path = realPath+ fileName.substring(0, fileName.lastIndexOf("/"));
  fileName = realPath + news.getFilename();
  File oldDir = new File(path);
  if (!oldDir.exists()) {
   oldDir.mkdirs();
  }

 

//注意下面是用freemarker来处理的了.


  try {
   cfg.setDirectoryForTemplateLoading(new File(templatePath));
   cfg.setObjectWrapper(new DefaultObjectWrapper());
   Template newsTemplate = cfg.getTemplate(templateParam.getTemplateName());
   newsTemplate.setEncoding("GBK");
   Map root = new HashMap();

//freemarker中用news对象放到map里面去.
   root.put("news", news);
   Writer out = new OutputStreamWriter(new FileOutputStream(fileName));
   try {
    newsTemplate.process(root, out);
   } catch (TemplateException e) {
    e.printStackTrace();
   }
   out.flush();
   out.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

http://www.jmfq.cn/news/5085451.html

相关文章:

  • 中纪委网站两学一做征文/谷歌账号注册
  • 网站怎么做付款平台/汕头seo外包机构
  • 贵州专业网站建设/免费推广网址
  • 长春网站建设/seo网站优化服务
  • axure做网站效果图步骤/全球搜索引擎市场份额
  • 制作网站付费软件/网站策划是什么
  • 电商网站制作流程图/上海抖音seo公司
  • 昆明哪些做网站建设的公司/移动慧生活app下载
  • 西安行业网站建设/网络营销到底是个啥
  • html 与wordpress/重庆seo霸屏
  • 企业网站及公众号建设方案/俄罗斯搜索引擎yandex官网入口
  • hefei 网站制作/关注公众号推广2元一个
  • 企业名录搜索网站/广州seo网站
  • 建网站做站长怎么赚钱/软文一般发布在哪些平台
  • 商城网站建设方案/东莞seo推广机构帖子
  • wordpress nginx 404/关键词推广seo怎么优化
  • 网站电子签名怎么做/最新中国新闻
  • 现在标书都从哪个网站下载/网络优化的基本方法
  • 网站如何提升流量/bt种子万能搜索神器
  • 辅导班如何做网站/网络查询网站
  • 千锋教育西安校区/seo排名优化公司价格
  • 营销型网站建设大千建站/浙江网络推广公司
  • 网页使用怎么做/杭州seo顾问
  • 网站域名怎么免费获取/网络优化工程师需要学什么
  • 福永网站的建设/建网站怎么赚钱
  • 网站后期维护工作包括哪些/厉害的seo顾问
  • 查询企业年报的网站/东莞搜索引擎推广
  • 成都设计公司视频制作/百度关键词优化系统
  • 篇高端网站愿建设/在线看seo网站
  • 乐山 做网站/百度搜索引擎优化的方法