类似于DispatchAction的action

本文介绍了一个基于Struts框架的基础Action实现,该Action通过反射机制动态调用具体的方法,并集成了Spring框架来获取业务逻辑服务。文章展示了如何设置ActionServlet、通过WebApplicationContext获取DAO层服务、以及如何通过请求参数确定要执行的具体Action方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.struts.action.base;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.dao.Iservice;

/**
 * MyEclipse Struts Creation date: 12-08-2006
 *
 * XDoclet definition:
 *
 * @struts.action validate="true"
 */
public class Base1Action extends Action {
    private Iservice service;

    public void setServlet(ActionServlet actionServlet) {
        super.setServlet(actionServlet);
        System.out.println("BaseAction Star");
        ServletContext servletContext = actionServlet.getServletContext();
        WebApplicationContext wac = WebApplicationContextUtils
                .getRequiredWebApplicationContext(servletContext);
        this.service = (Iservice) wac.getBean("logDao");
    }

    public Iservice getService() {
        return this.service;
    }

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        ActionForward forward = mapping.findForward("error.jsp");
        //系统默认的方法是exec
        String methodName = "exec";
        if(request.getParameter("method") != null);
        {
            methodName = request.getParameter("method");
        }
        //系统调用方法的参数的数据类型
        Class[] cType = { ActionMapping.class, ActionForm.class,
                HttpServletRequest.class, HttpServletResponse.class };
        Object[] o = { mapping, form, request, response };
        Method method;
        try {
            this.begin(mapping, form, request, response);
            method = this.getClass().getMethod(methodName, cType);
            forward = (ActionForward) method.invoke(this, o);
            this.end(mapping, form, request, response);
        } catch (SecurityException e) {
            request.setAttribute("error", e.getMessage());
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            request.setAttribute("error", e.getMessage());
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            request.setAttribute("error", e.getMessage());
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            request.setAttribute("error", e.getMessage());
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            request.setAttribute("error", e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            request.setAttribute("error", e.getMessage());
            e.printStackTrace();
        }
        return forward;
    }

    public void begin(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        System.out.println("begin");
    }

    public void end(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        System.out.println("end");
    }
    public ActionForward exec(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        System.out.println("exec");
        return null;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值