action是逻辑处理bean,actionform是数据bean

解决方案 »

  1.   

    同意,actionForm就是对数据的封装
      

  2.   

    怎么没见actionform处理的效果出来?
    帮我看看代码,谢谢
      

  3.   

    这是programming jakarta struts书中对actionform的描述。我依着例子写了上面的代码
    总是转向welcome.jsp,看不到actionform的效果在哪
    3.5.1 Using the Struts ActionForm
    Struts ActionForm objects are used in the framework to pass client input data back and forth between the user and the business layer. The framework automatically collects the input from the request and passes this data to an Action using a form bean, which then can be passed along to the business layer. To keep the presentation layer decoupled from the business layer, you should not pass the ActionForm itself to the business layer; rather, create the appropriate DTO using the data from the ActionForm. The following steps illustrate how the framework processes an ActionForm for every request: 1.       Check the mapping for the action and see if an ActionForm has been configured. 2.       If an ActionForm is configured for the action, use the name attribute from the action element to look up the form bean configuration information. 3.       Check to see if an instance of the ActionForm already has been created. 4.       If an ActionForm instance is present in the appropriate scope and it's the same type as needed for the new request, reuse it. 5.       Otherwise, create a new instance of the required ActionForm and store it in the appropriate scope (set by the scope attribute for the action element). 6.       Call the reset( ) method on the ActionForm instance. 7.       Iterate through the request parameters, and populate every parameter name that has a corresponding setter method in the ActionForm with the value for that request parameter. 8.       Finally, if the validate attribute is set to "true", invoke the validate( ) method on the ActionForm instance and return any errors. For every HTML page where form data is posted, you should use an ActionForm. The same ActionForm can be used for multiple pages if necessary, as long as the HTML fields and ActionForm properties match up. Example 3-5 shows the com.oreilly.struts.banking.form.LoginForm that is used with the banking application. 
      

  4.   

    ActionForm是对表单的封装(我想这个我应该不会弄错吧?)
    不会我看漏了一点
    public class LoginAction extends Action {
      public ActionForward execute( ActionMapping mapping,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response )
        throws Exception {
    ........................中form就是相对应的actionform,要强制转化后才能使用public class LoginForm extends ActionForm {
      // The user's private ID number
      private String pinNumber;
      // The user's access number
      private String accessNumber;
     
      public LoginForm(  ) {
        super(  );
        resetFields(  );
      }
      /**
       * Called by the framework to validate the user has entered values in the
       * accessNumber and pinNumber fields.
       */
      public ActionErrors validate(ActionMapping mapping, HttpServletRequest req ){
        ActionErrors errors = new ActionErrors(  );
    .........................不过我还是不清楚:ActionErrors返回给谁呢?如果返回的不是null值,又应当有什么反应呢?请赐教。谢谢
      

  5.   

    ActionErrors errors = form.validate(mapping, request);
            if ((errors == null) || errors.isEmpty()) {
                if (log.isTraceEnabled()) {
                    log.trace("  No errors detected, accepting input");
                }
                return (true);
            }        // Special handling for multipart request
            if (form.getMultipartRequestHandler() != null) {
                if (log.isTraceEnabled()) {
                    log.trace("  Rolling back multipart request");
                }
                form.getMultipartRequestHandler().rollback();
            }        // Has an input form been specified for this mapping?
            String input = mapping.getInput();
            if (input == null) {
                if (log.isTraceEnabled()) {
                    log.trace("  Validation failed but no input form available");
                }
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                                   getInternal().getMessage("noInput",
                                                            mapping.getPath()));
                return (false);
            }        // Save our error messages and return to the input form if possible
            if (log.isDebugEnabled()) {
                log.debug(" Validation failed, returning to '" + input + "'");
            }
            request.setAttribute(Globals.ERROR_KEY, errors);        if (moduleConfig.getControllerConfig().getInputForward()) {
                ForwardConfig forward = mapping.findForward(input);
                processForwardConfig( request, response, forward);
            } else {
                internalModuleRelativeForward(input, request, response);
            }        return (false);
    这是Struts中处理Form的一段代码,从上面可以看出validate返回如果不是null,则会放回到inputPage
      

  6.   

    你这段代码是在public class LoginAction extends Action {.............
    中显式调用public class LoginForm extends ActionForm {.........
    中的代码是吗?那么,配置文件中
    <action-mappings>
    <action path="/login"
    type="com.self.struts.actions.LoginAction"
    name="LoginForm"
    input="/login.jsp"
                               validate="true"  ///////////这又有什么用?
    scope="request">
    <forward name="Success" path="/welcome.jsp"/>
    <forward name="Failure" path="/login_error.jsp"/>
    </action>
    我想,validate属性设为true后,是不是系统就会自动处理,比如:errors 不为空就返回到表单页,如果在代码中调用了validate那么,不管validate属性是不是true,都调用了validate
    这样,这个validate属性不是没什么用吗?
      

  7.   

    好象是web服务器的问题
    在tomcat中,好象不管validate属性怎设都转向welcome.jsp
    在resin中,好象不管validate属性怎设都不反应,(只要返回的errors不为空)
      

  8.   

    纠正一下,在resin中,修改了struts-config中
    validate的属性后重启服务或重新编译一次actionform的那个类好象就有反应了。
    比如,返回的error不为空时
    validate设为true,提交表单它就不会跳到welcome.jsp去,而是留在表单页
    反之,若validate设为false,就会跳到welcome.jspaction-mappings>
    <action path="/login"
    type="com.self.struts.actions.LoginAction"
    name="LoginForm"
    input="/login.jsp"
                               validate="true"  ///////////这又有什么用?
    scope="request">
    <forward name="Success" path="/welcome.jsp"/>
    <forward name="Failure" path="/login_error.jsp"/>
    </action>
      

  9.   


    ("com.self.struts.errors.InvalidLoginError");
        errors.add (ActionErrors.GLOBAL_ERROR, newError);
        return errors; //这个值返回给谁用的?好象没反应啊
      }
    erres 是个对象集,里面是errer。返回到,input的jsp
      

  10.   

    actionFrom不处理,只做简单判断,我理解只是在jsp和action之间传输数据,逻辑在action里
      

  11.   

    to wwwtom:
    errors返回到input的jsp?
    那么也没见到有什么反应啊?
      

  12.   

    错了。就有反应了,会在你错的地方提示,例
    我自己觉得它的作用有点象脚本,
    <bean:message key="prompt.pageNote"/> :             //显示资源文件prompt.pageNote
    <html:textarea name="pageForm" property="pageNote"/>//用form里的validate="true“方法
    html:errors property="pageNote"/></br>//显示资源文件里的error.pageNote的地方
      

  13.   

    错了就有反应?
    你是说要在jsp脚本里使用
    <bean:message 
    <html:errors
    等标记?
    能不能介绍下它的所有参数含义?
    <html:errors
    标记所在的地方就是显示错误的地方?
      

  14.   

    ActionError newError = new ActionError ("com.self.struts.errors.InvalidLoginError");
    还有,在这里的出错信息是不是要在xml文件里定义?
      

  15.   

    ActionForm 实际上就是VALUE BEAN,用来封装数据的.
    Action     实际上就是Ctrl,用来控制你的事务逻辑的处理.