⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 详细分析二.txt

📁 开源论坛实现
💻 TXT
📖 第 1 页 / 共 5 页
字号:
			sb.append(this.getAttachFile(f)); //有附件!!!
		} else {
			sb.append("<div id=\"upfile");
			sb.append(f.getId());
			sb.append("\" class=\"font5\" style=\"display:none\"></div>");
		}

		if (f.getIsVote() == 0) {
			String detail = this.getForumDetail(f);
			if (f.getEditType() == 0) {
				sb.append(BBSCSUtil.filterText(detail, (board.getAllowHTML() == 1), (board.getAllowUBB() == 1), true));
			} else {
				sb.append(BBSCSUtil.filterScript(detail));
			}
		} else {
			if (f.getEditType() == 0) {
				sb.append(BBSCSUtil.filterText(f.getDetail(), (board.getAllowHTML() == 1), (board.getAllowUBB() == 1),
						true));
			} else {
				sb.append(BBSCSUtil.filterScript(f.getDetail()));
			}
		}
		return sb;
	}
对于getAttachFile()方法我们省略分析之,我们看下:
public static String filterText(String sign, boolean useHTML, boolean useUBB, boolean useSmile) {
		if (!useHTML) {
				sign = TextUtils.htmlEncode(sign);
		}
		if (useUBB) {
			sign = getUBB2HTML(sign);
		}
		if (useSmile) {
			sign = replaceSmile(sign);
		}
		sign = sign.replaceAll("\n", "<BR/>");
		sign = filterScript(sign);
		return sign;
	}
public static String getFileTypeIcon(String fileExt) {
		String fileTypeIcon = (String) Constant.ICON_MAP.get(fileExt);
		if (fileTypeIcon == null) {
			fileTypeIcon = "default.icon.gif";
		}
		return fileTypeIcon;
	}
需要注意的是ICON_MAP在Constant中有定义值了!
*/
							topiclist.add(tmap);
							if (x != 0) { //非第一个帖,就是mainID是自己的帖子!
								this.forumArchivesDAO.saveForumArchives(b.getId(), tf);//存档!
								this.getForumHistoryDAO().removeForum(tf);//从历史帖中删除之
							}
						}

						Template temptopic = this.getTempConfiguration().getTemplate("archivesPostTopic.ftl");
/**下面是这个ftl的部分代码:
<body>
<div id="topic">
<#list topiclist as fm>
<div class="postdiv" id="post${fm["id"]}">
  <div class="title" id="title${fm["id"]}">${fm["title"]}</div>
  <div class="author" id="author${fm["id"]}">作者:${fm["username"]} (发表时间:${fm["posttime"]})</div>
  <div class="content" id="content${fm["id"]}">
  ${fm["content"]}
  </div>
</div>
</#list>
<div align="center" class="postdiv">
<script type="text/javascript"><!--
google_ad_client = "pub-9505761087047507";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text";
google_ad_channel = "";
//-->
</script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
</div>
</body>
</html>
*/

						OutputStream out = new FileOutputStream(BBSCSUtil.getArchivesPostTopicPath(b.getId(), month, f
								.getPostTime())
								+ f.getId() + ".html");
/**
	public static String getArchivesPostMainListWebPath(long bid, String month) {
		StringBuffer sb = new StringBuffer();
		sb.append("archives/");
		sb.append(bid);
		sb.append("/");
		sb.append(month);
		sb.append("/");
		return sb.toString();
	}
public static String getArchivesPostTopicPath(long bid, String month, long posttime) {
		StringBuffer sb = new StringBuffer();
		sb.append(Constant.ROOTPATH);
		sb.append(getArchivesPostMainListWebPath(bid, month));
		sb.append(Util.formatDate4(new Date(posttime)));
		sb.append("/");
		sb.append((posttime % 100));
		sb.append("/");
		File ft = new File(sb.toString());
		if (!ft.exists()) {
			ft.mkdirs();
		}
		return sb.toString();
	}
*/
						Writer writer = new OutputStreamWriter(out, Constant.CHARSET);
						Map root = new HashMap();
						root.put("topoctitle", f.getTitle());
						root.put("topiclist", topiclist);

						temptopic.process(root, writer);
						writer.flush();//处理f存档!

						this.forumArchivesDAO.saveForumArchives(b.getId(), f);//保存好f
						this.getForumHistoryDAO().removeForum(f);//从历史帖中删除之
					}
					StringBuffer sb = new StringBuffer();
					for (int x = 1; x <= allPage; x++) {
						sb.append("[<a href=\"");
						sb.append(x);
						sb.append(".html\">");
						if (x == i) {
							sb.append("<strong>");
							sb.append(x);
							sb.append("</strong>");
						} else {
							sb.append(x);
						}
						sb.append("</a>] ");
					}

					OutputStream out = new FileOutputStream(BBSCSUtil.getArchivesPostMainListPath(b.getId(), month) + i
							+ ".html"); //i是页码,如果有30页的话,那就是30个文件了!
					Writer writer = new OutputStreamWriter(out, Constant.CHARSET);
					Map root = new HashMap();
					root.put("boardName", b.getBoardName());
					root.put("month", month);
					root.put("mainlist", mainlist);
					root.put("pagebreak", sb.toString());

					temp.process(root, writer);//这些参数与文件中的参数一致!
					writer.flush();
				}

			} catch (Exception e) {
				logger.error(e);
				throw new BbscsException(e);
			}
		}
	}
对于上面的部分可以查看历史帖选项查看效果!

