struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Jouravlev <jmi...@gmail.com>
Subject Re: Resetting booleans for session-scoped dynaforms
Date Wed, 12 Oct 2005 23:00:26 GMT
I do not use pre- and post- actions. I use only one action to "prepare
and display" and to "process input". Think ASP.NET or JSF. Therefore,
I need to differentiate what phase I am in.

I do not create form instance in an action class, I prefer it to be
created for me.

Here is the example from MailReader, which I... well... changed a bit:

<form-beans>
  <!-- Logon dynabean; validated with Commons Validator -->
  <form-bean name="LogonForm"
type="org.apache.struts.validator.DynaValidatorForm">
    <form-property name="username" type="java.lang.String"/>
    <form-property name="password" type="java.lang.String"/>
  </form-bean>
  ...
</form-beans>

Nice thing about this dynabean that it does not contain checkboxes ;-)

The action mapping; don't get scared of syntax, I defined a custom
ruleset object, which better describes what am I doing:

<component path = "/Logon"
  type = "net.jspcontrols.mailreader.LogonAction"
  form = "LogonForm">
  <event name = "error" path = "/Logon.do" />
  <event name = "success, cancel, logout" path = "/Home.do" />
  <render name = "notloggedin" path = "/mailreaderpages/logon.jsp" />
  <render name = "loggedin" path = "/mailreaderpages/logoff.jsp" />
</component>

Action class:

public class LogonAction extends DialogAction {
    ...
    protected Map getKeyMethodMap() {
        Map methodMap = new HashMap();
        methodMap.put("DIALOG-EVENT-INIT", "onInit");
        methodMap.put("DIALOG-EVENT-LOGON", "onLogon");
        methodMap.put("DIALOG-EVENT-LOGOFF", "onLogoff");
        methodMap.put("DIALOG-EVENT-CANCEL", "onCancel");
        return methodMap;
    }
    ...
    public ActionForward onLogoff (ActionMapping mapping,
                                   ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response)
            throws Exception {
        logoutUser(request, null);
        return mapping.findForward("logout");
    }
    ...
    public ActionForward getDialogView(ActionMapping mapping,
                                       ActionForm form,
                                       HttpServletRequest request,
                                       HttpServletResponse response)
            throws Exception {

        // Choose page that corresponds to login state.
        HttpSession session = request.getSession();
        return
          (session.getAttribute(Constants.USER_KEY) == null) ?
          mapping.findForward("notloggedin") :
          mapping.findForward("loggedin");
    }
    ...
}

DialogAction is an enhanced DispatchAction. I always turn form
autovalidation off, so request always comes to action class. When
request comes, DialogAction checks request type: POST or GET, that is,
input or render (there are other criteria involved).

If this is an input phase, I check what event was triggered (which
submit button was clicked) and dispatch event to a handler. You see a
very simple onLogoff handler here.

If this is a render phase, I call getDialogView() method, which can do
some other stuff preparing the output data, and then forwards to a
page, you can see "notloggedin" or "loggedin" mappings. **On this
phase I do not need my checkboxes or other fields to be reset, because
I do not expect any input** But reset() is still called by Struts. I
check the request type in reset() method and do not reset the form if
request has GET type.

Michael.

On 10/12/05, Hubert Rabago <hrabago@gmail.com> wrote:
> Hope you don't mind me trying again here (and removing FormDef from
> the subject).
>
> I see two scenarios.
>
> <action path="/edit" type="PrepareFormAction"/>
>
> <action path="/save" type="SubmitFormAction" name="myForm" input="/edit.jsp"/>
>
>
> in PrepareFormAction, you'd:
>
>         DynaActionForm myForm = (DynaActionForm) session.getAttribute("myForm");
>         if (myForm == null) {
>             myForm = (DynaActionForm) ModuleUtils.getInstance()
>                 .getModuleConfig(request).findFormBeanConfig("myForm")
>                 .createActionForm(getServlet());
>             session.setAttribute("myForm", myForm);
>         }
>         // change the values of myForm here, for example, though BeanUtils
>         BeanUtils.copyProperties(myForm, myBusinessObject);
>
> So, reset() doesn't get called.
>
> This is what I'm trying to understand.  How does reset() screw up your
> prepopulation?
>
> When the form gets submitted to /save, reset() gets called to clear
> the checkbox before the form is populated.  (We agree there, no
> problem.)
>
> Hubert

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message