Hi,

I made the following changes in my Action class and got my save to work.

HttpSession session = request.getSession();
TestGridForm testGridForm = (TestGridForm)session.getAttribute(
"testGridForm" );

However, while trying to debug my problem I found I get 3 ActionForm Beans
created when I only expected 1.

In my jsp I have the following:

<jsp:useBean id="testGridForm" class="com.qspgroup.utc.TestGridForm"
scope="session" />

I have added scope="session to the logic:iterate tag e.g. 
<struts_logic:iterate id="element" name="testGridForm" property="data"
scope="session" >

On the page where I display the results I also have:
<jsp:useBean id="testGridForm" class="com.qspgroup.utc.TestGridForm"
scope="session" />

I expected all the above to refer to the session scoped bean "testGridForm",
so why are 3 beans created?

Richard.

-----Original Message-----
From: Richard Murray [mailto:[EMAIL PROTECTED]]
Sent: Thursday, May 10, 2001 9:45 AM
To: '[EMAIL PROTECTED]'
Subject: RE: How do you use indexed properties with html:input?


Turgay,

Thanks for your help, I've tried something similar to your example, but I'm
still having some trouble.  I've probably missed something obvious as I'm
new to struts & JSP.

In my jsp I've got the following:
<%@ page import="com.qspgroup.utc.TestGridForm" errorPage="fcerror.jsp" %>
<%@ taglib uri="/WEB-INF/utctags/fcutc.tld" prefix="fcutc" %>
<%@ taglib uri="/WEB-INF/lib/struts/struts-bean.tld" prefix="struts_bean" %>
<%@ taglib uri="/WEB-INF/lib/struts/struts-html.tld" prefix="struts_html" %>
<%@ taglib uri="/WEB-INF/lib/struts/struts-logic.tld" prefix="struts_logic"
%>
<jsp:useBean id="testGridForm" class="com.qspgroup.utc.TestGridForm"
scope="session" />
........
........
<tr>
  <% int i=0; %>
  <struts_logic:iterate id="element" name="testGridForm" property="data" >
    <td>
    <input type="text" name="<%= "data" + i %>" size="10"
value="<struts_bean:write name="element"/>">
    </td>
    <% i++; %>
  </struts_logic:iterate>
</tr>

My action class is as follows:

public ActionForward perform( ActionMapping mapping, ActionForm form,
HttpServletRequest request,
                                      HttpServletResponse response ) throws
IOException, ServletException
{
        if ( form == null )
      {
        form = new TestGridForm();
      }

      TestGridForm testGridForm = ( TestGridForm )form;
                               
      for ( int i = 0; i < testGridForm.getData().length; i++ )
      {
        if ( request.getParameter( "data" + i ) != null )
            {
                testGridForm.setData( i, request.getParameter( "data" + i )
);
            }
      }
      return ( mapping.findForward( "success" ));
}

As you can see It doesn't do a lot.  My ActionForm class creates dummy data
and contains and contains get/set methods as shown below:

public class TestGridForm extends ActionForm
{
        String[] data = { "Rail", "Fridges", "Hay", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "10", "11", "12", "13" };

        public String[] getData()
        {
                return data;
        }

        public String getData( int i )
        {
                return data[i];
        }

        public void setData( String[] data )
        {
                this.data = data;
        }

