Action一个Java类,扩展了org.apache.struts.Action。ActionServlet组装ActionForm,然后将其传递给Action。Action通常负责输入校验,存取业务信息,以及决定向Servlet返回哪个ActionForward。
package app;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.io.*;
public class RegisterAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res) {
// 转换form到 RegisterForm
RegisterForm rf = (RegisterForm) form;
String username = rf.getUserName();
String password1 = rf.getPassword1();
String password2 = rf.getPassword2();
// 业务逻辑
if (password1.equals(password2)) {
try {
// 调用服务把用户加到数据库中去
//UserService.getInstance().AddUser(username, password1);
return mapping.findForward("success");
} catch (Exception e) {
return mapping.findForward("failure");
}
}
// Return ActionForward for failure
return mapping.findForward("failure");
}
}