RE: Accessing form bean from inside a custom tag, take 2

2002-04-23 Thread James Mitchell

Sorry if I'm late at getting you an answer.

But I hope others might find this helpful in the future as well.





If you follow the trail from the struts-example, you'll find just such an
example of what you are trying to do:
--
struts-config.xml
--
  action-mappings

!-- Edit user registration --
actionpath=/editRegistration

type=org.apache.struts.webapp.example.EditRegistrationAction
  attribute=registrationForm
  scope=request
   validate=false
  forward name=success  path=/registration.jsp/
/action



--
org.apache.struts.webapp.example.EditRegistrationAction
--

163 form = new RegistrationForm();
164if (request.equals(mapping.getScope()))
165request.setAttribute(mapping.getAttribute(), form);
166else
167session.setAttribute(mapping.getAttribute(), form);

/*
 *Notice line 165 and line 167, the mapping.getAttribute is getting
registrationForm from above in the struts-config.xml and storing the form
in the request by that name.
 * Now assuming that all goes well (the user filled in the form correctly),
then we go to our jsp.
 */

201 }
202 return (mapping.findForward(success));
203

--
registration.jsp
--
// Here the current bean instance is set into the pageContext by the name
subscription

143logic:iterate id=subscription name=user
property=subscriptions
144  tr

/* Then, down here we try to create our link tag, html:link works fine for
most
 * cases, however, we need have a special case.
 * Personally, I use a custom tag every time I need to pass an id to another
action
 * This works great for me because most of my detail records are unique by
their id and not
 * by some compound key
 * Basically, it appends id=n where n is the id of the database record
 * that I am using/changing
 */

158  app:linkSubscription page=/editSubscription.do?action=Delete
159bean:message key=registration.deleteSubscription/
160  /app:linkSubscription

/* app is defined as a custom tld. linkSubscription is of type
 * org.apache.struts.webapp.example.LinkSubscriptionTag
 */


--
org.apache.struts.webapp.example.LinkSubscriptionTag
--
173 Subscription subscription = null;
174 try {
175 subscription = (Subscription) pageContext.findAttribute(name);
176} catch (ClassCastException e) {
177 subscription = null;
178 }

/*
 * Notice here that we attempt to get the form from the pageContext.
 * the variable called name is set to a default value of subscription
 * in this class.  You can have given it any name you like
 * by changing it in the logic:iterate or if you are *not* within an
iteration,
 * by adding name=myName to the app:linkSubscription tag in
registration.jsp
 *
 * I hope that this provides some insight into writing your own custom
(struts) tags.
 * They are really quite flexible and powerful.
 *
 * As you build your application, try to think ahead and consider every
possible use
 * of a new tag, that will provide for better reuse and it will make it more
maintainable
 * for the next guy (or gal).
 *
 */






 -Original Message-
 From: Bryan P. Glennon [mailto:[EMAIL PROTECTED]]
 Sent: Monday, April 22, 2002 10:19 AM
 To: Struts Users Mailing List
 Subject: RE: Accessing form bean from inside a custom tag, take 2


 Christian -
 Those were the first two things I tried. In each case, I get a null
 returned from getAttribute. I even tried
 pageContext.findAttribute(beanName) and it also returned null. I am
 pretty sure I have things configured correctly:
 In struts-config.xml I define a form bean named fileXferBean. In the
 action mapping that will lead to the page with the custom tag, I specify
 fileXferBean for the name attribute. I see the bean get instantiated,
 but it is just not there when I try to get it from the doStartTag
 function (using ...getAttribute(fileXferBean)).

 I know I should be able to do this - what am I missing?

 Thanks again,
 Bryan


 --
 Bryan Glennon (mailto:[EMAIL PROTECTED])
 BPG Consulting, Inc. (http://www.bpgc.com)
 Tech Question? (mailto:[EMAIL PROTECTED])


 -Original Message-
 From: Christian Bouessay [mailto:[EMAIL PROTECTED]]
 Sent: Monday, April 22, 2002 8:53 AM
 To: Struts Users Mailing List
 Subject: Re: Accessing form bean from inside a custom tag, take 2


 Form-bean is stored in request/session.
 And in a custom tag, you can access the property pageContext.

 Why not using a code like :

 MyFormBean form = (MyFormBean)pageContext.getAttribute(myFormBeanName,
PageContext.SESSION_SCOPE);

 or

 MyFormBean form = (MyFormBean)pageContext.getAttribute(myFormBeanName,
PageContext.REQUEST_SCOPE);

 Don't know

Accessing form bean from inside a custom tag, take 2

2002-04-22 Thread Bryan P. Glennon

(I tried to post this to the list yesterday, but I don't think it made
it - it is not in the list archive. Sorry if this shows up twice.)

Is there any way to access the form bean from within the
doStartTag/doAfterBody functions of a custom tag?

Thanks,
Bryan

--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]




