jpetstore的一段代码,请高手解释一下什么意思啊

luowei
04-12-08 1 172

jpetstore的一段代码,请高手解释一下什么意思啊
public class BeanAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {

String forward = "success";

try {

ActionContext.initialize(request, response);

if (form != null) {

// Explicit Method Mapping
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) {
throw new BeanActionException("Error dispatching bean action via method parameter ('" + methodName + "'). Cause: " + e, e);
}
}

// Path Based Method Mapping
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) {
throw new BeanActionException("Error dispatching bean action via URL pattern ('" + methodName + "'). Cause: " + e, e);
}
}
}
}
}

} catch (Exception e) {
request.setAttribute("BeanActionException", e);
throw e;
}

return mapping.findForward(forward);
}

ljshan
2004-12-09 13:02

没有什么复杂的呀,就是简化了对Struts的依赖,用户只要直接写formbean就可以了。这种formbean要么不写(form == null),要么直接将操作方法写进来,然后返回forward名称。具体被调用的方法通过什么方式指明有两种方式:1)action的名字2)通过actionmapping中的parameter指定。这样做的好处是可以将操作与属性统一到一起,更加符合POJO的思想。