ModelDriven利用泛型实现多个Action得到不同的getModel类
来源:互联网 发布:淘宝每时每刻沙特代购 编辑:程序博客网 时间:2024/06/04 18:56
public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {Class<T> clazz;T t;public BaseAction() {super(); ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass(); Type[] types = parameterizedType.getActualTypeArguments(); clazz = (Class<T>) types[0]; try { t=clazz.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); }}@SuppressWarnings("unchecked")@Overridepublic T getModel(){ return t;}}
注意t是创建的类,而clazz是Class<T>这是不一样的,以后创建的action只要继承BaseAction
就可以使用封装好的t了
如下
public class DepartmentAction extends BaseAction<Department> {private DepartmentService departmentService;public DepartmentService getDepartmentService() {return departmentService;}@Resource(name="departmentServiceImpl")public void setDepartmentService(DepartmentService departmentService) {this.departmentService = departmentService;}public String add(){departmentService.save(t);return "toList";}}
注意不要把T 和Class<T>混淆,否则会出现save(Class<?>)不是save(T t)这样的错误
- ModelDriven利用泛型实现多个Action得到不同的getModel类
- ModelDriven利用泛型实现多个Action得到不同的getModel类
- modeldriven<>和getModel的作用
- 利用ajax实现多个按钮提交给不同action
- JFinal的getModel实现方式
- struts2.0框架中的action类实现ModelDriven的作用是什么?
- Action实现ModelDriven,不能实例化所需类的问题
- Action实现ModelDriven接口后的运行流程
- JS实现一个表单form,多个submit按钮提交时带不同的action参数
- 利用LocalConnection实现多个应用之间的通讯 (不同MXML之间的通信)
- JAVA_WEB项目之Action层利用ModelDriven抽取BaseAction
- 实现ModelDriven接口的好处
- Form实现提交到不同的action
- 在一个form表单中根据不同按钮实现多个action事件
- Form 表单实现多个Action 请求,将表单数据提交到不同请求
- 利用元祖返回多个不同的对象
- 利用元祖返回多个不同的对象
- 用一条SQL语句查询一张表得到不同条件的多个结果
- Inserting Sort (插入排序)
- 2013年目标
- Avro总结(RPC/序列化)
- java的异常处理
- 免费天气API
- ModelDriven利用泛型实现多个Action得到不同的getModel类
- jobdu 2 成绩排序
- js在IE和Firefox下不同之处
- 中级9 Be thoughtful
- ASP.NET中使用水晶报表
- SQL Server 智能感知IntelliSense 不起作用
- javascript打开选择对话框/保存对话框
- 纵向三级导航
- 基于openswan klips的IPsec VPN实现分析(十)认证算法维护