Re: Accessing form bean from inside a custom tag, take 2

2002-04-22 Thread Christian Bouessay

Form-bean is stored in request/session.
And in a custom tag, you can access the property pageContext.

Why not using a code like :

MyFormBean form = (MyFormBean)pageContext.getAttribute(myFormBeanName,
 PageContext.SESSION_SCOPE);

or

MyFormBean form = (MyFormBean)pageContext.getAttribute(myFormBeanName,
 PageContext.REQUEST_SCOPE);

Don't know if it answer your question :-)
--
C. Bouessay




Bryan P. Glennon wrote:
 (I tried to post this to the list yesterday, but I don't think it made
 it - it is not in the list archive. Sorry if this shows up twice.)
 
 Is there any way to access the form bean from within the
 doStartTag/doAfterBody functions of a custom tag?
 
 Thanks,
 Bryan
 
 --
 To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
 For additional commands, e-mail: mailto:[EMAIL PROTECTED]
 
 




--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]




RE: Accessing form bean from inside a custom tag, take 2

2002-04-22 Thread Bryan P. Glennon

Christian -
Those were the first two things I tried. In each case, I get a null
returned from getAttribute. I even tried
pageContext.findAttribute(beanName) and it also returned null. I am
pretty sure I have things configured correctly:
In struts-config.xml I define a form bean named fileXferBean. In the
action mapping that will lead to the page with the custom tag, I specify
fileXferBean for the name attribute. I see the bean get instantiated,
but it is just not there when I try to get it from the doStartTag
function (using ...getAttribute(fileXferBean)).

I know I should be able to do this - what am I missing?

Thanks again, 
Bryan


--
Bryan Glennon (mailto:[EMAIL PROTECTED])
BPG Consulting, Inc. (http://www.bpgc.com)
Tech Question? (mailto:[EMAIL PROTECTED])


-Original Message-
From: Christian Bouessay [mailto:[EMAIL PROTECTED]] 
Sent: Monday, April 22, 2002 8:53 AM
To: Struts Users Mailing List
Subject: Re: Accessing form bean from inside a custom tag, take 2


Form-bean is stored in request/session.
And in a custom tag, you can access the property pageContext.

Why not using a code like :

MyFormBean form = (MyFormBean)pageContext.getAttribute(myFormBeanName,
 PageContext.SESSION_SCOPE);

or

MyFormBean form = (MyFormBean)pageContext.getAttribute(myFormBeanName,
 PageContext.REQUEST_SCOPE);

Don't know if it answer your question :-)
--
C. Bouessay




Bryan P. Glennon wrote:
 (I tried to post this to the list yesterday, but I don't think it made

 it - it is not in the list archive. Sorry if this shows up twice.)
 
 Is there any way to access the form bean from within the 
 doStartTag/doAfterBody functions of a custom tag?
 
 Thanks,
 Bryan
 
 --
 To unsubscribe, e-mail:
mailto:[EMAIL PROTECTED]
 For additional commands, e-mail: 
 mailto:[EMAIL PROTECTED]
 
 




--
To unsubscribe, e-mail:
mailto:[EMAIL PROTECTED]
For additional commands, e-mail:
mailto:[EMAIL PROTECTED]


--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]