You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Mike Whittaker <mi...@ntlworld.com> on 2003/07/25 11:06:36 UTC

DynaAF in session scope & checkboxes

Has anyone had this combination to work?
I either lose session persistence or (classically) can't clear all
checkboxes.
I've tried overiding reset() in numerous ways.

Help!

TIA
--
Mike W


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


Re: RE[Enlightenment]: DynaAF in session scope & checkboxes

Posted by Jing Zhou <ji...@netspread.com>.
----- Original Message ----- 
From: "Mike Whittaker" <mi...@ntlworld.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>; "Jing
Zhou" <ji...@netspread.com>
Sent: Friday, July 25, 2003 3:42 PM
Subject: RE: RE[Enlightenment]: DynaAF in session scope & checkboxes


>
> You are right, I don't.  In fact the CondResetFormPropertyConfig class is
> just acting as a flag.
> Talk about overkill eh!?

No. I was just interested in the reason. Because your ideas about resetting
in the action class interest me. I was thinking doing reset after the
population
sometime ago. But it did not produce any meaningful results.

There are two fundamental problems if we decide to reset some
properties in the reset method:

1) The property names must match the ones in the web forms. If a page
    author moves the checkboxes, multiboxes, or select menus to another
    page, the reset method must be re-written for the two pages. This
    indicates that the reset method is overly tied to the views.

2) Assume the JavaScript is enabled at browsers (for rich client apps,
    this is required). The checkboxes, multiboxes, and select menus on
    a page could be enabled or disabled by the JavaScript. If we just check
    null parameter name to reset a property, it is not enough. Because
    disabled checkboxes will give you a null parameter name, but you
    should not reset it if it is checked and disabled.

Many developers may not be concerned about the second problem,
but it is a *hole*. The only way that could solve the two problems
is to let browsers send me explicitly what properties to be reset. That's
what we do currently.

It could be an overkill for small apps. But it solves the problems in
a clean way.

>
> I put in the getReset() method just for the sake of it really, first time
> I'd used custom <set-property> Maybe the class will grow with extra
> functionality?
>
> Can you suggest a less overkill flag?
>
> --
> Mike W.
>

Jing
Netspread Carrier
http://www.netspread.com



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


RE: RE[Enlightenment]: DynaAF in session scope & checkboxes

Posted by Mike Whittaker <mi...@ntlworld.com>.
You are right, I don't.  In fact the CondResetFormPropertyConfig class is
just acting as a flag.
Talk about overkill eh!?

I put in the getReset() method just for the sake of it really, first time
I'd used custom <set-property> Maybe the class will grow with extra
functionality?

Can you suggest a less overkill flag?

--
Mike W.

