Re: stupid Q: setters are not called if properties are empty
"Kan Leung, MK" wrote: > Sorry if you find if this is a bit off topic. ;) > > Hi all, > > Hope this is not a FAQ. (I spent 2 hours to do net search but didn't > get a good answer) > It is actually a pretty subtle point, and on-topic in my book :-) > > 8< > > > 8< > > Introspection magic makes the property setters called if they match the > input parameters by GET or by POST. > > e.g. URL?address1=a&address2=&address3= > > There are exceptions: setters of some parameters which have no value > won't get called. In the above string, the later two are not called > because they have no values assigned. Suppose my bean has session scope. > Values kept unchanged. > The exception you mention is specifically required in the JSP specification. For any property that would be set to a zero-length string, and you are using , the setter method is *not* called. A very similar problem occurs with checkboxes -- if the checkbox is checked, the value is included and the corresponding boolean setter is called. But, if it is not checked, *nothing* is sent to the server, so your boolean property will remain set :-(. > > My question is: how can the web users clear a text field if the field is > incorrectly filled? > Struts deals with these issues via a combination of features: * First, you should do your submits to an action defined in the controller servlet, rather than back to the JSP page. This makes the behavior of irrelevant, because it will never be utilized. * The Struts configuration file allows you to configure a form bean for each input form. If you do so, the Struts controller will automatically populate the bean's properties based on your request parameters, similar to what does. * However, you would seem to still have a problem with boolean properties and with setting String properties to zero length. This is resolved by requiring your bean to implement a reset() method, which will set all of the bean properties to default values (normally false for booleans, and null for Strings). Now, if your user erases the contents of a String field, the following things happen: * The reset() method of your form bean is called, setting the string property to null. * The setX() method is *not* called (to be consistent with semantics). * The end result is that your property has been set to a null string, which is what you normally want. You can also reset to a zero length String constant if you prefer. > > I suppose the introspection magic is a life-saver. I just don't want to > check the return value of request.getParameter("address2) for each input > to determine if it's empty. > > Can anybody give me a good alternative to clear unwanted fields easily? > > -- > Kan LEUNG, M K > email: [EMAIL PROTECTED] > Digital Empires Company Limited Craig McClanahan
Re: stupid Q: setters are not called if properties are empty
It's not a stupid question. I'm not sure if it's off-topic or not. :-) The JSP fragment you posted suggests that your JSP is being invoked directly. That is, the URL coming from the browser references the JSP, and not a Struts Action (e.g. doSomthing.do). If this is the case, then I don't think Struts can help you much, since it won't have a chance to touch the bean. In a typical Struts application, the request will be handled by the controller servlet, which will parse the request and populate a form bean with the parameters. An Action will then process the request and forward to a JSP for displaying the results to the user. In this scenario, since Struts populated form bean for you, there is usually no need to use the useBean/setProperty mechanism you have. Also - and this is the piece you need - the controller servlet will call the reset() method on your form bean immediately before populating it with the values from the new request. That gives you the chance to clear out the old values, so that they are not left in the form bean if there is no corresponding value in the request. Hope this helps. -- Martin Cooper Tumbleweed Communications At 10:42 PM 2/27/01 +0800, Kan Leung, MK wrote: >Sorry if you find if this is a bit off topic. ;) > >Hi all, > >Hope this is not a FAQ. (I spent 2 hours to do net search but didn't >get a good answer) > >8< > > >8< > >Introspection magic makes the property setters called if they match the >input parameters by GET or by POST. > >e.g. URL?address1=a&address2=&address3= > > >There are exceptions: setters of some parameters which have no value >won't get called. In the above string, the later two are not called >because they have no values assigned. Suppose my bean has session scope. >Values kept unchanged. > >My question is: how can the web users clear a text field if the field is >incorrectly filled? > >I suppose the introspection magic is a life-saver. I just don't want to >check the return value of request.getParameter("address2) for each input >to determine if it's empty. > >Can anybody give me a good alternative to clear unwanted fields easily? > >-- >Kan LEUNG, M K >email: [EMAIL PROTECTED] >Digital Empires Company Limited
stupid Q: setters are not called if properties are empty
Sorry if you find if this is a bit off topic. ;) Hi all, Hope this is not a FAQ. (I spent 2 hours to do net search but didn't get a good answer) 8< 8< Introspection magic makes the property setters called if they match the input parameters by GET or by POST. e.g. URL?address1=a&address2=&address3= There are exceptions: setters of some parameters which have no value won't get called. In the above string, the later two are not called because they have no values assigned. Suppose my bean has session scope. Values kept unchanged. My question is: how can the web users clear a text field if the field is incorrectly filled? I suppose the introspection magic is a life-saver. I just don't want to check the return value of request.getParameter("address2) for each input to determine if it's empty. Can anybody give me a good alternative to clear unwanted fields easily? -- Kan LEUNG, M K email: [EMAIL PROTECTED] Digital Empires Company Limited