如果为第个Form都建一个Action那是相当麻烦的,
如果像下面这样构建啦一个通用的Action会带来什么好处?
而像平常的做法是几乎是一个Form 和 Action的这种做法又有什么好处?
请大家谈谈自己的看法和经验?
下面是源码和相关的解释
请大家先看一下Struts-config.xml 中的一部分内容
xml 代码
- <!--第一种 parameter为空并且name为空时的情况 即 form=null 时 直接转发-->
- <action path="/dgroom/index" type="com.dgroom.struts.BeanAction"
- validate="false" >
- <forward name="success" path="/dgroom/index.xsp"/>
- </action>
- <!--第二种情况 parameter="*"时 意为想调用一个Bean但又不想调用他里面的任何方法时 -->
- <action path="/dgroom/loginForm" type="com.dgroom.struts.BeanAction"
- name="accountBean" scope="session" parameter="*"
- validate="false">
- <forward name="success" path="/account/loginForm.xsp"/>
- </action>
- <!--第三种 当 parameter=" NAME " 有值时 调用 accountBean中的displayAll()方法-->
- <action path="/dgroom/displayAllAccount" type="com.dgroom.struts.BeanAction"
- name="accountBean" scope="session" parameter="displayAll"
- validate="false">
- <forward name="success" path="/dgroom/displayAll.xsp"/>
- </action>
- <!--第四种 就直接调用地址栏中的最后的那个 login()方法 -->
- <action path="/dgroom/login" type="com.dgroom.struts.BeanAction"
- name="accountBean" scope="session"
- validate="false">
- <forward name="success" path="/dgroom/index.xsp"/>
- </action>
而在其它的Bean中的方法也如同下面的这个方法,都是返回一个字符串
而在AccountBean.java中的方法如下
java 代码
- public class AccountBean
- {
- public String login(){
- if(){
- //成功
- return "success";
- }
- //失败
- return "failure";
- }
- }
下面的是通用的 BeanAction.java 类
java 代码
- package com.dgroom.struts;
- 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 javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.lang.reflect.Method;
- import com.dgroom.util.*;
- public class BeanAction extends Action {
- public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- String forward = "success";
- try {
- //这里是对ActionContext.save(request,reponse);
- //对映 Struts-config.xml 中的 第一种 :当请求中的 Form 为空时直接转 发
- if (form != null) {
- Method method = null;
- String methodName = mapping.getParameter();
- //第三种:当参数中存在着某个值时并且不等于 * 时就直接调用那个参数中的方法
- if (methodName != null && !"*".equals(methodName)) {
- try {
- method = form.getClass().getMethod(methodName, null);
- forward = (String) method.invoke(form, null);
- } catch (Exception e) {
- // 这里是一个自定义的异常
- }
- }
- //第四种 当参数为空时 直接调用地址栏最后的方法
- if (method == null && !"*".equals(methodName)) {
- methodName = mapping.getPath();
- if (methodName.length() > 1) {
- int slash = methodName.lastIndexOf("/") + 1;
- methodName = methodName.substring(slash);
- if (methodName.length() > 0) {
- try {
- method = form.getClass().getMethod(methodName, null);
- forward = (String) method.invoke(form, null);
- } catch (Exception e) {
- // 这里是一个自定义的异常
- }
- }
- }
- }
- }
- } catch (Exception e) {
- // 这里是一个自定义的异常
- throw e;
- }
- return mapping.findForward(forward);
- }
- }