對天乙社區bbscs8實現的詳細分析十八

我們看一下signSet:
<strong><a href="javascript:;" οnclick="loadSignEditPage(?');"><s:text name="signset.sign"/>A</a></strong>
<div id="signDiv0" class="signDivOff" οnclick="loadSignEditPage(?');" οnmοuseοver="over(this);" οnmοuseοut="out(this);"><s:property value="%{userSign0}" escape="false"/></div>
需要注意到底部有個div:
<tr>
          <td colspan="2">
            <div id="signDetailChange"></div>
          </td>
        </tr>
我們來看loadSignEditPage(signID):
function loadSignEditPage(signID) {
Element.show("signDetailChange");
$('signDetailChange').innerHTML = pageLoading;//在jsMsg.jsp中,var pageLoading = "<s:text name="js.pageLoading"/>";
var url = getActionMappingURL("/signSet");
var pars = "action=edit&ajax=shtml&signID=" + signID;

var myAjax = new Ajax.Updater("signDetailChange", url, {method: 'get', parameters: pars});
}
我們看看這個/signSet?action=edit&ajax=shtml&singnID=0;
public String edit() {
this.setAction("editdo"); //設置action!
this.setSignDetail(this.getUserSession().getSignDetail()[this.getSignID()]);
return INPUT;
}
進入signEdit.jsp:由於頁面不能緩存以前的數據!
<%
request.setAttribute("decorator", "none");
response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
%>
這裏將顯示出的內容到signDetailChange這個DIV內,而這裏的內部有一個顯示Smile的:
loadSmilePage('signDetail')
-->
function loadSmilePage(inputName) {
Element.show("smileDiv");
$('smileDiv').innerHTML = pageLoading;
var url = "smile.jsp";//根路徑中!
var pars = "inputName="+inputName;
var myAjax = new Ajax.Updater("smileDiv", url, {method: 'get', parameters: pars});
}
這裏有用到了smile.jsp,下面是顯示錶情圖片的一個jsp代碼:
<%
    int counter = 0;
    for (int i = 0; i < 85; i++) {
      if (counter == 0) {
        counter = 8;
        out.println("<tr>");
      }
%>
    <td>
      <div align="center">
        <img id="smile<%=i%>" src="images/smile/<%=i%>.gif" alt="smile" οnmοusemοve="this.style.cursor='hand'this.style.cursor='pointer'" οnclick="insertSmile('<%=inputName%>','{<%=i%>}');"/>
      </div>
    </td>
<%
    if (counter == 1) {
      counter = 0;
      out.println("</tr>");
    }
    else {
      counter = counter - 1;
    }
    }
%>
當我們選擇一個後,點擊後將觸發insertSmile('<%=inputName%>','{<%=i%>}');注意inputName爲需要寫入到哪個元素,這裏當然是textarea的signDetail,所以才用loadSmilePage('signDetail').下面的js改變文本域內容:
function insertSmile(inputName,smlieTag) {
$(inputName).value = $(inputName).value + smlieTag; //smileTag類似於{1}
$(inputName).focus();
}
對於關閉:
function closeSmilePage() {
$("smileDiv").innerHTML = "";
Element.hide("smileDiv");
}
好,我們單擊保存標籤按鈕:function signEditDo() {
var signID = $('signID').value;
var oSignEditAjax = new SignEditAjax(signID);//用到ajax請求
oSignEditAjax.edit();
}
下面是其代碼:
var SignEditAjax = Class.create();
SignEditAjax.prototype = {
initialize: function(signID) { //構造
    this.signID = signID;
},

edit: function() {
    showExeMsg();
    var url = getActionMappingURL("/signSet");
    var pars = "action=editdo&ajax=xml&signID="+this.signID+"&signDetail="+encodeURIComponent($('signDetail').value);//將文本字符串編碼爲一個統一資源標識符 (URI) 的一個有效組件
    var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: this.editCompleted.bind(this)});
},

