再改struts1.2.*,让struts1再焕发表春,支持Annotation和POJO风格的Action

最近有个项目改造,经过一番讨论后决定使用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这三个步骤进行改造,至于怎么改造看提交的代码啦!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值