File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Struts +retrieving values from text area Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts +retrieving values from text area" Watch "Struts +retrieving values from text area" New topic
Author

Struts +retrieving values from text area

A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Hi all I am working with struts 1.2.9 and trying to figure out why I can't retrieve a value from a text area in a form. I can pull and put values that aren't in a text area and I can update the value in the text area, but I can't seem to get the right syntax to pull the value from the form. I have the following JSP code:

<logic:notEmpty name="ProdSelectionForm" property="results">
<logic:iterate id="counter" name="prodSelectionbean" property="results">
<tr>
<td><bean:write name="counter" property="product" /></td>
<td><bean:write name="counter" property="description" /> </td>
<td><html:text indexed="true" name="counter" property="numProducts" size="15" maxlength="15"/></td>
</tr>
</logic:iterate>
</logic:notEmpty>


The numProducts is where I have the problem. I am trying to use code such as :

ProdSelectionForm prodSelection = (ProdSelectionForm) form;
prodSelection.getNumProducts();

This however isn't giving me any values even though there are numbers within numProducts.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
It appears that you don't have a clear understanding about how indexed properties work in Struts. Here are a couple of links that should give you an idea of how they work:

http://struts.apache.org/1.2.9/faqs/indexedprops.html

http://wiki.apache.org/struts/StrutsCatalogLazyList

If you still have questions, let us know.


Merrill
Consultant, Sima Solutions
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Looking at the JSP code that you posted, it looks basically correct. It seems like the name attributes on your notEmpty tags and iterate tags should be the same. Maybe it would help if you posted your ActionForm code. The snippet of Java code that you posted does not seem to match up with the JSP. Your JSP would translate into Java code like this (I think):

prodSelection.getCounter(index).getNumProducts();

- Brent
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Originally posted by Brent Sterling:
Looking at the JSP code that you posted, it looks basically correct. It seems like the name attributes on your notEmpty tags and iterate tags should be the same. Maybe it would help if you posted your ActionForm code. The snippet of Java code that you posted does not seem to match up with the JSP. Your JSP would translate into Java code like this (I think):

prodSelection.getCounter(index).getNumProducts();

- Brent


You are right I should have posted more of my java code, as it would make a bit more sense. What I am doing is the following:

int count = 0;
// take in the number of items in the order ArrayList and iterate
through them
while(count < order.getProductsSize() )
{
Product p = null;
//pull the product from products at each position to change them.
p = (Product) products.get(count);

// set the order amount to be what the old amount was on top of
the amount that is currently on the screen
p.setNumProducts(p.getNumProducts()+prodSelection.getNumProducts());

// the below code was to test and see if the variable would
change at all, with the +100 it would increment the amount on
the screen by 100 everytime the action was called, so that
lead me to believe that I am setting the values right.

//p.getNumProducts() + 100 + prodSelection.getNumProducts() );

// set the product back into the product arraylist so it can
be passed to the
products.set(count, p);
count++;
}

//set the product arrayList back into order so it can be presented on the screen.
order.setProducts(products);
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Originally posted by Merrill Higginson:
It appears that you don't have a clear understanding about how indexed properties work in Struts. Here are a couple of links that should give you an idea of how they work:

http://struts.apache.org/1.2.9/faqs/indexedprops.html

http://wiki.apache.org/struts/StrutsCatalogLazyList

If you still have questions, let us know.


As always Merrill you are a fountain of knowledge, and I thank you for it. I am working through the info on the wiki page, and of course I am getting an index out of bounds exception. I am reading the page on the wiki about that, but it isn't quite making sense to me. From what I read it is now an issue with my form, and that there is an issue with dynamically sized arrays, but from what I understand arraylist should be dynamic enough to shrink and grow as needed. I am continuously getting the error on index 6 size 0. I will continue to dig into it in the mean time thanks again for all the ideas. I have modified the jsp code so that it is as follows:


<logic:notEmpty name="ProdSelectionForm" property="results">
<logic:iterate name="ProdSelectionForm" property="results" id="results">
<tr>
<td><bean:write name="results" property="product" /></td>
<td><bean:write name="results" property="description" /></td>
<td><html:text name="results" property="numProducts"indexed="true" size="15" maxlength="15"/></td>
</tr>
</logic:iterate>
</logic:notEmpty>

*Edit* Fixed typos
[ October 03, 2006: Message edited by: A knibbs ]
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Yea, that wiki page is pretty good. It took me a long time to figure out this tip:

Struts html tags have an indexed attribute which will generate the appropriate html to populate a collection of beans when the form is submitted. The trick is to name the id attribute to the same as the indexed property.

What strategy are you using? I have always used the strategy explained in the "Hand Cranking lazy List in the ActionForm" section. Keep in mind that the index values will not be submitted in order. Post your ActionForm and I am sure somebody will spot the problem.

