让webwork2零配置,第一章(主贴再次更新)
/**
*作者:张荣华(ahuaxuan)
*2007-06-18
*转载请注明出处及作者
*/
让webwork2零配置,第一章
一直以来我都有一个想法,想要找一个比较好的web框架,不用jsp,不用繁琐的配置,比如说struts1.x的action的配置,webwork2的action的配置,其他框架我没有用过,但是类似的,都有很多这样的配置,一个很大的项目,struts的配置文件都是上w,上十几w行,当然我早已放弃struts,投向webwork2.2的怀抱,虽然没有了form的配置,action的配置也比struts的简化了很多。但是我还是不满足,我想要的框架应该比这个还要简单,而action的配置应该抛弃,xwork.xml中应该只存放一些common的配置,比如说interceptor,自定义的result等等。也许你要说你也可以扩展struts1.x啊,而且他的用户更多,但是struts给我感觉是他马上就要退出历史的舞台了,这都是由于他本身的缺点导致的,比如说ActionForm,他把我们绑定到jsp上,如果用模板,那么我们就要自己组装pojo了,这一点,有了拦截器的webwork2做得很好,这也是我选他的原因,同样,我也没有选择struts2.0,因为我讨厌它的庞大和笨重,呵呵。
那么你也许要问,我action中返回页面如何指定呢,我怎么知道我要返回到那个页面呢,我的想法是coc,使用固定的规则能够使我们省去不少繁琐的配置,虽然说配置能够给我们带来巨大的灵活性,但是配置也给我们带来了巨大的不可维护性(这里是指维护起来很繁琐)。
以下是我想象中的做法(我强烈建议在view层使用模板技术,velocity或者freemarker):
1,每个action的页面都单独放置,路径和Action的package类似,只是在Action的那一级目录创建一个和Action同名的目录用来放这个Action中方法所返回的页面。比如说有一个UserAction,package:org.easywebwork.action.UserAction,那么就在这个package下创建一个UserAction目录,这个目录下存放所有的UserAction返回的页面。
2,Action的方法返回的字符串在annotation中指定,比如:@result name=”success” template=”editUser.htm” type=”redirect”,editUser.html就是UserAction目录下的一个页面模板
说到底还是得用规则和注释来实现这个功能,这样做的一个优点是方法的返回页面一眼就能看出来,根本不用去xml中找了,尤其在Action类很多的情况下,这样做优点更是明确了。
那最终的效果我想应该是这样的,http://localhost:8080/test/userAction!editUser,就能触发上面这个方法,之后根据editUser的注释就能返回对应的模板页面了。
思路定下来了,那么就是实现了,要实现这个方案的第一步就是自定义annotation,关于annotation,已经属于java基初知识了,java engineer迟早都需要学习的,任何一本关于jdk5.0的书上都有详细的解释,而且论坛上也能搜出一堆,固不作重复的解释,让我们来直接定义我们需要的annotation吧。
既然我们定义的是result,那么我们的annotation元素的个数和类型就应该和xwork.xml中的result的元素的个数和类型是一样的:
/** * @author 张荣华(aaron) * @since 2007-6-17 * @version $Id$ *///这个annotation是用在方法级别的@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME) public @interface Result {//action方法的返回名称String name();//result类型,如果没有就是默认值, 如果使用模板那么就是velocity或者freemarkerString result() default "velocity";//默认值使用velocity,正如前面所说,我强烈推荐在view层使用模板引擎//模板的名字String template();//是哪些返回类型,比如说redirect, chain等等String type() default "dispatcher";}那么接下来就是定义一个ResultList的annotation了,因为action中的每一个方法都可以有多个返回的result:/** * @author 张荣华(aaron) * @since 2007-6-17 * @version $Id$ *///这个annotation是用在方法级别的@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME) public @interface ResultList {//这里存放的是每个方法可能拥有的多个resultResult [] results();}/** * @author 张荣华(aaron) * @since 2007-6-17 * @version $Id$ */public class UserAction {/* * 注意: 这里有一个复合annotation,当然,如果你理解了annotation的用法,及如何自定义annotation, * 那么复合annotation对你来说也是小菜一碟了 */@ResultList(results = {@Result(name="SUCCESS", template="editUser.htm"), @Result(name="ERROR", template="error.htm")}) //这个uploadInterceptor是在xwork.xml文件中声明的,我还是坚持把common的东西放到xml中去 @Interceptor(interceptors = {"uploadInterceptor"})public String editUser(){System.out.println("edit user info");return "SUCCESS";}@ResultList(results = {@Result(name="SUCCESS", template="success.htm"),@Result(name="CANCEL", template="editUser.htm")})public String saveUser(){System.out.println("save user info");return "SUCCESS";}}这个类就代表一个webwork2的action,再让我们来写一个测试类:public class UserActionTest {public static void main(String args[]) throws ClassNotFoundException {UserAction action = new UserAction();Method[] methods = action.getClass().getDeclaredMethods();Set<Method> set = new HashSet<Method>();for (int i = 0; i < methods.length; i++) {if (methods[i].isAnnotationPresent(ResultList.class)) {set.add(methods[i]);}}for (Method m : set) {ResultList list = m.getAnnotation(ResultList.class);for (Result s : list.results()) {System.out.println(s.name() +" "+ s.template() +" "+ s.result() +" "+ s.type());}}}}Run一下这个test类的main方法,我们可以看到输出的内容为SUCCESS editUser.htm velocity dispatcherERROR error.htm velocity dispatcherSUCCESS success.htm velocity dispatcherCANCEL editUser.htm velocity dispatcher
/** * @author 张荣华(aaron) * @since 2007-6-19 * @version $Id$ *///这个annotation是用在方法级别的@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME) public @interface Interceptor {//在这里设置的interceptor就是相当于<interceptor-ref/>元素String[] interceptors();}/** * @author 张荣华(aaron) * @since 2007-6-19 * @version $Id$ *///这个annotation是用在方法级别的@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME) public @interface Action {/* * 这样一来,配置就想xwork.xml中的配置了,如果要实现downpour所说的用法, * 就可以用这个annotation实现了。 */ResultList resultList();Interceptor interceptors();}