editCompleted: function(res) {
    resText = res.responseText;
   var jsonMsgObj = new JsonMsgObj(resText);
   var codeid = jsonMsgObj.getCodeid();
    hiddenExeMsg();
    alert(jsonMsgObj.getMessage());//提示信息
    if (codeid == "0") {
      $('signDiv'+this.signID).innerHTML = jsonMsgObj.getText();//寫入內容
      closeSignEditPage();//關閉編輯框!
    }
}
};
這裏又用致函一個函數:
function hiddenExeMsg() {
var loade = document.getElementById("exeingdiv");
if (loade != null) {
    loade.style.display = "none";
}
}
OK!我們進入SignSet.java的editdo方法中:
public String editdo() {
if (BBSCSUtil.getSysCharsetStrLength(this.getSignDetail()) > this.getSysConfig().getSignMaxLen()) { // 簽名超過指定長度
/**
public static int getSysCharsetStrLength(String txt) {
try {
   return txt.getBytes(Constant.CHARSET).length;
} catch (UnsupportedEncodingException ex) {
   return txt.length();
}
}
*/
   this.getAjaxMessagesJson().setMessage(
     "E_USER_SIGN_TOOLONG",
     this.getText("error.sign.toolong", new String[] { String.valueOf(this.getSysConfig()
       .getSignMaxLen()) }));
   return RESULT_AJAXJSON;
}
UserInfo ui = this.getUserService().findUserInfoById(this.getUserSession().getId());
if (ui != null) {
   String signDetail = "";
   if (StringUtils.isBlank(this.getSignDetail())) { // 簽名爲空,設爲默認簽名
    signDetail = this.getText("bbscs.userdefaultsign");//系統用的資源
    switch (this.getSignID()) {
    case 0:
     ui.setSignDetail0(signDetail);
     break;
    case 1:
     ui.setSignDetail1(signDetail);
     break;
    case 2:
     ui.setSignDetail2(signDetail);
     break;
    }
   } else {
    signDetail = this.getSysConfig().bestrowScreen(this.getSignDetail()); // 過濾敏感詞

    switch (this.getSignID()) {
    case 0:
     ui.setSignDetail0(signDetail);
     break;
    case 1:
     ui.setSignDetail1(signDetail);
     break;
    case 2:
     ui.setSignDetail2(signDetail);
     break;
    }
   }
   try {
    ui = this.getUserService().saveUserInfo(ui);
    this.getUserSession().getSignDetail()[this.getSignID()] = signDetail;
    signDetail = BBSCSUtil.filterText(signDetail, this.getSysConfig().isSignUseHtml(), this.getSysConfig()
      .isSignUseUBB(), this.getSysConfig().isSignUseSmile());
    this.getAjaxMessagesJson().setMessage("0", this.getText("sign.edit.ok"), signDetail);//設置message提示信息!
   } catch (BbscsException ex) {
    logger.error(ex);
    this.getAjaxMessagesJson().setMessage("E_USER_SIGN_ERROR", this.getText("error.sign.edit"));
   }
}
return RESULT_AJAXJSON;
}
這裏的關鍵是對文本信息的過濾(BBSCSUtil):
public String bestrowScreen(String txt) { //將系統不允許出現的字詞換成**
if (StringUtils.isNotBlank(this.getScreenWord())) {
   String[] words = this.getScreenWord().split(";");
   for (int i = 0; i < words.length; i++) {
    txt = txt.replaceAll(words[i], this.getBestrowScreen());
   }
}
return txt;
}
public static String filterText(String sign, boolean useHTML, boolean useUBB, boolean useSmile) {
if (!useHTML) { //默認1
   sign = TextUtils.htmlEncode(sign);//轉意字符!這裏用的是com.opensymphony.xwork2.util工具類!
}
if (useUBB) {//0
   sign = getUBB2HTML(sign);
}
if (useSmile) {//1
   sign = replaceSmile(sign);
}
sign = sign.replaceAll(" ", "<BR/>");
sign = filterScript(sign);
return sign;
}
-->
public static String replaceSmile(String txt) {
if (txt != null) {
   return txt.replaceAll("/{(/d{1,2})/}", "<img src="images/smile/$1.gif" alt="smile"/>");//正則表達式!
} else {
   return "";
}
}
public static String filterScript(String txt) {
return txt.replaceAll("[Ss][Cc][Rr][Ii][Pp][Tt]", "s.c.r.i.p.t");
}
public static String getUBB2HTML(String txt) {//UBB實現
if (txt != null) {
   AutoFilter af = new AutoFilter(txt);
   txt = af.getFilteredStr();
}
return txt;
}
AutoFilter繼承了RegexFilter...而RegexFilter實現了Filter接口:
public interface Filter {
public abstract String getFilteredStr();//我們便通過這個方法得到返回的結果的!
}
在AutoFilter的構造方法中有許多regex的reStr屬性(在RegexFilter定義爲protected)也有this.doFiltration();方法,我們看super(txt)方法:
protected RegexFilter(String source) {
    this.source = source;
    this.tempSource = source;
}
而每次代換doFiltration():
protected void doFiltration() {
    this.applyFilter();
    this.tempSource = filter.getFilteredStr();
}
用了 protected void applyFilter() {
    FilterBuilder builder = new RegFilterBuilder(regex, rpStr, tempSource);//注意這裏用的是tempSource!
    FilterDirector direct = new FilterDirector(builder);//導演FilterDirector生成FilterBuilder的各種實現
    direct.construct();

    this.filter = builder.getFilter();
}
我們看真正的執行者:RegFilterBuilder,實現了FilterBuilder中的所有方法:
public interface FilterBuilder {
public abstract void buildFilter();
public abstract Filter getFilter();
}
它使用的是JDK中的正則類:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public RegFilterBuilder(String regex, String rpStr, String source) {
    super();
    this.regex = regex;
    this.rpStr = rpStr;
    this.source = source;
}
public void buildFilter() { //關鍵的方法!需重點理解之!請參考資料:http://wcjok.bokee.com/4293762.html
    if (this.regex == null) {
      return;
    }
    Pattern p = Pattern.compile(regex, 2);
    Matcher matcher = p.matcher(this.source);
    StringBuffer sb = new StringBuffer();
    String tempString = rpStr;
    int rpL = rpStr.split("/$[0-9]+").length;
    while (matcher.find()) {
      for (int i = 0; (i < rpL) && (i < matcher.groupCount()); i++) {
        tempString = tempString.replaceAll("/$" + i, matcher.group(i));
      }
      matcher.appendReplacement(sb, tempString);
    }
    matcher.appendTail(sb);
    this.result = sb.toString();
}
public Filter getFilter() { //回調一個剛過濾的中間結果!
    return (new RegexFilter() {
      public String getFilteredStr() {
        return result;
      }
    });
當然我們還有一個FilterDirector!由它管理FilterBuilder對象和它的buildFilter方法!
public FilterDirector(FilterBuilder builder) {
    this.builder = builder;
}
public void construct() {
    builder.buildFilter();
}
而getFilter則仍由Builder自己來返回this.filter = builder.getFilter();
接下來,我們分析nickNameSet.bbscs,它很簡單:
<action name="nickNameSet" class="nickNameSetAction">
   <interceptor-ref name="mainUserAuthInterceptorStack"></interceptor-ref>
   <result name="input">/WEB-INF/jsp/nickNameSet.jsp</result>
</action>
public String index() {
this.setAction("edit");
this.setNickName(this.getUserSession().getNickName());
return INPUT;
}
<s:form action="nickNameSet">
      <s:hidden name="action"></s:hidden>
      <tr>
        <td><s:text name="nickset.title"/></td>
        <td>
          <s:textfield id="nickName" name="nickName" cssClass="input2" size="40" maxlength="20" οnkeypress="return handleEnter(this, event);"></s:textfield>//已經被填充!
        </td>
....
我們提交editNickName():
function editNickName() {
var url = getActionMappingURL("/nickNameSet");
var pars = "action=edit&ajax=xml&nickName="+encodeURIComponent($('nickName').value);
var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: editNickNameOK});
}