- Brent
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
{
//pull the session
HttpSession session = request.getSession();

//pull the form
ProdSelectionForm prodSelection = (ProdSelectionForm) form;

//create a user, order and product array
User user;
Order order = new Order();
ArrayList products = new ArrayList();

//pull the user, order and the product array
user = (User)session.getAttribute("User");
order = user.getOrder("ID12");
products = order.getProducts();

int count = 0;
//while we still have products to iterate through
while(count < order.getProductsSize() )
{
Product p = null;
// pull the product from the arraylist
p = (Product) products.get(count);

// set the number of products based on what was put on the form.
p.setNumProducts( prodSelection.getNumProducts(count) );

// set the product back into the product array
products.set(count, p);
count++;
}

//set the product array back in the order.
order.setProducts(products);

//set the order and put it back into the user.
order.setOrderId("ID12");
user.changeOrder(order, "ID12");

// put the result back into the form page.
prodSelection.setResults(products);
session.setAttribute("prodSelection", prodSelection);

return mapping.findForward("success");
}
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Thanks for showing us your Action class code, but it's actually the code in your ProdSelectionForm that we're more interested in. It's the coding of the methods of this class that can prevent you from getting that "Index out of bounds" exception.

Incidentally, one solution that we haven't discussed much is changing your action mapping so that the ActionForm is in session scope. This will also eliminate the "Index out of bounds" exception. Brent and I tend to disagree somewhat on this issue, but I think it's perfectly OK to store the ActionForm in the session, particularly in cases where the model being represented by the ActionForm is complex and multi-layered. If you do this, it's important to remove the object from the session once you're done with it in order to keep memory usage under control.
[ October 04, 2006: Message edited by: Merrill Higginson ]
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
This should be all of the relevant code - trying to keep it as concise as possible, and once again thanks for all of the help.

/**
* @return Returns the numProduct.
*/
public int getNumProducts() {
return numProducts;
}
/**
* @return Returns numProduct at the specified index
*/
public int getNumProducts(int index) {
int returnVal = 0;
if(results.size() != 0)
{
Product p = (Product) results.get(index);
returnVal = p.getNumProducts();
}
return returnVal;
}

/**
* @param numProducts The numProduct to set.
*/
public void setNumProducts(int numProducts) {
this.numProducts = numProducts;
}

/**
* @param reset the page
*/

public void reset(ActionMapping mapping,HttpServletRequest request)
{
this.searchString=null;
this.results = new ArrayList();
this.pageNum = 0;
this.description = "dfasdasdtest";
this.numProducts = 0;
}

Thanks for showing us your Action class code, but it's actually the code in your ProdSelectionForm that we're more interested in. It's the coding of the methods of this class that can prevent you from getting that "Index out of bounds" exception.

Incidentally, one solution that we haven't discussed much is changing your action mapping so that the ActionForm is in session scope. This will also eliminate the "Index out of bounds" exception. Brent and I tend to disagree somewhat on this issue, but I think it's perfectly OK to store the ActionForm in the session, particularly in cases where the model being represented by the ActionForm is complex and multi-layered. If you do this, it's important to remove the object from the session once you're done with it in order to keep memory usage under control.

I am looking for something to work right now. I unfortunately don't know enough of the finer details of struts to know which is best, so I am working with what i know until I encounter a problem at which time I look for a more elegant/robust/scaleable solution. I suspect this is how most people work on their apps.
[ October 04, 2006: Message edited by: A knibbs ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If you just want to get this working, change the action mapping in your struts-config.xml file to specify scope="session". That should keep you from getting the "Index out of bounds" exception.

Once you've got it working, if you decide it's important to you to have the form in request scope, study the link that I gave you carefully, and you'll understand how to give your AcgtionForm lazy initialization behavior.
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Originally posted by Merrill Higginson:
If you just want to get this working, change the action mapping in your struts-config.xml file to specify scope="session". That should keep you from getting the "Index out of bounds" exception.

Once you've got it working, if you decide it's important to you to have the form in request scope, study the link that I gave you carefully, and you'll understand how to give your AcgtionForm lazy initialization behavior.


I definately want to get this working, but more importantly I want to get it working properly following the best practices I can. I am going to try the scope="session" for right now, and see if I can get that working at the very least. I do appreciate all of the help that you have all given me. Guess I am just a little bit thicker than the person that the explanation was written for. I am almost guarenteed it is something rediculously simple.


thanks for now, I'll continue to work on this and hopefully come back with good news.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
What I was looking for was the implementation of your getResults method. Do you have a method that looks like this?


- Brent
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
I was playing around with this again today, and low and behold I got it to work. I was hoping that I could run this by the 2 of you in particular, but anyone else who is interested in reading it. I think I have found my major problem. The problem was that the following: (Please correct me if I am wrong)
When a page is called the reset is also called- the reset on my page wasn't creating the array whatso-ever, so when the getResults for the page was called there wasn't any array to pull data from. Now the issue that I have run into is that I am not sure if I have solved this the proper way, and I was also wondering how I was going to pull the relevant information to setup the reset page properly, but I have also just had an idea for that. Hopefully this makes some semblance of sense, but as of right now it works, and that includes using scope=request for in the struts-config for that form. Hopefully the idea I have to solve my one issue will be able to be solved with my idea as well.

thanks so much for all the time and suggestions you have both provided. It certainly makes me want to become a part of the javaranch group.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Yea, right. That is the basic problem. The wiki page linked above is a little confusing, but that is the basic problem that people have tried to solve with various methods. The example that I posted above is based on a "template" that I use and it is similar to the version in the wiki in the section "Hand Cranking lazy List in the ActionForm".

The second example shown on that page that uses ListUtils.lazyList looks pretty good (the first one has your form implement Factory, which seems like a bad idea). I say that one of these days I will give it a try, but I have not had any issues hand cranking them.

I can't say if you solved it the correct way without seeing your solution.

- Brent
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Struts +retrieving values from text area