背景:
产品一期已经上线,二期必然有很多改动,其中有一处是在原有工作流中引入新的工作流,说白了就是在原先做好某个页面上加入一块新的内容,通常你会去修改你的原先的 service使得其多返回一块数据,或者改变action的内容使其调用某个新的service(这样做不好),如果改变频繁,你会不断的修改以前的东西,其实完全可以拦截器来隔离关注点,这样的话你的修改不会干扰的以前的内容,如果你的修改的东西下一期又不要了,也不要紧,在配制文件中去掉这个拦截器就行了
废话说了一堆,进入实战,看看方案一
step 1)写ListTopVideosContributedIntercept类
public class ListTopVideosContributedIntercept implements Interceptor {
。。。。。。
public String intercept(ActionInvocation invocation) throws Exception {
// 获取service
TopVideosContributedBySelfService topVideosContributedBySelfService = (TopVideosContributedBySelfService) SpringBeanProxy
.getBean("topVideosContributedBySelfService");
// 获取形参1
HttpServletRequest request = ServletActionContext.getRequest();
String userNo = request.getParameter("userNo");
// 调用service
List list = topVideosContributedBySelfService.getTopnVideos(userNo);
// 结果放入值栈
OgnlValueStack stack = ServletActionContext.getContext()
.getValueStack();
stack.setValue("topVideosContributedList", list);
// 返回
return invocation.invoke();
}
。。。。。
}
step2)改变以前的action
加入以下内容:
private List topVideosContributedList;
/**
*
* @author weip
* @time 2006-6-8 9:56:08
* @return List
*/
public List getTopVideosContributedList() {
return topVideosContributedList;
}
/**
*
* @author weip
* @time 2006-6-8 9:56:12
* @param topVideosContributedList List
*/
public void setTopVideosContributedList(List topVideosContributedList) {
this.topVideosContributedList = topVideosContributedList;
}
主要是加入一个属性,以便注入数据
3)修改配置文件
<action name="tvuserinfo" class="moxtv.central.web.action.tvuserinfo.TVUserInfoAction">
<result name="success" type="freemarker">/cn/tvuserinfo/tvuserinfo.ftl</result>
<result name="error" type="freemarker">/cn/tvuserinfo/tvuserinfo_error.ftl</result>
<interceptor-ref name="defaultWebStack" />
<interceptor-ref name="listtopcontribitors" />
<interceptor-ref name="listtopvideoscontributed" />
</action>
加入红色的那一行
这样的话就直接通过拦截器实现了数据的表现,不会干扰以前的内容,有很好的即插即用效果,但有一点不好
需要改变原有的action以实现注入,虽然是很小的表现改动,但还是不爽,如何能做到更加完美的插件效果,看方案下集
产品一期已经上线,二期必然有很多改动,其中有一处是在原有工作流中引入新的工作流,说白了就是在原先做好某个页面上加入一块新的内容,通常你会去修改你的原先的 service使得其多返回一块数据,或者改变action的内容使其调用某个新的service(这样做不好),如果改变频繁,你会不断的修改以前的东西,其实完全可以拦截器来隔离关注点,这样的话你的修改不会干扰的以前的内容,如果你的修改的东西下一期又不要了,也不要紧,在配制文件中去掉这个拦截器就行了
废话说了一堆,进入实战,看看方案一
step 1)写ListTopVideosContributedIntercept类
public class ListTopVideosContributedIntercept implements Interceptor {
。。。。。。
public String intercept(ActionInvocation invocation) throws Exception {
// 获取service
TopVideosContributedBySelfService topVideosContributedBySelfService = (TopVideosContributedBySelfService) SpringBeanProxy
.getBean("topVideosContributedBySelfService");
// 获取形参1
HttpServletRequest request = ServletActionContext.getRequest();
String userNo = request.getParameter("userNo");
// 调用service
List list = topVideosContributedBySelfService.getTopnVideos(userNo);
// 结果放入值栈
OgnlValueStack stack = ServletActionContext.getContext()
.getValueStack();
stack.setValue("topVideosContributedList", list);
// 返回
return invocation.invoke();
}
。。。。。
}
step2)改变以前的action
加入以下内容:
private List topVideosContributedList;
/**
*
* @author weip
* @time 2006-6-8 9:56:08
* @return List
*/
public List getTopVideosContributedList() {
return topVideosContributedList;
}
/**
*
* @author weip
* @time 2006-6-8 9:56:12
* @param topVideosContributedList List
*/
public void setTopVideosContributedList(List topVideosContributedList) {
this.topVideosContributedList = topVideosContributedList;
}
主要是加入一个属性,以便注入数据
3)修改配置文件
<action name="tvuserinfo" class="moxtv.central.web.action.tvuserinfo.TVUserInfoAction">
<result name="success" type="freemarker">/cn/tvuserinfo/tvuserinfo.ftl</result>
<result name="error" type="freemarker">/cn/tvuserinfo/tvuserinfo_error.ftl</result>
<interceptor-ref name="defaultWebStack" />
<interceptor-ref name="listtopcontribitors" />
<interceptor-ref name="listtopvideoscontributed" />
</action>
加入红色的那一行
这样的话就直接通过拦截器实现了数据的表现,不会干扰以前的内容,有很好的即插即用效果,但有一点不好
需要改变原有的action以实现注入,虽然是很小的表现改动,但还是不爽,如何能做到更加完美的插件效果,看方案下集