function editNickNameOK(res) {
resText = res.responseText;
var jsonMsgObj = new JsonMsgObj(resText);
var codeid = jsonMsgObj.getCodeid();
alert(jsonMsgObj.getMessage());
if (codeid == "0") {
    $('nickNameDiv').innerHTML = jsonMsgObj.getText();
}
}
對於public String edit()方法我們不在分析了,哦,注意edit()的返回類型!String!
OK!我們已經將接下來分析userConfig.bbscs!
<action name="userConfig" class="userConfigSetAction">
   <interceptor-ref name="mainUserAuthInterceptorStack"></interceptor-ref>
   <result name="input">/WEB-INF/jsp/userConfig.jsp</result>
</action>
先看userConfigSet.java:有acceptFriend,forumPerNum,forumViewMode,hiddenLogin,postPerNum,receiveNote,editType,timeZone等交互字段!也有sysOptionsValues(服務),userForumNumPerPageValues(List<OptionsInt>類型),userPostNumPerPageValues,userTimeZoneValues(List<OptionsString>),forumViewModeValues,radioEditInterfaceList等填充用的!
public String index() {
this.setUserForumNumPerPageValuesInit();//初始化用戶文章列表每頁顯示數
this.setUserPostNumPerPageValuesInit();
this.setForumViewModeValuesInit();
this.setRadioEditInterfaceValues();
this.setAction("edit");
UserInfo ui = this.getUserService().findUserInfoById(this.getUserSession().getId());
if (ui != null) {
   this.setAcceptFriend(this.int2boolean(ui.getAcceptFriend()));//字段,這裏的int2boolean是BaseAction中的方法!現在用的是int2boolean!
   this.setForumPerNum(ui.getForumPerNum());
   this.setForumViewMode(ui.getForumViewMode());
   this.setHiddenLogin(this.int2boolean(ui.getHiddenLogin()));
   this.setPostPerNum(ui.getPostPerNum());
   this.setReceiveNote(this.int2boolean(ui.getReceiveNote()));
   this.setTimeZone(ui.getTimeZone());
   this.setEditType(ui.getEditType());
}
return INPUT;
}
-->
private void setUserForumNumPerPageValuesInit() {
this.setUserForumNumPerPageValues(this.getSysOptionsValues().getUserForumNumPerPageValues(this.getLocale()));
}
private void setUserPostNumPerPageValuesInit() {
this.setUserPostNumPerPageValues(this.getSysOptionsValues().getUserPostNumPerPageValues(this.getLocale(),
    this.getSysConfig().getUserPostPerPageNum()));
}
private void setForumViewModeValuesInit() {
this.setForumViewModeValues(this.getSysOptionsValues().getForumViewModeValues(this.getLocale()));
}
private void setRadioEditInterfaceValues() {
radioEditInterfaceList.add(new RadioInt(-1, this.getText("bbscs.editInterface"))); //值對!
radioEditInterfaceList.add(new RadioInt(0, this.getText("bbscs.editInterface0")));
radioEditInterfaceList.add(new RadioInt(1, this.getText("bbscs.editInterface1")));
radioEditInterfaceList.add(new RadioInt(2, this.getText("bbscs.editInterface2")));
}
注意到SysOptionsValues提供了系統的一些Option值,它在com.laoer.bbscs.comm包中! 我們看它是怎麼被注入到spring中的,在applicationContext.xml:
<bean id="sysOptionsValues"
class="com.laoer.bbscs.comm.SysOptionsValues">
<property name="messageSource">
   <ref bean="messageSource" />
</property>
</bean>
OK!
public List<OptionsInt> getUserForumNumPerPageValues(Locale locale) {//local用於本地化!
List<OptionsInt> l = new ArrayList<OptionsInt>();
l.add(new OptionsInt(0, this.getMessageSource().getMessage("bbscs.usesystem", null, locale)));
l.add(new OptionsInt(20, "20"));
l.add(new OptionsInt(30, "30"));
l.add(new OptionsInt(40, "40"));
return l;
}
public List<OptionsInt> getUserPostNumPerPageValues(Locale locale, String[] ppns) {
List<OptionsInt> l = new ArrayList<OptionsInt>();
l.add(new OptionsInt(0, this.getMessageSource().getMessage("bbscs.usesystem", null, locale)));
for (int i = 0; i < ppns.length; i++) {
   l.add(new OptionsInt(NumberUtils.toInt(ppns[i], 10), ppns[i]));
}
return l;
}
public List<OptionsInt> getForumViewModeValues(Locale locale) {
List<OptionsInt> l = new ArrayList<OptionsInt>();
l.add(new OptionsInt(0, this.getMessageSource().getMessage("bbscs.viewmode0", null, locale)));
l.add(new OptionsInt(1, this.getMessageSource().getMessage("bbscs.viewmode1", null, locale)));
l.add(new OptionsInt(2, this.getMessageSource().getMessage("bbscs.viewmode2", null, locale)));
return l;
}
這裏有一個OptionsInt,它在com.laoer.bbscs.web.ui包中!它也是
public OptionsInt(int key, String value) {
this.key = key;
this.value = value;

}
在UserconfigSet中還有一個List<OptionsString>:
private List<OptionsString> userTimeZoneValues = Constant.USERTIMEZONE;
public List<OptionsString> getUserTimeZoneValues() { //提供給JSP頁面顯示!
return userTimeZoneValues;
}
我們可以看看Constant中的初始化,在其static代碼段內:
for (int i = 0; i < TIMEZONEVALUES.length; i++) {
   String[] values = TIMEZONEVALUES[i];
   TIMEZONE.add(new OptionsInt(i, values[0]));
   USERTIMEZONE.add(new OptionsString(values[1], values[0]));//返回值!
}
-->
public OptionsString(String key, String value) {
this.key = key;
this.value = value;
}
好,我們看顯示的JSP頁面,userConfig.jsp:
<s:select list="forumViewModeValues" name="forumViewMode" id="forumViewMode" cssClass="select1" listKey="key" listValue="value"></s:select>//OptionInt中有key和value兩個屬性!
   <s:radio list="radioEditInterfaceList" name="editType" listKey="key" listValue="value" theme="bbscs0"></s:radio>//theme=bbscs0!,下面是radiomap.ftl的代碼:
<@s.iterator value="parameters.list">
    <#if parameters.listKey?exists>
        <#assign itemKey = stack.findValue(parameters.listKey)/>
    <#else>
        <#assign itemKey = stack.findValue('top')/>
    </#if>
    <#assign itemKeyStr = itemKey.toString() />
    <#if parameters.listValue?exists>
        <#assign itemValue = stack.findString(parameters.listValue)/>
    <#else>
        <#assign itemValue = stack.findString('top')/>
    </#if>
<input type="radio" name="${parameters.name?html}" id="${parameters.id?html}${itemKeyStr?html}"<#rt/>
<#if tag.contains(parameters.nameValue, itemKey)> //關鍵點!!!
checked="checked"<#rt/>
</#if>
<#if itemKey?exists>
value="${itemKeyStr?html}"<#rt/>
</#if>
<#if parameters.disabled?default(false)>
disabled="disabled"<#rt/>
</#if>
<#if parameters.tabindex?exists>
tabindex="${parameters.tabindex?html}"<#rt/>
</#if>
<#if parameters.cssClass?exists>
class="${parameters.cssClass?html}"<#rt/>
</#if>
<#if parameters.cssStyle?exists>
style="${parameters.cssStyle?html}"<#rt/>
</#if>
<#if parameters.title?exists>
title="${parameters.title?html}"<#rt/>
</#if>
<#include "/${parameters.templateDir}/simple/scripting-events.ftl" />
<#include "/${parameters.templateDir}/simple/common-attributes.ftl" />
/><#rt/>
<label for="${parameters.id?html}${itemKeyStr?html}"><#rt/>
    ${itemValue}<#t/>
