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

📄 详细分析四.txt

📁 开源论坛实现
💻 TXT
📖 第 1 页 / 共 5 页
字号:
}

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" onclick="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 onload="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:;" onclick="loadFriendList();"><s:text name="friend.fuser"/></a></li>
      <li id="tab2" class="f_tabClass2"><a href="javascript:;" onclick="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:;" onclick="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:;" onclick="friendNew('0');"><s:text name="friend.add"/></a>
      </s:if>
      <s:if test="%{isBlack==1}">
      <a href="javascript:;" onclick="friendNew('1');"><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();
      }
    }
  }
};
我们看JAVA代码片断:
Friend f = this.getFriendService().findFriendByID(this.getId(), this.getUserSession().getId());//找到friend!
		if (f != null) {
			UserInfo ui = this.getUserService().findUserInfoById(f.getFriendID());
			int isBlack = f.getIsBlack();
			try {
				this.getFriendService().removeFriend(f);//去之
				if (ui != null) {
					if (isBlack == 0) {//数据库的记录是好友!
						ui.setUserKnow(ui.getUserKnow() - 1); // 减少用户人缘系数
						this.getUserService().saveUserInfo(ui);
					} else {
						ui.setUserKnow(ui.getUserKnow() + 1); // 增加用户人缘系数
						this.getUserService().saveUserInfo(ui);
					}

⌨️ 快捷键说明

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