后台处理前台模板例子(tmp文件)

public static String getTopNewsTemp(List<Article> lists) {

//读取定义前台值变量的内容
  String tempContent = TempUtils.getTempFile("topNew.tmp");

//定义前台样式并用{}定义值存放位置

//  String format = "<div style=\"line-height:20px;\"><div style=\"width:350px;padding-right:5px;padding-left:5px;float:left;font-size:12px;\"><a href=\"{0}\" target=\"_blank\" title=\"{1}\">·{2}</a></div><div style=\"font-size:12px;float:right;text-aligh:left;padding-right:15px;\">{3}</div></div>";
  String format = "<li><a href=\"{0}\">{2}</a><em>{3}</em></li>";

  StringBuffer allContent = new StringBuffer();

  StringBuffer list = new StringBuffer();

//遍历添加数据到前台可读取样式

  for (int index = 0; index < lists.size(); index++) {
   Article article = lists.get(index);

   String title = article.getTitle();

//添加样式 和 值
   list.append(MessageFormat.format(format, article.getUrl(), title,
     getShortTitle(title, 27),
     getShortDate(article.getCreateDate())));

  }

//组装消息    tmp文件 #list# 获取该值

  allContent.append(tempContent.replace("#list#", list.toString()));

  return allContent.toString();
 }

 

public class TempUtils {

public static String getTempFile(String fileName)
 {
  String str = TempUtils.class.getResource("../entity/temp/local/" + fileName).getFile();
  File file = new File(str);
  StringBuffer content = new StringBuffer();
  try{
  
    InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
    BufferedReader in = new BufferedReader(isr);
    String s;

//throws IOException读取一个文本行。通过下列字符之一即可认为某行已终止:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行。
//返回:包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null 抛出: IOException - 如果发生 I/O 错误
          while ((s = in.readLine()) != null)
          {
           content.append(s);
          }
          isr.close();
          in.close();
  
  }catch(Exception ex)
  {
   return "处理读取模板时,出现错误,请检查 " + str + " 路径";
  }
  
  return content.toString();
 }
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值