</label><br/>
</@s.iterator>
我們看下生成的代碼html:
   <input type="radio" name="editType" id="userConfig_editType-1" value="-1"/><label for="userConfig_editType-1">使用系統默認設置</label><br/>
<input type="radio" name="editType" id="userConfig_editType0" value="0"/><label for="userConfig_editType0">禁用控件</label><br/>
<input type="radio" name="editType" id="userConfig_editType1" value="1"/><label for="userConfig_editType1">啓用標準控件</label><br/>
<input type="radio" name="editType" id="userConfig_editType2" checked="checked" value="2"/><label for="userConfig_editType2">啓用標準和所見即所得控件</label><br/>
好,我們修改後提交觸發 <input type="button" name="ClosePage" value="<s:text name="bbscs.botton.save"/>" class="button2" οnclick="editUserConfig();"/>
下面是ajax處理的js:
function editUserConfig() {
showExeMsg();//處理框出現!紅色的哦~
var url = getActionMappingURL("/userConfig");
var pars = "action=edit&ajax=xml&hiddenLogin=" + getCheckBoxValue("hiddenLogin") + "&receiveNote="
+ getCheckBoxValue("receiveNote") + "&acceptFriend=" + getCheckBoxValue("acceptFriend") + "&forumViewMode="
+ $('forumViewMode').value + "&forumPerNum=" + $('forumPerNum').value + "&postPerNum="
+ $('postPerNum').value + "&timeZone=" + encodeURIComponent($('timeZone').value)
+ "&editType=" + getRadioValueByName("editType"); //好多的參數啊!!!!
//alert(pars);
var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: editUserConfigOK});
}

function editUserConfigOK(res) {
resText = res.responseText;
var jsonMsgObj = new JsonMsgObj(resText);
var codeid = jsonMsgObj.getCodeid();
hiddenExeMsg();
alert(jsonMsgObj.getMessage());
}
好的,我們回到UserConfigSet.java:
public String edit() {
UserInfo ui = this.getUserService().findUserInfoById(this.getUserSession().getId());
if (ui != null) {
   ui.setAcceptFriend(this.boolean2int(this.getAcceptFriend()));//action自動獲得值!注意現在是boolean2int!
   ui.setForumPerNum(this.getForumPerNum());
   ui.setForumViewMode(this.getForumViewMode());
   ui.setHiddenLogin(this.boolean2int(this.getHiddenLogin()));
   ui.setPostPerNum(this.getPostPerNum());
   ui.setReceiveNote(this.boolean2int(this.getReceiveNote()));
   ui.setTimeZone(this.getTimeZone());
   ui.setEditType(this.getEditType());

   try {
    ui = this.getUserService().saveUserInfo(ui);
    this.getUserCookie().addCookies(ui);//加入cookie中!
    this.getAjaxMessagesJson().setMessage("0", this.getText("userconfig.set.ok"));
   } catch (BbscsException ex) {
    logger.error(ex);
    this.getAjaxMessagesJson().setMessage("E_USERCONFIG_EDITFAILED",
      this.getText("error.userconfig.seterror"));
   }
   return RESULT_AJAXJSON;
} else {
   this.getAjaxMessagesJson().setMessage("E_USER_NOEXIST", this.getText("error.user.noexist"));
   return RESULT_AJAXJSON;
}
}
我們看friendSet.bbscs!
<action name="friendSet" class="friendSetAction">
   <interceptor-ref name="mainUserAuthInterceptorStack"></interceptor-ref>
   <result name="success">/WEB-INF/jsp/friendSet.jsp</result>
   <result name="flist">/WEB-INF/jsp/friendList.jsp</result>
   <result name="input">/WEB-INF/jsp/friendAdd.jsp</result>