接下来是FriendFactory,是有个方法public Friend getInstance(String userId);用于实例化Friend对象用!进入Friend Bean:(注意其实现了可序列化接口implements Serializable与BookMark一样,其实后面的Note,Subscibe都一样,bean包下还有Friend0~9个与Friend内容一样的BEAN,它们主要是为了拆表准备的,普通版本用不到)
	private String id;
	private String userID;
	private String userName;
	private String friendID;
	private String friendName;
	private String friendComment;//介绍
	private int isBlack;//是否加入黑名单
我们看其Friend.hbm.xml:
<hibernate-mapping package="com.laoer.bbscs.bean">
  <class name="Friend" table="bbscs_friend">
    <id name="id" column="ID" type="string" unsaved-value="null">
      <generator class="uuid"/>
    </id>
    <property column="UserID" length="40" name="userID" not-null="true" type="string"/>
    <property column="UserName" length="20" name="userName" not-null="true" type="string"/>
    <property column="FriendID" length="40" name="friendID" not-null="true" type="string"/>
    <property column="FriendName" length="20" name="friendName" not-null="true" type="string"/>
    <property column="FriendComment" length="2000" name="friendComment" type="string"/>//length=2000?--->对应表中的`FriendComment` text, //说明
    <property column="IsBlack" length="1" name="isBlack" type="int"/>//`IsBlack` tinyint(1) default '0'
  </class>
</hibernate-mapping>
看服务层的方法:
public Friend saveFriend(Friend f) throws BbscsException;
public Friend findFriendByID(String id, String ownId);
public Friend findFriendByName(String fname, String ownId);//根据朋友名、自己的ID取得
public long getFriendNum(String ownId, int isBlack);
public List findFriends(String ownId, int isBlack);
public void removeFriend(Friend f) throws BbscsException;
public void removeFriend(String id, String ownId) throws BbscsException;
public void friendIDsToFile(String ownId);//好友列表ID保存至文件
public List fileToFriendIDs(String ownId);
public List findFriendIds(String ownId, int isBlack);
进入service.imp层,首先是
public class FriendFactoryImp
    implements FriendFactory {
  public FriendFactoryImp() {
  }
   public synchronized Friend getInstance(String userId) {
    return new Friend();//同步方法!!实例化Friend对象
  }
}
而另外的FriendsFactoryImp实现,需要注意的是它也实现了FriendFactory接口:
public synchronized Friend getInstance(String userId) {
    try {
      return (Friend) Class.forName(BBSCSUtil.getClassName("Friend", userId)).newInstance();
    }
/**
	public static String getClassName(String className, String userID) {
		int num = Math.abs(userID.hashCode());
		className = Constant.BEANPERFIX + className + (num % 10);
		return className; //Friend0~~~~9
	}
public static String getClassName(String className, String userID, int modnum) {
		int num = Math.abs(userID.hashCode());
		className = Constant.BEANPERFIX + className + (num % modnum);
		return className;
	}//modnum由setModnum具体得到
*/
    catch (ClassNotFoundException ex) {
      logger.error(ex);
      return null;
    }
    catch (IllegalAccessException ex) {
      logger.error(ex);
      return null;
    }
    catch (InstantiationException ex) {
      logger.error(ex);
      return null;
    }
  }
我们看其主要的实现方法:
/**
 * 好友列表ID保存至文件
 */
  public void friendIDsToFile(String ownId) {
    List l = this.getFriendDAO().findFriends(ownId, 0);
    StringBuffer sb = new StringBuffer();
    Friend f;
    for (int i = 0; i < l.size(); i++) {
      f = (Friend) l.get(i);
      sb.append(f.getFriendID());
      sb.append(",");
    }
    File toFile = new File(this.getUserConfig().getUserFilePath(ownId) + Constant.USER_FRIEND_FILE);
//public static final String USER_FRIEND_FILE = "UserFriendFile.txt";
    try {
		FileUtils.writeStringToFile(toFile, sb.toString(), Constant.CHARSET);
	} catch (IOException e) {
		logger.error(e);
	}
  }
   
  public List fileToFriendIDs(String ownId) {
    List<String> l = new ArrayList<String>();
    File fromFile = new File(this.getUserConfig().getUserFilePath(ownId) + Constant.USER_FRIEND_FILE);
    try {
		String fids = FileUtils.readFileToString(fromFile, Constant.CHARSET);
		String[] ids = fids.split(",");//分割出来!
		if (ids != null) {
		  for (int i = 0; i < ids.length; i++) {
		    //System.out.println(ids[i]);
		    l.add(ids[i]);
		  }
		}
	} catch (IOException e) {
		logger.error(e);
	}
    return l;
  }

  @SuppressWarnings("unchecked")
public List findFriendIds(String ownId, int isBlack) {
	  List l = this.getFriendDAO().findFriendIds(ownId, isBlack);
	  if (l.isEmpty()) {   //填充List
		  l.add("0");
	  }
	  return l;
  }

Friend DAO接口中提供如下方法:
 public Friend saveFriend(Friend f);
 public Friend findFriendByID(String id, String ownId);
 public Friend findFriendByName(String fname, String ownId);
 public long getFriendNum(String ownId, int isBlack);
 public List findFriends(String ownId, int isBlack);
 public void removeFriend(Friend f);
 public void removeFriend(String id, String ownId);
 public List findFriendIds(String ownId, int isBlack);
对于DAO的实现FriendHibernateDAO省略.由于上次忘记分析BookMarksHibernateDAO(拆表用的),这里我们详细分析一下FriendsHibernateDAO:(有一个属性private int modNum,而这个服务类其实也不用了)
public Friend findFriendByID(String id, String ownId) {
		StringBuffer sb = new StringBuffer();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -