You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Karl Stenerud <ka...@webartjapan.com> on 2003/03/24 03:47:00 UTC

Re: Populating form fields (more)

Hi, thanks for the reply, but I'm looking for something a little different 
from this...

I'm not sure if I am missing some fundamental concept of struts, but it 
appears to be missing the ability to have the action class provide a form 
bean for presentation that is different from the form bean passed in for the 
business logic to act upon.

Basically I wanted to have the following use case:

- User is on page tuna in their browser.
- User fills out the form and hits submit.
- Struts gets the request and fills out a TunaForm object, and then passes it 
to TunaAction.
- TunaAction acts upon the request, and then prepares SwordfishForm.
- Struts then gets swordfish.jsp and fills out some default values from the 
SwordFishForm that TunaAction made and filled out.
- Struts sends the resulting HTML back, and it reaches the user's browser.
- User makes some modifications and/or additions and hits submit.
- Struts gets the request and fills out a SwordfishForm object, and then 
passes it to SwordfishAction.
- SwordfishAction acts upon the request and does whatever.

As far as I can tell, inside TunaAction I need to make a SwordfishForm and 
then save it on the request:

public ActionForward perform(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response) {

    TunaForm workForm = (TunaForm)form;
    // ... do something with workForm

    // Now we've done our business logic so make the form for our next view
    SwordfishForm outForm = new SwordfishForm();
    // ... fill out data pieces here
    // Now set into the request
    request.setAttribute("swordfishForm", outForm);

    // All done
    return (mapping.findForward("success"));
}


I thought at this point I could just let Struts fill out swordfish.jsp 
automatically from SwordfishForm like this:


<html:form action="someOtherAction.do" name="swordfishForm" 
type="SwordfishForm">
    <table>
        <tr>
            <th>Name</th>
            <td>
                <html:text property="name"/>
            </td>
        </tr>
        <tr>
            <th>Color</th>
            <td>
                <html:text property="color"/>
            </td>
        </tr>
    </table>
    <br>
    <html:hidden property="id"/>
    <html:submit value="Set"/>
</html:form>


But if I do this, the data doesn't get filled out (the form I put into the 
request is different from the one associated in the struts config file I 
guess?)

If I do this:


<html:form action="someOtherAction.do" name="swordfishForm" 
type="SwordfishForm">
    <table>
        <tr>
            <th>Name</th>
            <td>
                <html:text property="name" 
value="<%=swordfishForm.getName()%>"/>
            </td>
        </tr>
        <tr>
            <th>Color</th>
            <td>
                <html:text property="color" 
value="<%=swordfishForm.getColor()%>"/>
            </td>
        </tr>
    </table>
    <br>
    <html:hidden property="id" value="<%=swordfishForm.getId()%>"/>
    <html:submit value="Set"/>
</html:form>

It will fill out name and color, but the hidden property causes it to throw an 
exception.

/home/projects/tomcat/work/Standalone/localhost/tuna/swordfish_jsp.java:153: 
Cannot resolve symbol
Symbol   : Method setValue (int)
Location : Class org.apache.struts.taglib.html.HiddenTag
              _jspx_th_html_hidden_0.setValue(swordfishForm.getId());

This is also kind of ugly because now I have to mix java code and html, 
something I'd rather avoid if possible.

That's where I'd gotten the idea to do this:


<jsp:useBean id="swordfishForm" scope="request" class="SwordfishForm"/>

<html:form action="someOtherAction.do" name="swordfishForm" 
type="SwordfishForm">
    <table>
        <tr>
            <th>Name</th>
            <td>
                <html:text property="name" value="<bean:write 
name="swordfishForm" property="name">"/>
            </td>
        </tr>
        <tr>
            <th>Color</th>
            <td>
                <html:text property="color" value="<bean:write 
name="swordfishForm" property="color">"/>
            </td>
        </tr>
    </table>
    <br>
    <html:hidden property="id" value="<bean:write name="swordfishForm" 
property="id">"/>
    <html:submit value="Set"/>
</html:form>


Unfortunately, this doesn't work either since the taglibs don't appear to 
allow embedded taglib tags...



2003 3月 20 木曜日 19:24、shirishchandra.sakhare@ubs.com さんは書きました:
> Hi,
> There was some similar question on the mailing list a long back and i am
> reaending the answers i had given...Let me know if u have any doubts.. But
> please be a  bit patient and go through the mails ..MAy be first have a
> look at the jsp i have given at the end first and then go through the rest
> of the mails..
>
> Hope this helps..
>
> regards,
> Shirish


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