- 浏览: 2998381 次
- 性别:
- 来自: 上海
-
文章分类
- 全部博客 (2529)
- finance (1459)
- technology (218)
- life (343)
- play (150)
- technology-component (0)
- idea (6)
- house (74)
- health (75)
- work (32)
- joke (23)
- blog (1)
- amazing (13)
- important (22)
- study (13)
- Alternative (0)
- funny (8)
- stock_technology (12)
- business (16)
- car (21)
- decorate (4)
- basketball (2)
- English (16)
- banker (1)
- TheBest (1)
- sample (2)
- love (13)
- management (4)
最新评论
-
zhongmin2012:
BSM确实需要实践,标准ITIL服务流程支持,要做好,需要花费 ...
BSM实施之前做什么 -
shw340518:
提示楼主,有时间逻辑bug:是你妈二十那年写的 那会儿连你爹都 ...
80后辣妈给未来儿子的信~我的儿,你也给我记住了~~~ -
guoapeng:
有相关的文档吗?
it项目管理表格(包含146个DOC文档模板) -
solomon:
看到的都是 这种 CTRL+C 和 CTRL+V 的文章, ...
Designing a website with InfoGlue components -
wendal:
恩, 不错. 有参考价值
Designing a website with InfoGlue components
JpetStore中的Action与普通Struts的Action处理方式不一样。遍历JpetStore的src文件夹,并无一个具体的Action,那么它是如何来完成普通Struts的Action工作了?
查看JpetStore的Struts.xml可以发现,它的Action只有一个,即“org.apache.stuts.beanaction.Beanaction”。通过Eclipse查看beanaction.jar的源代码,可以发现Beanaction继承与普通Action,即具备普通的action的功能。那么它无具体Action的奥妙在哪,继续研究BeanAction的代码,截取BeanAction的excute方法中核心部分代码如下:
private static final String NO_METHOD_CALL = "*";
…….
BaseBean bean = (BaseBean) form;
ActionContext.initCurrentContext(request, response);
if (bean != null) {
// Explicit Method Mapping
Method method = null;
String methodName = mapping.getParameter();
if (methodName != null && !NO_METHOD_CALL.equals(methodName)) {
try {
method = bean.getClass().getMethod(methodName, null);
synchronized (bean) {
forward = bean.getInterceptor().intercept(new ActionInvoker(bean, method));
}
……..
// Path Based Method Mapping
if (method == null && !NO_METHOD_CALL.equals(methodName)) {
methodName = mapping.getPath();
if (methodName.length() > 1) {
int slash = methodName.lastIndexOf("/") + 1;
methodName = methodName.substring(slash);
if (methodName.length() > 0) {
try {
method = bean.getClass().getMethod(methodName, null);
synchronized (bean) {
forward = bean.getInterceptor().intercept(new ActionInvoker(bean, method));
}
……..
return mapping.findForward(forward);
通过研究上面这段代码,我们可知,JpetStore中没有具体Action实现的关键原因即在于下面这几句
method = bean.getClass().getMethod(methodName, null);
synchronized (bean) {
forward = bean.getInterceptor().intercept(new ActionInvoker(bean, method));
}
即将原来Action中的excute方法的实现转移到FormBean中,这样实现显得更为简捷,方便。研究ActionInvoke,它的核心代码如下:
以上是整个beanaction的实现机制。个人感觉此种实现方法对于开发者而言已经类似于ASP.NET的.aspx与.cs开发模式了。下面是通过实例来说明一下BeanAction如何控制formbean的
Struts-config.xml的配置里有3种映射方式,来告诉BeanAction把控制转到哪个form bean对象的哪个方法来处理。
(1)parameter=”*’直接跳转;(2)Parameter中含具体的方法名;(3)Path中最后一个/后的方法名
以这个请求连接为例http://localhost/jpetstore4/shop/viewOrder.shtml
1. URL Pattern
此种方式表示,控制将被转发到"orderBean"这个form bean对象的"viewOrder"方法(行为)来处理。方法名取"path"参数的以"/"分隔的最后一部分。
2. Method Parameter
此种方式表示,控制将被转发到"orderBean"这个form bean对象的"viewOrder"方法(行为)来处理。配置中的"parameter"参数表示form bean类上的方法。"parameter"参数优先于"path"参数。
3. No Method call
此种方式表示,form bean上没有任何方法被调用。如果存在"name"属性,则struts把表单参数等数据填充到form bean对象后,把控制转发到"success"。否则,如果name为空,则直接转发控制到"success"。
这就相当于struts内置的org.apache.struts.actions.ForwardAction的功能
查看JpetStore的Struts.xml可以发现,它的Action只有一个,即“org.apache.stuts.beanaction.Beanaction”。通过Eclipse查看beanaction.jar的源代码,可以发现Beanaction继承与普通Action,即具备普通的action的功能。那么它无具体Action的奥妙在哪,继续研究BeanAction的代码,截取BeanAction的excute方法中核心部分代码如下:
private static final String NO_METHOD_CALL = "*";
…….
BaseBean bean = (BaseBean) form;
ActionContext.initCurrentContext(request, response);
if (bean != null) {
// Explicit Method Mapping
Method method = null;
String methodName = mapping.getParameter();
if (methodName != null && !NO_METHOD_CALL.equals(methodName)) {
try {
method = bean.getClass().getMethod(methodName, null);
synchronized (bean) {
forward = bean.getInterceptor().intercept(new ActionInvoker(bean, method));
}
……..
// Path Based Method Mapping
if (method == null && !NO_METHOD_CALL.equals(methodName)) {
methodName = mapping.getPath();
if (methodName.length() > 1) {
int slash = methodName.lastIndexOf("/") + 1;
methodName = methodName.substring(slash);
if (methodName.length() > 0) {
try {
method = bean.getClass().getMethod(methodName, null);
synchronized (bean) {
forward = bean.getInterceptor().intercept(new ActionInvoker(bean, method));
}
……..
return mapping.findForward(forward);
通过研究上面这段代码,我们可知,JpetStore中没有具体Action实现的关键原因即在于下面这几句
method = bean.getClass().getMethod(methodName, null);
synchronized (bean) {
forward = bean.getInterceptor().intercept(new ActionInvoker(bean, method));
}
public String invoke() {
try {
return (String) method.invoke(bean, null);
} catch (Exception e) {
throw new BeanActionException("Error invoking Action. Cause: " + e, e);
}
}
至此可知,它调用的是formbean中的函数。且从这段代码可知,formbean的这类特殊函数,此处称为action方法,要符合两个特征:1)无参数;2)返回值为string,此返回string即是Struts-config.xml的全局或局部的forward。 try {
return (String) method.invoke(bean, null);
} catch (Exception e) {
throw new BeanActionException("Error invoking Action. Cause: " + e, e);
}
}
以上是整个beanaction的实现机制。个人感觉此种实现方法对于开发者而言已经类似于ASP.NET的.aspx与.cs开发模式了。下面是通过实例来说明一下BeanAction如何控制formbean的
Struts-config.xml的配置里有3种映射方式,来告诉BeanAction把控制转到哪个form bean对象的哪个方法来处理。
(1)parameter=”*’直接跳转;(2)Parameter中含具体的方法名;(3)Path中最后一个/后的方法名
以这个请求连接为例http://localhost/jpetstore4/shop/viewOrder.shtml
1. URL Pattern
<action path="/shop/viewOrder" type="com.ibatis.struts.BeanAction"
name="orderBean" scope="session"
validate="false">
<forward name="success" path="/order/ViewOrder.jsp"/>
</action>
name="orderBean" scope="session"
validate="false">
<forward name="success" path="/order/ViewOrder.jsp"/>
</action>
此种方式表示,控制将被转发到"orderBean"这个form bean对象的"viewOrder"方法(行为)来处理。方法名取"path"参数的以"/"分隔的最后一部分。
2. Method Parameter
<action path="/shop/viewOrder" type="com.ibatis.struts.BeanAction"
name="orderBean" parameter="viewOrder" scope="session"
validate="false">
<forward name="success" path="/order/ViewOrder.jsp"/>
</action>
name="orderBean" parameter="viewOrder" scope="session"
validate="false">
<forward name="success" path="/order/ViewOrder.jsp"/>
</action>
此种方式表示,控制将被转发到"orderBean"这个form bean对象的"viewOrder"方法(行为)来处理。配置中的"parameter"参数表示form bean类上的方法。"parameter"参数优先于"path"参数。
3. No Method call
<action path="/shop/viewOrder" type="com.ibatis.struts.BeanAction"
name="orderBean" parameter="*" scope="session"
validate="false">
<forward name="success" path="/order/ViewOrder.jsp"/>
</action>
name="orderBean" parameter="*" scope="session"
validate="false">
<forward name="success" path="/order/ViewOrder.jsp"/>
</action>
此种方式表示,form bean上没有任何方法被调用。如果存在"name"属性,则struts把表单参数等数据填充到form bean对象后,把控制转发到"success"。否则,如果name为空,则直接转发控制到"success"。
这就相当于struts内置的org.apache.struts.actions.ForwardAction的功能
<action path="/shop/viewOrder" type="org.apache.struts.actions.ForwardAction"
parameter="/order/ViewOrder.jsp " scope="session" validate="false"> </action> |
发表评论
-
New Enterprise Security Solutions
2011-09-13 15:46 0<!-- [if !mso]> <styl ... -
ES Announces Enterprise Security Solutions
2011-09-13 15:40 0<!-- [if !mso]> <styl ... -
linux下如何将文件打包、压缩并分割成制定大小?
2010-09-15 18:52 3330将大文件或目录打包、 ... -
rhel4 yum安装, 使用
2010-09-07 16:37 0第一种方法: yum源来自chinalinuxpub.com ... -
Windows: 远程自动安装程序
2010-08-26 15:48 1133问题的提出 作为 ... -
Oracle体系结构
2010-08-07 09:53 1073Oracle体系结构 Oracle Server包括Oracl ... -
ocp sesson 3
2010-07-31 14:39 0show parameter undo 只有 默认情况下服务 ... -
ocp session 2
2010-07-25 17:00 0/home/oracle/raInventory/orains ... -
ocp session 1
2010-07-24 13:02 0ocp first lesson D:\oracle_cou ... -
Python的xmlrpc调试
2010-07-19 23:55 2177Python的xmlrpc 调 试 ----------- ... -
mdadm使用详解及RAID 5简单分析
2010-07-11 16:19 1429http://blog.csdn.net/chinalinux ... -
Linux的lvm的基本配置步骤
2010-07-11 14:53 13181.增加硬件 增加的ide硬盘前缀为hd,scs ... -
OCP study material
2010-07-11 13:52 0\\192.168.1.105watch -n 1 'stat ... -
apache+python+mod_python+django 编译安装指南
2010-06-24 17:25 14931、本文将知道你在 linux 下使用源码包安装 ... -
在ubuntu下配置apache运行python脚本
2010-06-22 16:11 2310常用的简单命令 sudo apt ... -
Python 2.5 Quick Reference
2010-06-21 11:18 1498... -
shell 面试题汇集
2010-06-10 19:50 1110利用 top 取某个进程的 CPU 的脚本 : ... -
shell程序面试题
2010-06-10 19:48 29911.要求分析Apache访问日志,找出里面数量在前面100位的 ... -
EMC技术支持工程师笔试部分试题回忆
2010-06-07 15:16 1680要查看更多EMC公司笔经相关信息,请访问EMC公司校园招聘CL ... -
linux shell 条件语句
2010-06-03 23:29 1829...
相关推荐
在jpetstore4.0中,iBatis主要负责: 1. **SQL动态生成**:iBatis允许根据传入的参数动态生成SQL语句,避免硬编码SQL,提高了代码的可读性和可维护性。 2. **数据访问抽象**:通过配置文件,iBatis将数据库操作与...
iBatis(现称MyBatis)是一个半自动化的ORM(对象关系映射)框架,它在jpetstore系统中用于处理数据持久化操作。通过DaoManager创建的SqlMapDao实例,系统能够高效地执行SQL语句,与数据库进行交互。此外,...
在这个例子中,你可以看到如何使用Spring配置文件管理Bean,定义Bean之间的依赖关系,以及如何在Struts Action中通过@Autowired注解自动注入依赖。同时,iBatis的SQL映射文件展示了如何编写动态SQL和结果映射,以...
通过整合Struts、Spring与ibatis,JpetStore 4.0不仅展现了ibatis在数据持久化方面的高效与灵活性,也展示了Struts与Spring在构建高效、可维护的J2EE应用程序中的协同优势。这种模式下的开发,不仅减少了代码量,...
然而,传统的Struts开发模式可能会导致过多的Action类和Form Bean,JpetStore 4.0则采取了创新的方式,仅使用一个自定义Action类和简化form bean的定义,降低了复杂性。 Spring Framework的核心功能包括AOP(面向切...
在架构上,JpetStore 4.0的核心特点是只有一个通用的BeanAction类,它通过反射机制根据请求URL调用对应的Form Bean方法。这种方式降低了对Struts的依赖,使得代码更简洁,同时也便于维护和扩展。 总体来说,Struts+...