You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Tom Janofsky <to...@yahoo.com> on 2001/02/06 23:34:04 UTC

Getting a value in the JSP from an ActionForm

Here's my dilemma (see 2 summary questions at end):

I'm coming to a page with a value set in an ActionForm by my action for
me.  Works great.  Now what I want to do is get that property out of
that bean, and into another bean that's only being used on the page. 
Something conceptually like:

<jsp:useBean id="page_only_bean" scope="page" class="xyz">
<jsp:setProperty name="page_only_bean"
value="action_form_bean.property">

No go.  Any thoughts on an easy way to do this?

Alternatively I thought html:form looked up the bean, and put it under
the name that i have registered in struts-config.xml for form-bean.  So
after my html:form tag, I tried:

<jsp:useBean id="page_only_bean" scope="page" class="xyz"/>
<%
  page_only_bean.setXXX(bean_name_from_form-bean_tag.getXXX());
%>

Again, no dice.  JSP compiler says no such variable.


Anyway, I guess this boils down to 2 simple questions:

1) (The most importnant) I've got an ActionForm that is populated and
passed all around.  I can directly manipulate it in Action classes no
problem.  html:text and bean:writes work fine.  Now how do I get a real
reference to the ActionForm in the JSP?  (Why?  What if I want to call
getXXX() or setXXX() on the Form bean in a scriptlet?)

2) Is there a way using bean:define to copy a property from one bean
into another bean?  (Don't think so, although I can make a bean that
consists just of that property...)


Thanks,

--tom

Re: Getting a value in the JSP from an ActionForm

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Tom Janofsky wrote:

> Here's my dilemma (see 2 summary questions at end):
>
> I'm coming to a page with a value set in an ActionForm by my action for
> me.  Works great.  Now what I want to do is get that property out of
> that bean, and into another bean that's only being used on the page.
> Something conceptually like:
>
> <jsp:useBean id="page_only_bean" scope="page" class="xyz">
> <jsp:setProperty name="page_only_bean"
> value="action_form_bean.property">
>

How about:

<jsp:setProperty name="page_only_bean"
  value="<%= action_form_bean.getProperty() >%"/>

for this?

A more Struts-oriented solution might be some extended version of
<jsp:setProperty> that behaves more like what you are trying ... maybe for
Struts 1.1?

>
> No go.  Any thoughts on an easy way to do this?
>
> Alternatively I thought html:form looked up the bean, and put it under
> the name that i have registered in struts-config.xml for form-bean.  So
> after my html:form tag, I tried:
>
> <jsp:useBean id="page_only_bean" scope="page" class="xyz"/>
> <%
>   page_only_bean.setXXX(bean_name_from_form-bean_tag.getXXX());
> %>
>
> Again, no dice.  JSP compiler says no such variable.
>
> Anyway, I guess this boils down to 2 simple questions:
>
> 1) (The most importnant) I've got an ActionForm that is populated and
> passed all around.  I can directly manipulate it in Action classes no
> problem.  html:text and bean:writes work fine.  Now how do I get a real
> reference to the ActionForm in the JSP?  (Why?  What if I want to call
> getXXX() or setXXX() on the Form bean in a scriptlet?)
>

The form bean for the current form is stored as a request or session
attribute (depending on your struts-config.xml setting) with a key equal to
the form name.  That is how the <html:form> tag itself finds these beans,
so you can count on it as well.

>
> 2) Is there a way using bean:define to copy a property from one bean
> into another bean?  (Don't think so, although I can make a bean that
> consists just of that property...)
>

Not yet, but a runtime expression should work as illustrated above.

>
> Thanks,
>
> --tom

Craig