浏览 3740 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-07-19
最近有个项目改造,经过一番讨论后决定使用struts1,为什么不是struts2,呵呵,不是由我一个人说了算了,因为使用习惯了struts2,回头再来用struts1,感觉非常不爽,所以打 算对其进行改造一番,在网上也搜了一些关于struts1改造,都只是支持了Annotation配置和从IOC中取Action,我的改造目标在此之上加了一些,主要有: 1.支持基于Annotation的配置 2.不使用ActionForm但不去除它 3.支持Action方法参数值注入 4.简化文件上传处理 5.自定义返回结果处理如:直接返回string、Json、或者导出Excel 6.支持url到actionmethod的映射,如/actionPath/actionMethod.do而不是/actionPath.do?cmd=actionMethod 废话不多说,先看效果代码: //定义action的入口URL @Action(path="/hello") //spring bean定义 @Component("hello") public class HelloAction { //领域数据对象 public static class Hello { private String say; public String getSay() { return say; } public void setSay(String say) { this.say = say; } public String getName() { return name; } public void setName(String name) { this.name = name; } private String name; public String toString() { return String.format("%s:'Hello,%s'", say,name); } } //如果一个方法想作为一个外部接口,必须定义@ActionMethod,此配置有两个属性: //path和resultType,path表示对外部的访问接口,不定义则直接使用方法名,resultType表示 //对返回的对象根据设置的结果进行任意处理 //returnType="string",表示对返回的任何对象调用toString直接写到响应流中去 @ActionMethod(resultType="string") public String say(String name) { return String.format("<h2>hello,%s</h2>", name); } //resultType=json表示对返回的对象以json格式的字符串写进响应流 @ActionMethod(resultType="json") public List test(@Param("a")//表示从request取参数名为a的参数进行转型,转型使用commons工具实现,如果没有定义@param,则取参数的参数名则直接使用方法定义的形参名,对于Request,Response,actionForm,actionMapping属于默认处理类型,不需要定义@Param List<String> aa,@Param("b")List<Integer> bb){ List all = Lists.newList(); all.addAll(aa); all.addAll(bb); return all; } @ActionMethod(resultType="string") public Hello go(@Param("..")Hello hello) { return hello; } @ActionMethod() //文件上传直接使用commons的FileItem,Struts的文件上传不支持对同参数名的多个文件上传 public String upload(List<FileItem> test,ExecuteContext ctx) { ctx.storeObject("files",Lists.toArray(test), Scope.REQUEST); return "/upload"; } } 主要就是继承RequestProcessor对创建Action、ActionMapping和ActionExecute这三个步骤进行改造,至于怎么改造看提交的代码啦! 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-07-21
希望楼主讲解一下改造过程。不是光看成果啊。
|
|
返回顶楼 | |
发表时间:2010-07-22
这个..不就成了spring mvc了麽...
|
|
返回顶楼 | |
发表时间:2010-07-22
有启发。。
|
|
返回顶楼 | |
发表时间:2010-07-22
看了挺有些触动, 没想到struts1也能这样用,感觉跟struts2都象了。学习下lz的源码。
|
|
返回顶楼 | |
发表时间:2010-07-22
好,一种优雅的解决方案,支持!
|
|
返回顶楼 | |
发表时间:2010-07-23
我像问楼主这还是struts1么?
你们组决定用struts1,可能是因为项目组大多数人都熟悉struts1吧。你现在改成这样了,那还不如直接叫他们学spring mvc,还更规范。 |
|
返回顶楼 | |