以微協同項目的日誌評論發表爲例,如圖所示:
JSP頁面對應的源碼爲:
<div id="time_rzpinglun${vs.count}" class="time_pinglun left" style="display: none;">
<div class="time_pinglunlist" >
<ul id="divxs${log.id}">
<!--評論拼接顯示的地方-->
</ul>
<div class="more" id="morerz_${vs.count}" style="display: none">
<a href="javascript:void(0);" id="jzgdrz_${vs.count }" οnclick="loadmorerz('${vs.count}','${ctx }','${log.id}',1);" title="加載更多">更多評論</a>
</div>
</div>
<div class="pl_text" id="plnr_${vs.count}" contenteditable="true" οnfοcus="logfoc('${vs.count }','1');"></div>
<input class="gb_bt" type="button" value="發表" id="rzfb_${vs.count }" οnclick="subzr('${vs.count }','${log.id}','${user.id}',1,'${ctx }');" style="display: none;"/>
</div>
日誌評論文本框,默認顯示“評論點什麼吧~”,發表按鈕默認是隱藏的;當點擊文本框時,清空且顯示發表按鈕,對應的JS代碼爲:
function logfoc(num,bh){
if(bh==1){
var plrz=document.getElementById("plnr_"+num).innerHTML;
if(plrz=="評論點什麼吧~"){//當評論內容是 評論點什麼吧~
document.getElementById("plnr_"+num).innerHTML="";//清空文本框;
}
$("#rzfb_"+num).show();//日誌發表按鈕顯示
}else if(bh==2){
var plrc=document.getElementById("plrcnr_"+num).innerHTML;
if(plrc=="評論點什麼吧~"){
document.getElementById("plrcnr_"+num).innerHTML="";
}
$("#rcfb_"+num).show();
}
}
文本框輸入日誌評價內容,點擊發表按鈕,觸發input按鈕的onclick事件,對應的JS代碼爲:
function subzr(num,bh,userno,cz,ctx){
//alert(num+" "+bh+" "+userno+" "+cz+" "+ctx);
$("#plrcnr_"+num).html("");
if(cz==1){
var plnr=document.getElementById("plnr_"+num).innerHTML;//獲取評論內容
if(plnr==""){//如果評論內容爲空
document.getElementById("plnr_"+num).innerHTML="評論點什麼吧~";
return;
/*@cc_on @*/
}else if(plnr!="評論點什麼吧~"){ //如果評論內容爲其它內容
$.ajax({
type:'post',
url:ctx+'/fusion/saveReview.action',
data:'plnr='+plnr+'&scheLogId='+bh+'&userid='+userno+'&num='+num,
dataType:'text',
success:function(data){
loadtop5com(num,bh,ctx,cz);
setTimeout( //超時情況下
function(){
$("#remoteServerMessage"+bh).html(""); //彈出提示空值
$("#plnr_"+num).attr("value","");//評論內容輸入區域爲空
document.getElementById("plnr_"+num).innerHTML="評論點什麼吧~";
$("#rzfb_"+num).hide(); //發表按鈕隱藏
}, 1000);
}
});
}
}else if(cz==2){
var plnr=document.getElementById("plrcnr_"+num).innerHTML;
if(plnr==""){
document.getElementById("plrcnr_"+num).innerHTML="評論點什麼吧~";
return;
/*@cc_on @*/
}else if(plnr!="評論點什麼吧~"){
$.ajax({
type:'post',
url:'timeFAction!sendMesToLogSche.action',
data:'xxnr='+plnr+'&ywbh='+bh+'&userno='+userno,
dataType:'text',
success:function(data){
$("#time_rcpinglun"+num).hide();
$("#remoteServerMessage"+bh).html("評論成功");
setTimeout(
function(){
$("#remoteServerMessage"+bh).html("");
$("#plrcnr_"+num).attr("value","");
document.getElementById("plrcnr_"+num).innerHTML="評論點什麼吧~";
$("#rcfb_"+num).hide();
}, 1000);
}
});
}
}
}
獲取文本框輸入的評論內容,如果評論內容爲空,默認顯示“評論點什麼吧~”;如果評論內容不爲空,利用post方法傳值到後臺,具體過程爲:
確定post的URL,傳遞參數,數據類型和回調函數,其中URL,傳遞參數,數據類型是用來訪問後臺的,回調函數用於接收後臺執行的返回結果;
訪問的路徑是 項目下的/fusion命名空間下的action名稱爲saveReview 的action,Struts配置文件的具體配置,如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="fusion" extends="struts-global" namespace="/fusion">
<action name="fusion" class="fusionAction" method="fusionPersion">
<result name="fusionPersion">/WEB-INF/template/fusion/time_person.jsp</result>
</action>
<action name="fusionLogAndSche" class="fusionAction" method="LoadLogAndSche">
<result name="fusionLogAndSche">/WEB-INF/template/fusion/time_LogAndSche.jsp</result>
</action>
<!-- 異步加載評論列表 -->
<action name="top5ScheLogRevList" class="fusionAction" method="top5ScheLogRevList">
</action>
<!-- 異步發表評論 -->
<action name="saveReview" class="fusionAction" method="saveReview">
</action>
</package>
</struts>
通過class屬性值fusionAction確定Spring配置文件的Java bean的 id="fusionAction",method屬性值saveReview確定Spring配置文件中Java bean 類中要訪問的方法;
對應的Spring配置文件的具體配置,如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 日誌模塊配置 -->
<bean id="fusionDao" class="com.microxt.fusion.dao.FusionDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="fusionLogDao" class="com.microxt.fusion.dao.FusionLogDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="fusionScheDao" class="com.microxt.fusion.dao.FusionScheDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!--在FusionBizImpl中注入fusionLogDao中的方法,就是可以調用 fusionLogDao中的方法 -->
<bean id="fusionBiz" class="com.microxt.fusion.biz.impl.FusionBizImpl">
<property name="hibernateDao"> <!--當前類中的屬性 -->
<ref bean="fusionDao" /> <!--所要注入類的 bean 的 id值 即:FusionBizImpl類可以調用FusionDao類中的方法 -->
</property>
<property name="fusionLogDao">
<ref bean="fusionLogDao" />
</property>
<property name="fusionScheDao">
<ref bean="fusionScheDao" />
</property>
<property name="fusionLogReviewDao"> <!--FusionBizImpl類中聲明的logReviewDao類的對象名稱-->
<ref bean="logReviewDao"/>
</property>
<property name="logBizImpl">
<ref bean="logBiz" />
</property>
</bean>
<bean id="fusionAction" class="com.microxt.fusion.web.FusionAction"
scope="prototype">
<property name="baseBiz" ref="fusionBiz" />
</bean>
</beans>
JSP頁面文本框輸入評論內容,點擊發表按鈕,觸發onclick事件,通過JS獲取頁面傳值,結合post URL地址,通過Struts、Spring配置文件訪問後臺com.microxt.fusion.web.FusionAction類中的saveReview方法,並獲取前臺傳值;之後訪問接口中的public void saveScheLogReview(long scheLogId, long userId, String username,String plnr, String num);方法,訪問接口實現類FusionBizImpl中的public
void saveScheLogReview(long scheLogId, long userId, String username,
String plnr, String num)方法,在該業務實現類中只需進行簡單的業務判斷,調用LogBizImpl業務類中的public void saveReview(String review,Long userId,String userName,Long logId)方法,即可實現日誌評論發表功能;
關鍵點是在MVC框架下,FusionBizImpl類中如何能夠訪問LogBizImpl類中的方法呢:
1、在FusionBizImpl類中引入LogBizImpl類
import com.microxt.log.biz.impl.LogBizImpl;
2、在FusionBizImpl類中引入LogBizImpl類中定義LogBizImpl類變量
private LogBizImpl logBizImpl;
3、在FusionBizImpl類中定義logBizImpl變量的get、set方法
/**
* @return the logBizImpl
*/
public LogBizImpl getLogBizImpl() {
return logBizImpl;
}
/**用於Spring中注入LogBizImpl實例
* @param logBizImpl the logBizImpl to set
*/
public void setLogBizImpl(LogBizImpl logBizImpl) {
this.logBizImpl = logBizImpl;
}
4、在Spring配置文件中的Java bean的 com.microxt.fusion.biz.impl.FusionBizImpl類中注入LogBizImpl類實例
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 日誌模塊配置 -->
<bean id="fusionDao" class="com.microxt.fusion.dao.FusionDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="fusionLogDao" class="com.microxt.fusion.dao.FusionLogDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="fusionScheDao" class="com.microxt.fusion.dao.FusionScheDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!--在FusionBizImpl中注入fusionLogDao中的方法,就是可以調用 fusionLogDao中的方法 -->
<bean id="fusionBiz" class="com.microxt.fusion.biz.impl.FusionBizImpl">
<property name="hibernateDao"> <!--當前類中的屬性 -->
<ref bean="fusionDao" /> <!--所要注入類的 bean 的 id值 即:FusionBizImpl類可以調用FusionDao類中的方法 -->
</property>
<property name="fusionLogDao">
<ref bean="fusionLogDao" />
</property>
<property name="fusionScheDao">
<ref bean="fusionScheDao" />
</property>
<property name="fusionLogReviewDao">
<ref bean="logReviewDao"/>
</property>
<property name="logBizImpl">
<ref bean="logBiz" />
</property>
</bean>
<bean id="fusionAction" class="com.microxt.fusion.web.FusionAction"
scope="prototype">
<property name="baseBiz" ref="fusionBiz" />
</bean>
</beans>
其中name的屬性值logBizImpl爲在FusionBizImpl類中定義的LogBizImpl類變量實例,bean的屬性值logBiz爲LogBizImpl類對應的Spring配置文件中的bean id,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 日誌模塊配置 -->
<bean id="logDao" class="com.microxt.log.dao.LogDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="logReviewDao" class="com.microxt.log.dao.LogReviewDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="logTransmitDao" class="com.microxt.log.dao.LogTransmitDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="logBiz" class="com.microxt.log.biz.impl.LogBizImpl">
<property name="hibernateDao">
<ref bean="logDao" />
</property>
<property name="logReviewDao">
<ref bean="logReviewDao" />
</property>
<property name="logTransmitDao">
<ref bean="logTransmitDao" />
</property>
<property name="userBiz" ref="userBiz" />
</bean>
<bean id="logAction" class="com.microxt.log.web.LogAction"
scope="prototype">
<property name="baseBiz" ref="logBiz" />
</bean>
</beans>
至於底層的內容,見下章分享。