>
>It is not clear to me that why you need to reset only when
>CondResetFormPropertyConfig.getReset() returns true.
>What's the reason behind this?
>
>Jing
>
>----- Original Message -----
>From: "Mike Whittaker" <mi...@ntlworld.com>
>To: "Struts Users Mailing List" <st...@jakarta.apache.org>
>Sent: Friday, July 25, 2003 6:22 AM
>Subject: RE[Enlightenment]: DynaAF in session scope & checkboxes
>
>
>>
>> >Has anyone had this combination to work?
>> >I either lose session persistence or (classically) can't clear all
>> >checkboxes.
>> >I've tried overiding reset() in numerous ways.
>> >
>>
>> I think I have found a solution.
>> I hope this will be of interest.  Any comments appreciated.
>>
>> We are told to overide reset() in DynaAF to give desired behaviour ie
>> clearing all checkboxes as browser sends no request parameters for empty
>> boxes.
>>
>> However, since reset is called before form population for that action, we
>> cannot find from Struts methods how many boxes were checked, except if we
>> use request parameters.
>>
>> So we can look for null for the request parameter we are interested in.
>>
>> However this reset method is called for every reference to the form not
>just
>> on submit.  And since the request parameter does not persist, our session
>> scoped form will not persist!
>>
>> Solution:
>> Do nothing in the resest - but shift the exact same logic to an Action
>that
>> is called just on Submit.
>>
>> /*
>> FormPropertyConfig can be subclassed to provide custom reset by property
>set
>> in struts-config:
>> <form-property name="Grades" type="java.lang.String[]"
>> className="com.company.CondResetFormPropertyConfig">
>> <set-property property="reset" value="true" />
>> </form-property> ]
>> */
>>
>> import java.util.*;
>> import javax.servlet.http.*;
>> import org.apache.struts.action.*;
>> import org.apache.struts.config.*;
>> import org.apache.struts.util.*;
>>
>> public class PreferencesAction extends Action {
>>
>> public ActionForward execute(ActionMapping mapping, ActionForm aForm,
>> HttpServletRequest request, HttpServletResponse res) throws Exception {
>>
>> DynaActionForm form = (DynaActionForm)aForm;
>>
>> String name = mapping.getName(); // name of form bean
>> if (name == null) {
>>    return null; // probably throw exception, should always be bean for
>> this action
>> }
>> FormBeanConfig config =
>>    mapping.getModuleConfig().findFormBeanConfig(name); // FBC for this
>> form bean
>> if (config == null) {
>>    return null; // probably throw exception
>> }
>> FormPropertyConfig[] props = config.findFormPropertyConfigs();
>> for (int i = 0; i < props.length; i++) {
>> // reset only if CondResetFPC & getReset() returns true
>> if (props[i] instanceof CondResetFormPropertyConfig &&
>> (((CondResetFormPropertyConfig)props[i]).getReset())) {
>>
>> if(props[i].getName().equals("Grades")) { // trace
>> System.out.println("****Action.perform()****");
>>         System.out.println("PropertyLength
>> \t"+((String[])form.get("Grades")).length);
>>         System.out.println("PropertyType     \t"+props[i].getType());
>>         System.out.println("PropertyName     \t"+props[i].getName());
>> System.out.println("RequestParameter
>> \t"+request.getParameter(props[i].getName()));
>>      }
>>
>> if(request.getParameter(props[i].getName()) == null) {
>> form.set(props[i].getName(), props[i].initial());
>> }
>>
>> }
>>
>> }
>>
>> return mapping.getInputForward();
>> }
>>
>> }
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>>
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>


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


Re: RE[Enlightenment]: DynaAF in session scope & checkboxes

Posted by Jing Zhou <ji...@netspread.com>.
It is not clear to me that why you need to reset only when
CondResetFormPropertyConfig.getReset() returns true.
What's the reason behind this?

Jing

----- Original Message ----- 
From: "Mike Whittaker" <mi...@ntlworld.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Friday, July 25, 2003 6:22 AM
Subject: RE[Enlightenment]: DynaAF in session scope & checkboxes


>
> >Has anyone had this combination to work?
> >I either lose session persistence or (classically) can't clear all
> >checkboxes.
> >I've tried overiding reset() in numerous ways.
> >
>
> I think I have found a solution.
> I hope this will be of interest.  Any comments appreciated.
>
> We are told to overide reset() in DynaAF to give desired behaviour ie
> clearing all checkboxes as browser sends no request parameters for empty
> boxes.
>
> However, since reset is called before form population for that action, we
> cannot find from Struts methods how many boxes were checked, except if we
> use request parameters.
>
> So we can look for null for the request parameter we are interested in.
>
> However this reset method is called for every reference to the form not
just
> on submit.  And since the request parameter does not persist, our session
> scoped form will not persist!
>
> Solution:
> Do nothing in the resest - but shift the exact same logic to an Action
that
> is called just on Submit.
>
> /*
> FormPropertyConfig can be subclassed to provide custom reset by property
set
> in struts-config:
> <form-property name="Grades" type="java.lang.String[]"
> className="com.company.CondResetFormPropertyConfig">
> <set-property property="reset" value="true" />
> </form-property> ]
> */
>
> import java.util.*;
> import javax.servlet.http.*;
> import org.apache.struts.action.*;
> import org.apache.struts.config.*;
> import org.apache.struts.util.*;
>
> public class PreferencesAction extends Action {
>
> public ActionForward execute(ActionMapping mapping, ActionForm aForm,
> HttpServletRequest request, HttpServletResponse res) throws Exception {
>
> DynaActionForm form = (DynaActionForm)aForm;
>
> String name = mapping.getName(); // name of form bean
> if (name == null) {
>    return null; // probably throw exception, should always be bean for
> this action
> }
> FormBeanConfig config =
>    mapping.getModuleConfig().findFormBeanConfig(name); // FBC for this
> form bean
> if (config == null) {
>    return null; // probably throw exception
> }
> FormPropertyConfig[] props = config.findFormPropertyConfigs();
> for (int i = 0; i < props.length; i++) {
> // reset only if CondResetFPC & getReset() returns true
> if (props[i] instanceof CondResetFormPropertyConfig &&
> (((CondResetFormPropertyConfig)props[i]).getReset())) {
>
> if(props[i].getName().equals("Grades")) { // trace
> System.out.println("****Action.perform()****");
>         System.out.println("PropertyLength
> \t"+((String[])form.get("Grades")).length);
>         System.out.println("PropertyType     \t"+props[i].getType());
>         System.out.println("PropertyName     \t"+props[i].getName());
> System.out.println("RequestParameter
> \t"+request.getParameter(props[i].getName()));
>      }
>
> if(request.getParameter(props[i].getName()) == null) {
> form.set(props[i].getName(), props[i].initial());
> }
>
> }
>
> }
>
> return mapping.getInputForward();
> }
>
> }
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>


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


RE[Enlightenment]: DynaAF in session scope & checkboxes

Posted by Mike Whittaker <mi...@ntlworld.com>.
>Has anyone had this combination to work?
>I either lose session persistence or (classically) can't clear all
>checkboxes.
>I've tried overiding reset() in numerous ways.
>

I think I have found a solution.
I hope this will be of interest.  Any comments appreciated.

We are told to overide reset() in DynaAF to give desired behaviour ie
clearing all checkboxes as browser sends no request parameters for empty
boxes.

However, since reset is called before form population for that action, we
cannot find from Struts methods how many boxes were checked, except if we
use request parameters.

So we can look for null for the request parameter we are interested in.

However this reset method is called for every reference to the form not just
on submit.  And since the request parameter does not persist, our session
scoped form will not persist!

Solution:
Do nothing in the resest - but shift the exact same logic to an Action that
is called just on Submit.

/*
FormPropertyConfig can be subclassed to provide custom reset by property set
in struts-config:
<form-property name="Grades" 	type="java.lang.String[]"
	className="com.company.CondResetFormPropertyConfig">
		<set-property property="reset" value="true" />
</form-property> ]
*/

import java.util.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.config.*;
import org.apache.struts.util.*;

public class PreferencesAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm aForm,
		HttpServletRequest request, HttpServletResponse res) throws Exception {

		DynaActionForm form = (DynaActionForm)aForm;

		String name = mapping.getName(); // name of form bean
		if (name == null) {
		   return null; // probably throw exception, should always be bean for
this action
		}
		FormBeanConfig config =
		   mapping.getModuleConfig().findFormBeanConfig(name); // FBC for this
form bean
		if (config == null) {
		   return null; // probably throw exception
		}
		FormPropertyConfig[] props = config.findFormPropertyConfigs();
		for (int i = 0; i < props.length; i++) {
			// reset only if CondResetFPC & getReset() returns true
			if (props[i] instanceof CondResetFormPropertyConfig &&
				(((CondResetFormPropertyConfig)props[i]).getReset())) {

				if(props[i].getName().equals("Grades")) { // trace
						System.out.println("****Action.perform()****");
		        		System.out.println("PropertyLength
\t"+((String[])form.get("Grades")).length);
		        		System.out.println("PropertyType     \t"+props[i].getType());
		        		System.out.println("PropertyName     \t"+props[i].getName());
						System.out.println("RequestParameter
\t"+request.getParameter(props[i].getName()));
		     	}

				if(request.getParameter(props[i].getName()) == null) {
					form.set(props[i].getName(), props[i].initial());
				}

			}

		}

		return mapping.getInputForward();
	}

}




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