2007-09-03

在Struts2中使用Groovy来编写Action

关键字: struts2 groovy plugins
原文地址:http://www.blogjava.net/puras/archive/2007/09/03/142221.html

今天在查关于Struts2和Ajax方面的资料的时候,发现一个在Struts2中使用Groovy编写Action的插件.因为本身就对Groovy比较感兴趣,但因为最近的工作,没有多少时间来研究,看到这个插件,哈,正好可以在这段时间的应用中使用到,这样就可以在工作中应用Groovy啦.

这个插件是:s2ss(Struts 2 Scripting Support), 地址:http://code.google.com/p/s2ss/

下载地址:http://s2ss.googlecode.com/files/struts2-groovy-plugin-0.2.jar

将这个Jar包放到WEB-INF/lib下,之后修改struts.properties,添加一行:
[code] struts.objectFactory=groovyObjectFactory [/code]
也可以修改struts.xml,添加下面一行:
[code] <constant name="struts.objectFactory" value="groovyObjectFactory" /> [/code]
个人习惯使用后面一种.

再将Groovy的包放到WEB-INF/lib下,我用的是groovy-all-1.0.jar.

这个插件所依赖的包有:

    Struts 2.0.x (struts2-api-2.0.x.jar, struts2-core-2.0.x.jar)
    Groovy 1.0 (groovy-all-1.0.jar)
    XWork 2.x (xwork-2.0.x.jar)
    Commons Loggings 1.0 (commons-logging-1.0.4.jar)

在struts.xml中配置Action,与正常的配置差不多,只不过class里用的是Groovy的类,而且需要加上Groovy的扩展名,如下:
[code] <action name="groovy" class="com.puras.groovy.TestAction.groovy"> <result>/WEB-INF/pages/groovy/test_action.jsp</result> </action> [/code]
拦截器与Action相似,
[code] .. <interceptor name="groovyInterceptor" class="com.puras.groovy.Interceptor.groovy" /> .. <interceptor-ref name="groovyInterceptor" /> [/code[ 这里就只演示Action的了.呵 下面来编写TestAction.groovy: [code] package com.puras.groovy; class TestAction { def message; def execute() { message = "Hello world" return "success" } def getMessage() { return message } } [/code]
不用进行编译,直接将.groovy文件复制到WEB-INF/classes/com/puras/groovy/下就可以了.

类之间的调用等,与使用Java相同.

最后再编写一下用于显示的test_action.jsp就OK啦.代码如下:
[code] <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <s:property value="message"/> <br /> <hr /> </body> </html> [/code]
现在,启动你的WEB服务器,之后打开浏览器,输入地址:http://localhost:8080/your_webapp/groovy.action,就可以看到效果了.

下一步目标:

加入Spring
加入Ajax
评论
kyo100900 2007-10-15
Puras 写道
我觉得
1.使用Groovy,可以让代码更加简洁,如上例中的
# def getMessage() {
# return message
# }
是这可以省略的
2.响应速度更快
如果使用Java,改完之后会等WEB容器重新编译
而使用Groovy就不需要了,直接刷新页面就可以看到修改的结果了


我感觉第二点更诱人,Java布署一个项目,如果机器差的话,真的可以喝咖啡了。。。。。。。。。。
Puras 2007-09-04
我觉得
1.使用Groovy,可以让代码更加简洁,如上例中的
# def getMessage() {
# return message
# }
是这可以省略的
2.响应速度更快
如果使用Java,改完之后会等WEB容器重新编译
而使用Groovy就不需要了,直接刷新页面就可以看到修改的结果了
Fly_m 2007-09-03
学习ing.
最近也在研究怎么在项目中运用groovy和grails,还不太习惯~
也对struts2感兴趣,不过为什么grails中的web层是spring mvc?
山风小子 2007-09-03
不要光看到自己所失去的,想想自己所获得的吧~
agile_boy 2007-09-03
支持博主,大家把Groovy应用的越来越丰富多彩了啊!
至于大家关注的性能问题,应该不是大问题啊,众人拾材火焰高嘛 ,还有选择工具其实也是一个平衡过程
ray_linn 2007-09-03
groovy运行效率是个问号,一般都是用来写Test Case的。
williamy 2007-09-03
Groovy偶也有所研究,本来想在偶得框架里引进这个技术,不过最终老子放弃了它,其实Groovy还是运行前还是要编译的,并且编译出来的class文件实在有够烂,我认为groovy有3个用途,1 写testCase 2,写code gen。。 3,作为粘合剂代替xml,(这个我没有想到怎么用,估计可以,不过偶都是用annotation来代替xml的)
发表评论

提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则

您还没有登录,请登录后发表评论

Puras
搜索本博客
我的相册
41e24df7-43bc-3fa7-9e74-0b4a6e096290-thumb
图1-10
共 10 张
存档
最新评论