aspose file tools*
The moose likes Struts and the fly likes Form Elements and Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Form Elements and Arrays" Watch "Form Elements and Arrays" New topic
Author

Form Elements and Arrays

Tim Esch
Greenhorn

Joined: Aug 01, 2011
Posts: 4
Greetings from Indiana!

I need a little help. I have a struts form:

<html:text size="8" maxlength="8" property="workStart[0]" />
<html:text size="8" maxlength="8" property="workEnd[0]" />

<html:text size="8" maxlength="8" property="workStart[1]" />
<html:text size="8" maxlength="8" property="workEnd[1]" />

<html:text size="8" maxlength="8" property="workStart[2]" />
<html:text size="8" maxlength="8" property="workEnd[2]" />


I also have an ActionForm:

private String [] workStart;
private String [] workEnd;



public String [] getWorkStart() {
return workStart;
}

public void setWorkStart(String workStart, int index) {
this.workStart[index] = workStart;
}

public String [] getWorkEnd() {
return workEnd;
}

public void setWorkEnd(String workEnd, int index) {
this.workEnd[index] = workEnd;
}


And, yes, I even have an ActionClass with an action forward function containing:

ScheduleForm f = (ScheduleForm) form;
request.setAttribute("WorkStart", f.getWorkStart());
request.setAttribute("WorkEnd", f.getWorkEnd());
return mapping.findForward("show");


Finally, I have a jsp page with the following code,

String [] x = (String []) request.getAttribute("WorkStart");
out.println(x);
-----------------------------------------------------------------------------------

I get a null output, I cannot figure out what I am doing wrong. I am missing something when dealing with arrays and forms and I am not quite sure what it can be. I think it may be my setter functions, because I am getting null, which leads me to believe the ActionForm is not getting populated.

Anyone have an idea's, or know how to accomplish what I am trying to get done?

Thanks for any help!!!


Tim







Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Welcome to the Ranch.

Please be sure to ask Struts questions in the Struts forum. I have moved this post there for you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tim Esch
Greenhorn

Joined: Aug 01, 2011
Posts: 4
Sorry, I am such a greenhorn, I couldn't discern what forum to post my question in! To me, JSP is the programming language, and Struts is an application framework JSP runs in. I felt it was a programming question so I elected to go with the JSP.
Shankar Tanikella
Ranch Hand

Joined: Jan 30, 2011
Posts: 329

Tim wrote:
And, yes, I even have an ActionClass with an action forward function containing:

ScheduleForm f = (ScheduleForm) form;
request.setAttribute("WorkStart", f.getWorkStart());
request.setAttribute("WorkEnd", f.getWorkEnd());
Looks like you are just initializing the form and setting few attributes in request which were never assigned. Tell me if i am wrong, you are not setting anything in your form (at least from the above code). Did you try putting a System.out before setting the parameter in the request?
Tim wrote:
Finally, I have a jsp page with the following code,

String [] x = (String []) request.getAttribute("WorkStart");
out.println(x);

Tim wrote:
I elected to go with the JSP
yes, you are right, you are not at all using struts in you are JSP.

So what i can see is you are just creating an form bean instance and setting "WorkStart" attribute from without initializing it. Populate this element in the form with some values and it would work fine.
If it is already populated in form, put a System out for "f.getWorkStart()" elements just before setting it in request.
If blank values are expected in your JSP for the first time it loads, then initialize the "WorkStart" item in the form bean to avoid null or use conditional statement (logic:empty) in JSP for display


Have Fun with Java
little,little.. little by little makes a lot..
Tim Esch
Greenhorn

Joined: Aug 01, 2011
Posts: 4
Thanks for the reply Shankar.

I added a test variable

ActionForm:
<html:text size="8" maxlength="8" property="test" />
<html:text size="8" maxlength="8" property="workStart[0]" />
<html:text size="8" maxlength="8" property="workEnd[0]" />
<html:text size="8" maxlength="8" property="workStart[1]" />
<html:text size="8" maxlength="8" property="workEnd[1]" />

Action Class:
ScheduleForm f = (ScheduleForm) form;
request.setAttribute("WorkStart", f.getWorkStart());
request.setAttribute("WorkEnd", f.getWorkEnd());
request.setAttribute("Test", f.getTest());
return mapping.findForward("show");

Show.jsp
String [] x = (String []) request.getAttribute("WorkStart");
out.println(x);
String y = (String) request.getAttribute("Test");
out.println(y);

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

I get an output value for the test variable with whatever I populated the form with. However, the Array variables are outputting a null value even if I put values into the form. So I think the problem is with my array's getter and setter functions.




Shankar Tanikella
Ranch Hand

Joined: Jan 30, 2011
Posts: 329

What was the outcome of "System out for "f.getWorkStart()" elements just before setting it in request" in your action class?
 
 
subject: Form Elements and Arrays