各位大哥,小弟想写一个超类ActionX,以便其它的Action类(如LoginAction)都直接继承它,另外,我希望在LoginAction内里面可以实现多个方法,如login,loginSucess,而不只是execute方法
小弟的配置文件都写对了,其中ActionX是这样写的(其实是看别人的,但实现起来有问题)
public class ActionX extends Action implements Serializable {

private static final String hdf_USER_ID = "userId";
private static MessageResources messages = MessageResources.getMessageResources("org.apache.struts.actions.LocalStrings");
private HashMap methods; public ActionX() {
methods = new HashMap();
}
public final ActionForward execute(
ActionMapping actionmapping,
ActionForm actionform, 
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) throws ServletException,
IOException, Exception {
ActionForward forward = null;
HttpSession session = httpServletRequest.getSession(true);
try {
System.out.println("inActionX");
validateSession(httpServletRequest);
String currentPath = (String) session.getAttribute("currentaction");
if (currentPath != null) {
ActionMapping currentMap = this.getServlet().findMapping(currentPath);
if (!currentMap.getType().equals(actionmapping.getType()))
this.release(currentMap, httpServletRequest);
}
if (isHasPermission(actionmapping, httpServletRequest)) {
String methodName = findMethod(actionmapping, actionform,
httpServletRequest, httpServletResponse);
forward = performMethod(actionmapping, actionform,
httpServletRequest, httpServletResponse, methodName);
} else {
forward = actionmapping.findForward("NO_PERMISSION");
}
session.removeAttribute("currentaction");
session.setAttribute("currentaction", actionmapping.getPath());
} catch (Exception e) {
return processError(httpServletRequest, actionmapping, e);
}
if (forward == null) {
System.out.println("--Warning in ActionX:  forward=null-- ");
forward = actionmapping.findForward("FORWARD_NULL");
}
System.out.println("ok");
return forward;
} private String findMethod(ActionMapping mapping, 
ActionForm form,
HttpServletRequest request, 
HttpServletResponse response)throws IOException, ServletException, DefaultException, Exception {
String parameter = mapping.getParameter();
String todo = request.getParameter("todo");
if (parameter == null || "auto".equalsIgnoreCase(parameter))
parameter = "executeIt";
else if (parameter != null && parameter.equals("todo"))
parameter = todo;
return parameter;
} private ActionForward performMethod(ActionMapping mapping, ActionForm form,
HttpServletRequest request, 
HttpServletResponse response,
String methodnName) throws IOException, ServletException,
DefaultException, Exception {
Method method = null;
try {
method = getMethod(methodnName);
} catch (NoSuchMethodException e) {
e.printStackTrace();
String message = messages.getMessage("dispatch.method", mapping.getPath(), methodnName);
throw new DefaultException(message);
}
ActionForward forward = null;
try {
Object args[] = { request, response, form, mapping };
forward = (ActionForward) method.invoke(this, args);
} catch (ClassCastException e) {
e.printStackTrace();
String message = messages.getMessage("dispatch.return", mapping.getPath(), methodnName);
throw new DefaultException(message);
} catch (IllegalAccessException e) {
e.printStackTrace();
String message = messages.getMessage("dispatch.error", mapping.getPath(), methodnName);
throw new DefaultException(message);
} catch (InvocationTargetException e) {
e.printStackTrace();
throw new DefaultException(e.getTargetException().getMessage());
}
return forward;
}    protected Method getMethod(String name) throws NoSuchMethodException {
     Class types[] = {
     javax.servlet.http.HttpServletRequest.class, 
     javax.servlet.http.HttpServletResponse.class, 
     org.apache.struts.action.ActionForm.class, 
     org.apache.struts.action.ActionMapping.class };
        synchronized (methods) {
            Method method = (Method) methods.get(name);
            if (method == null) {
                method = this.getClass().getMethod(name, types);
                methods.put(name, method);
            }
            return (method);
        }
    }
    
public final ActionForward ensureDelete(String title, String description,
HttpServletRequest request) {
request.setAttribute("title", title);
request.setAttribute("description", description);
return getServlet().findForward("GLOBALENSURE");
} public final ActionForward ensureDelete(String title, String description,
String preURL, HttpServletRequest request) {
request.setAttribute("title", title);
request.setAttribute("description", description);
if (preURL != null && !"".equals(preURL))
request.setAttribute("GLOBALENSURE", preURL);
return getServlet().findForward("GLOBALENSURE");
} protected boolean isHasPermission(ActionMapping anActionMapping,
HttpServletRequest aHttpServletRequest) {
return true;
} protected void release(ActionMapping currentMapping,
HttpServletRequest aHttpServletRequest) {
HttpSession session = aHttpServletRequest.getSession(false);
if (session == null)
return;
ActionMapping mapping = currentMapping;
String form = mapping.getAttribute();
if (form != null && session.getAttribute(form) != null)
session.removeAttribute(form);
} protected ActionForward processError(HttpServletRequest request,
ActionMapping am, Exception e) {
String expLeve = ConnectionProper.getProperty("errorLevel");
ActionForward af = new ActionForward();
e.printStackTrace(System.out);
request.setAttribute("title", e);
if (e instanceof DefaultException) {
DefaultException de = (DefaultException) e;
if (de.getExceptionLevel().equalsIgnoreCase("info"))
af = am.findForward("SYSTEM_INFO");
else if (de.getExceptionLevel().equalsIgnoreCase("warn"))
af = am.findForward("SYSTEM_WARN");
else if (expLeve.equalsIgnoreCase("debug"))
af = am.findForward("SYSTEM_DEBUG");
else
af = am.findForward("SYSTEM_ERROR");
} else {
af = am.findForward("SYSTEM_ERROR");
}
return af;
} protected void validateSession(HttpServletRequest request)throws DefaultException {
HttpSession session = request.getSession();
if (session.getAttribute("userId") == null)
throw new DefaultException("当前用户为空!");
else
return ;
}}我不明白,如果是第一次执行这个类,那么userId肯定为空,也就一定会招聘异常,其它的几个方法也是这样,我不知道在实现这个类的时候是不是还需要初始化一些什么东西
还望各位大哥赐教!
不知道大哥们能不能给我一点启示,小弟非常感谢,如果那个大哥有同样的代码并能够将其实现过程的代码给我一份那就太感谢了,谢谢!
本人邮箱:[email protected]
小弟谢谢!