以微协同项目的日志评论发表为例,如图所示:
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>
至于底层的内容,见下章分享。