Java模仿Struts2模型驱动的实现
2013-09-20 21:21
393 查看
首先,我没有看过Struts2的源码,是自己写的。
我自己写的一个框架Dreamer,模仿Struts2的。看下Action配置
这个类和使用Struts2的Action是差不多的。但是我没有使用Struts2的任何包。都是自己实现的
具体的实现:
这里用到了一个类,普通表单的处理:
我自己写的一个框架Dreamer,模仿Struts2的。看下Action配置
package com.pan.actions; import org.dreamer.annotation.Action; import org.dreamer.annotation.Result; import org.dreamer.code.ModelDriven; import org.dreamer.support.ActionSupport; import com.deramer.vo.VUser; import com.dreamer.action.test.ITest; @Action(results = { @Result(path = "login", location = "/login.jsp"),@Result(path="user",location="/user.jsp") }) public class IndexAction extends ActionSupport implements ModelDriven<VUser>,ITest{ private VUser vUser=new VUser(); public String login() { actionContent.put("title", "用户登录"); System.out.println(vUser.getUsername()); return "login"; } public String user(){ return "user"; } public VUser getModel() { // TODO Auto-generated method stub return vUser; } }
这个类和使用Struts2的Action是差不多的。但是我没有使用Struts2的任何包。都是自己实现的
具体的实现:
这里用到了一个类,普通表单的处理:
package org.dreamer.action.dao.impl; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import javax.management.ReflectionException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.dreamer.action.dao.IActionForm; import org.dreamer.code.ModelDriven; import org.dreamer.exception.AbnormalException; import org.dreamer.exception.InitializeActionException; import org.dreamer.exception.ModelDrivenException; import org.dreamer.orm.convetor.FieldConvertor; import org.dreamer.support.ServletActionContent; import org.dreamer.util.InterfaceUtil; import org.dreamer.util.MethodsUtil; import org.dreamer.util.StringUtil; /** * 普通表单处理 * @author Pan * */ public class CommonForm implements IActionForm{ private HttpServletRequest request; //请求对象 private HttpServletResponse response; //响应对象 private Map<String, Object> map=new HashMap<String, Object>(); //map对象 private Object result; //返回类型 private String methodName; //需要操作的方法 private Object action; //用户的Action public CommonForm(HttpServletRequest request,HttpServletResponse response,Object action,String methodName){ this.request=request; this.response=response; this.action=action; this.methodName=methodName; } /** * 模型驱动处理 * @throws NoSuchMethodException * @throws SecurityException * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws ModelDrivenException */ private void modelDriven() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, ModelDrivenException{ //获取Action,检测是否实现了ModelDriven接口 Class<?> cls=this.action.getClass(); Boolean exits=InterfaceUtil.checkInterface(cls.getInterfaces(), ModelDriven.class); if(exits){ //调用方法 Method method=cls.getMethod("getModel", null); if(method!=null){ Object object=method.invoke(this.action, null); if(object==null){ throw new ModelDrivenException(); }else { initField(object); } } //initField(action); } } public Map getMap() { // TODO Auto-generated method stub return this.map; } public Object getResult() { // TODO Auto-generated method stub return result; } /** * 将请求的值放入map */ private void invokeMap(){ Iterator it=request.getParameterMap().entrySet().iterator(); while (it.hasNext()) { Map.Entry entry=(Map.Entry)it.next(); String key=entry.getKey().toString(); String value=StringUtil.getStringArray((String[])entry.getValue()); map.put(key, value); } } /** * 将相关对象设置到用户的Action中 * @throws AbnormalException * @throws Exception * @throws InitializeActionException * @throws NoSuchMethodException * @throws SecurityException * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */ public void Initialization() throws NullPointerException,ReflectionException,RuntimeException, AbnormalException{ //给对象设置字段参数 initField(action); //调用Action的方法 try{ this.result=invokeMethod(this.action); }catch (NullPointerException e) { StringBuffer sb=new StringBuffer(); sb.append(e.toString()+"\r\n"); sb.append("Initialize the user Action in the process of abnormal happened, please check the Action method for error!\r\n"); sb.append("Abnormal method:"+action.getClass().getName()+"."+this.methodName+"()"); throw new AbnormalException(sb.toString()); } catch (Exception e) { StringBuffer sb=new StringBuffer(); sb.append(e.toString()+"\r\n"); sb.append("Initialize the user Action in the process of abnormal happened, please check the Action method for error!\r\n"); sb.append("Abnormal method:"+action.getClass().getName()+"."+this.methodName+"()"); sb.append(e.getMessage()); throw new AbnormalException(sb.toString()); } } /** * 初始化字段 */ private void initField(Object object){ //获取字段集合 Field[] fields=object.getClass().getDeclaredFields(); //获取方法集合 Method [] methods=object.getClass().getDeclaredMethods(); for (Field field : fields) { //给指定赋值 String MethodName="set"+StringUtil.capitalize(field.getName()); if(MethodsUtil.exist(methods,MethodName)){ field.setAccessible(true); Object value=map.get(field.getName()); if(value!=null){ FieldConvertor.convertor(object, field, value.toString()); } } } } /** * 调用方法 * @throws NoSuchMethodException * @throws SecurityException * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */ private Object invokeMethod(Object object) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{ //创建ServletActionContent实例对象 ServletActionContent servlet=new ServletActionContent(); servlet.setRequest(request); servlet.setResponse(response); servlet.setSession(request.getSession()); //创建参数类型 Class parameter[]=new Class[]{ServletActionContent.class}; Method method=object.getClass().getMethod("setServletActionContent", parameter); //参数值 Object obj[]=new Object[]{servlet}; method.invoke(object, obj); //操作方法 //调用execute 方法 //Method execute=class1.getMethod("execute", new Class[0]); Method execute=object.getClass().getMethod(this.methodName, new Class[0]); Object type= execute.invoke(object, new Class[0]); return type; //设置返回类型 } public Object getAction() { // TODO Auto-generated method stub return this.action; } public void handle() throws NullPointerException, ReflectionException, RuntimeException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, ModelDrivenException, AbnormalException { // TODO Auto-generated method stub invokeMap(); modelDriven(); Initialization(); } }
相关文章推荐
- [java]struts2 模型驱动
- JavaWeb -- Struts2 模型驱动
- JavaWeb -- Struts2 模型驱动
- struts2的Action的实现之模型驱动
- 用java实现编译器之有限状态机驱动的整形,浮点型数值识别器
- Java多线程之~~~~使用wait和notify实现生产者消费者模型
- 基于Java的开源的模型驱动转换器和抽象用户接口标识语言6.1发布
- 自然语言处理基于java实现(4) 之 基于VSM模型的信息检索程序
- 基于Java的开源的模型驱动转换器和抽象用户接口标识语言6.1发布
- 四种IO模型在底层驱动的实现方法
- Struts2模型驱动
- Struts2的属性驱动原理和模型驱动原理
- Struts2中属性驱动与模型驱动
- 基于Struts2 的日志管理系统的Java实现
- Struts2模型驱动
- Struts2的模型驱动与属性驱动的理解
- Struts2表单验证、模型驱动、防重复提交、数据回显笔记整理 文章标题
- Struts2之模型驱动及分层架构初识
- Java实现生产者消费者模型:多线程+BlockingQueue
- Java---彩票双色球原始模型实现