by agate - Published: 2008-05-01 [9:42 下午] - Category: 程序编码

不知道各位使用 Struts1.x 的朋友们晓得不晓得,Struts1.x 的 org.apache.struts.action.Action 中有两个 execute 方法,在我们使用 eclipse 的自动完成 override 功能的时候要是不小心给弄错了你就等着迎接一个不报错的空白页面吧!让我们看看代码:

// one execute
public ActionForward execute(ActionMapping mapping, ActionForm form,
	HttpServletRequest request, HttpServletResponse response) {
	//code...
}
// another execute
public ActionForward execute(ActionMapping mapping, ActionForm form,
	ServletRequest request, ServletResponse response) {
	//code...
}

好,公布结果!只有 override 上面这个 execute 才能起作用。如果你 override 的是下面这个 execute 的话,很不幸,您调用这个 action 的时候响应给你的是一个空白的页面,你也别想得到任何 exception 的提示!

在 eclipse 中我导入了 struts1.x 的 src 路径,并通过 Open Call Hierarchy 查找调用上面第二个 execute 的类时发现竟然没有调用者!换句话说当我们实现第二个 execute 的时候(没有实现第一个 execute ),是根本没用的!程序根本不会调用到我们 override 的那个 execute 只会傻傻地调用第一个 execute 的默认实现,返回一个 null

那第二个 execute 有什么用呢?我觉得他不是用来给我们重写的,看看它的内容:

public ActionForward execute(ActionMapping mapping, ActionForm form,
	ServletRequest request, ServletResponse response)
	throws Exception {
	try {
		return execute(mapping, form,
			(HttpServletRequest) request,
			(HttpServletResponse) response);
	} catch (ClassCastException e) {
		return null;
	}
}

在我看来,其实它是为了当一个请求是一个非 http 请求的时候,作为一个前端转换器,重新包装请求和响应,然后才交给真正的,也就是我们的第一个 execute 方法来实现。这个从 src 的注释中我们也可以比较清晰的了解:

Process the specified non-HTTP request, and create the corresponding non-HTTP response (or forward to another web component that will create it), with provision for handling exceptions thrown by the business logic. Return an ActionForward instance describing where and how control should be forwarded, or null if the response has already been completed.

所以……当你重写这个 action 的 execute 方法时,注意咯!是重写那个参数是 http-request/response 的 execute哦!!!

这里严重鄙视一下 struts1.x 的编码态度!

Tags: [ , , , , , ] - Comments: View Comments
by agate - Published: 2008-04-08 [2:12 下午] - Category: 程序编码

最近使用struts1.x进行项目开发,发现虽然struts2的设计让功能更为强大,但是struts1.x的开发更好定制!完全servlet体系的扩展,虽然有耦合的嫌疑吧,但是让很多从Servlet/JSP开发转移过来的朋友大概会有很大的亲切感。
以下是我得出的Struts1 + Spring + Hibernate整合开发的一些建议:

1.所有页面都用struts来转发!不能直接访问到资源页面。把所有资源页面放在WEB-INF\pages下

2.虽然很多文档中似乎可以不用在struts-config.xml中以plugin的方式启用spring托管【在web.xml中声明监听器】,但是加载spring框架方式使用struts-plugin的这种老式的方式保守。

3.对此!我们的action的type通通改成org.springframework.web.struts.DelegatingActionProxy这个代理类!交给spring动态获取这个action实例!
对此必须设定对应action的bean配置,此bean的并非配置id标示属性,改为配置name属性其值为struts的action的path值。

4.使用ActionForm的validate方法来验证表单数据的合法性。用代码来控制,不使用xml配置。

5.显示页面部分,我们统一使用el表达式+struts标签形式!不使用scriptlet的脚本形式,保证页面的高可读性!

其他:有可能我们要使用到spring提供的open-session-view的过滤器,来处理持久层的东西,那么我们不得不吧spring的托管放在web.xml中因为这个过滤器要用到那个监听器……注意,事实证明这样是更为优秀的整合手段,可以放心使用!

还有的当然就是我们的编码问题咯!这是一个十分冗长的问题,需要时间来研究【特别是tomcat】的具体运作问题!接下来我会写一个文章来参数这个问题的

Tags: [ , , ] - Comments: View Comments