      public void setData( int i, String d )
      {
                data[i] = d;
      }

        
My data gets displayed OK.  On success I go to testgrid.jsp but this keeps
displaying the original data.  I've checked the tomcat log files and the new
data is being sent as request parameter so I don't understand why the new
data isn't saved in my bean.
e.g. 2001-05-10 09:21:20 -        Request Params: 
2001-05-10 09:21:20 -            data2 = 30
2001-05-10 09:21:20 -            data1 = 20

TestGrid.jsp is as follows:

<%@ page import="com.qspgroup.utc.DataCaptureDemo" errorPage="fcerror.jsp"
%>
<jsp:useBean id="testGridForm" class="com.qspgroup.utc.TestGridForm"
scope="session" />


<html>
<head>
<title>Test JSP</title>
</head>

<body>
<h2>Test JSP Page</h2>

<%      
        out.println( "<br>data in 1st cell is "+ testGridForm.getData( 0 )
);
        out.println( "<br>data in 2st cell is "+ testGridForm.getData( 1 )
);
        out.println( "<br>data in 3rd cell is "+ testGridForm.getData( 2 )
);    
%>

</body>
</html>

Sorry to swamp you with info, I can't seem to figure this out, although I'm
sure I've missed something obvious here.
Thanks.

Richard.

-----Original Message-----
From: Turgay Zengin [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, May 09, 2001 1:13 PM
To: [EMAIL PROTECTED]
Subject: Re: How do you use indexed properties with html:input?


Hi,
I *guess* that the html:xxxx tags require a form bean (please correct me if 
this is wrong) so they won't work with dynamically created page elements 
like input text fields.

I did the following and it works, got the idea from Scott Walter (thanks) - 
see http://www.mail-archive.com/struts-user@jakarta.apache.org/msg06972.html

There may be a better solution to this, if there is, I would like to know.
Background: "dbconnection" is a bean I use to keep the session's database 
connection, and other objects. wpLaborEntries is a vector holding objects of

type WpLabor.

In my jsp page I create input text fields like this:

<FORM METHOD="POST" ACTION="UpdateWoplan.do">
<% int i=0; %>
<input type="submit" value="Update_Fields">
<table border="1" cellpadding="0" cellspacing="0" align="center">
<logic:iterate name="dbconnection" property="wpLaborEntries"
               id="wpl" type="com.tz.maximo.woplan.WpLabor" >
<tr>
          <td><INPUT TYPE="text" NAME="<%="craft"+i%>" size="5"
              value="<bean:write name="wpl" property="craftqty"/>"></td>
          <td><INPUT TYPE="text" NAME="<%="lhrs"+i%>" size="5"
              value="<bean:write name="wpl" property="laborhrs"/>"></td>
          <td><INPUT TYPE="text" NAME="<%="w1"+i%>" size="5"
              value="<bean:write name="wpl" property="wpl1"/>"></td>
          <td><INPUT TYPE="text" NAME="<%="w2"+i%>" size="5"
              value="<bean:write name="wpl" property="wpl2"/>"></td>
</tr>
<% i++; %>
</logic:iterate>
</table>
</FORM>

So, the key point is, giving a "consistent" name for each input text field.

And, in my action class, I get the values like this:
public ActionForward perform(ActionMapping mapping,
                             ActionForm form,
                             HttpServletRequest request,
                             HttpServletResponse response)
{
   HttpSession session = request.getSession();
   OracleConnBean 
dbconn=(OracleConnBean)session.getAttribute("dbconnection");
   for(int i=0;i<dbconn.getWpLaborEntries().size();i++){
     WpLabor wplx=(WpLabor)dbconn.getWpLaborEntries().get(i);
     if(request.getParameter("craft"+i)!=null)
         wplx.craftqty=request.getParameter("craft"+i);
     if(request.getParameter("lhrs"+i)!=null)
         wplx.laborhrs=request.getParameter("lhrs"+i);
     if(request.getParameter("w1"+i)!=null)
         wplx.wpl1=request.getParameter("w1"+i);
     if(request.getParameter("w2"+i)!=null)
         wplx.wpl2=request.getParameter("w2"+i);
   }
   dbconn.updateValues();
   if (dbconn.successfulQuery=="true") {
      return mapping.findForward("success");
   }
   else {
      return mapping.findForward("failure");
   }
}

Hope this helps (and it is clear!)...
Turgay.

>From: Richard Murray Subject: How do you use indexed properties with 
>html:input? Date: Tue, 08 May 2001 08:44:41 -0700

----------------------------------------------------------------------------
----

>Hi,

>I've got the following property:

>        String[] data.

>I want to iterate the array and display each String in a text input >field 
>in
>a table. I can get the data to display using iterate and a bean:write >tag 
>as
>follows:

><struts_logic:iterate id="element" name="testGrid" property="data" >
>        <td><struts_bean:write name="element" /></td>
></struts_logic:iterate>

>I can't get it working with a text input field ( html:text ).  I've >tried
>the following and get javax.servlet.jsp.JspException: No getter method >for
>property element of bean org.apache.struts.taglib.html.BEAN

>I want to use property="data", but how can I can specify which element >of
>the array to output and save on an update?

><struts_logic:iterate id="element" name="testGrid" property="data" >
>        <td><struts_html:text property="element" /></td>
></struts_logic:iterate>

>Hope someone can help.

>Richard.



_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager at [EMAIL PROTECTED]

This footnote also confirms that this email message has been swept by
for the presence of computer viruses.

http://www.qspgroup.com
**********************************************************************

Reply via email to