</action>
它有friendList,freindName,id,isBlack,friendComment等屬性.
public String index() {
return SUCCESS;
}
直接進入friendSet.jsp:
<body οnlοad="loadFriendList();"> //<script type="text/javascript" src="js/friend.js"></script>
<div id="f_bg">
<div id="f_tabs">
    <ul>
      <li id="tab1" class="f_tabClass1"><a href="javascript:;" οnclick="loadFriendList();"><s:text name="friend.fuser"/></a></li>
      <li id="tab2" class="f_tabClass2"><a href="javascript:;" οnclick="loadBlackUserList();"><s:text name="friend.blackuser"/></a></li>
    </ul>
</div>
</div>
<div id="f_main">
<div id="friendlist"></div>//一個div
<div id="addfriend"></div>//另一個div,用於ajax
</div>
</body>
我們在js/friend.js中找到相應的js:
function loadFriendList() { //含一些初始化工作!
hiddenElement("addfriend");//隱藏addfriend這個div
$('tab1').className = "f_tabClass1";//着色!
$('tab2').className = "f_tabClass2";
$('friendlist').innerHTML = pageLoading;//加載中!
var url = getActionMappingURL("/friendSet");
var pars = "action=flist&ajax=shtml&isBlack=0";
var myAjax = new Ajax.Updater("friendlist", url, {method: 'get', parameters: pars});
}
-->
public String flist() {
this.setFriendList(this.getFriendService().findFriends(this.getUserSession().getId(), this.getIsBlack()));//找到他們!
return "flist";
}
這裏首先用了:
<%
request.setAttribute("decorator", "none");
response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
%>
<s:iterator id="f" value="%{friendList}"> //下面是用於顯示的遍歷!
    <tr>
      <td>
        <s:property value="#f.friendName"/>
      </td>
      <td>
        <s:property value="#f.friendComment"/>
      </td>
      <td><a href="javascript:;" οnclick="friendDel('<s:property value="#f.id"/>','<s:property value="#f.isBlack"/>');"><s:text name="bbscs.del"/></a></td>
    </tr>
</s:iterator>
下面的是這個增加按鈕的顯示:
<s:if test="%{isBlack==0}">
      <a href="javascript:;" οnclick="friendNew(?');"><s:text name="friend.add"/></a>
      </s:if>
      <s:if test="%{isBlack==1}">
      <a href="javascript:;" οnclick="friendNew(?');"><s:text name="friend.addblack"/></a>
      </s:if>
當我們點擊黑名單時,調用JS:
function loadBlackUserList() {
hiddenElement("addfriend");
$('tab1').className = "f_tabClass2";
$('tab2').className = "f_tabClass1";
$('friendlist').innerHTML = pageLoading;
var url = getActionMappingURL("/friendSet");
var pars = "action=flist&ajax=shtml&isBlack=1";
var myAjax = new Ajax.Updater("friendlist", url, {method: 'get', parameters: pars});
}
如果有好友,我們可以刪除之..
function friendDel(id,isBlack) {
var del = confirm(confirm_del); //需要確認一下!
if (del) {
    var oFriendDelAjax = new FriendDelAjax(id,isBlack);
    oFriendDelAjax.delFriend();
}
else {
    return false;
}
}
看下面的代碼:
var FriendDelAjax = Class.create();
FriendDelAjax.prototype = {
initialize: function(id,isBlack) {
    this.id = id;
    this.isBlack = isBlack;
},

delFriend: function() {
    showExeMsg();
    var url = getActionMappingURL("/friendSet");
    var pars = "action=del&ajax=xml&id=" + this.id;
    var myAjax = new Ajax.Request(url, {method: 'get', parameters: pars, onComplete: this.delFriendCompleted.bind(this)});
},
delFriendCompleted: function(res) {
    resText = res.responseText;
   var jsonMsgObj = new JsonMsgObj(resText);
   var codeid = jsonMsgObj.getCodeid();
    hiddenExeMsg();
    alert(jsonMsgObj.getMessage());
    if (codeid == "0") {
      if (this.isBlack == "0") {
        loadFriendList(); //根據isBlack重新加載好友列表!
      }
      if (this.isBlack == "1") {
        loadBlackUserList();
      }
    }
}
};

發佈了76 篇原創文章 · 獲贊 4 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章