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 ActionForm question (struts 1) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "ActionForm question (struts 1)" Watch "ActionForm question (struts 1)" New topic
Author

ActionForm question (struts 1)

ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
The first page of my app is /myapp/showOption, it has a drop-down list containg some dat that is retrieved from database. so in struts-config.xml I set

<action path="/showOption" type="...MyAction", name="myForm", input="show.jsp">
<forward name="success" path="/showOption"/>
</action>

The "myForm" maps to a Form class defined in <form-bean>

when user clicks "/showOption" link, it triggers "MyAction" class' "execute" method, it gets data from database and do the setting methods on the form, show.jsp displays the values in drop down list.

On "show.jsp", there is a "update" button, user can pick one item from the options list and clcik "update", it saves this new value into another table. so the button links another action "/updateOption". Now, my question is --

I create another mapping for "/updateOption"--

<action path="/updateOption" type="...UpdateAction", name="myForm", input="show.jsp">
<forward name="success" path="/showOption"/>
</action>

I want that, after clicks "update", it stays on the "show.jsp" page. Question is --

For "update" action, can I use the SAME action form for display action ? The display form has an attribute that is a List of values. But for "update" action, I just need to input the selected value to the action. If using the same form, how do I get the selected item in the "execute" method of "update" action class ?
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Yes of course you can use the same form even the same action class.

To read the selected value just get it from the Dropdown list property from the ActionForm

If you have options to select multiple values in the Dropdown list, it will be returned as Array i think.

You can even use the same action instead of new one.

just create a hidden field and create a appropriate proerty in the ActionForm also.

So when you click the update button set the hidden field to some value and check that value inthe action class if the value is set then you have to update the data into the DB else the request is to retrieve the data from DB to display them in the Dropdown box.This will limit the number of Actions.

e.g. <html:select property="userType">
<html: options collection="types" property="typeid" labelProperty="typename"/>
</html:select>

then read the property userType to get the selected value in the action class.


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
appreciate it.
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
do you think this will work ?

public class MyForm extends ActionForm {
private String selectedItem;

private List items;
}


<html:select property="selectedItem" >
<htmlptionsCollection name="myForm" property="items" value="id" label="name"/>
</html>

where "items" is collection of object of the following class --

public class VO {

private string id;
private string name;
}

so in the action class, I set the values for the "items" attribute of "MyForm" but leave "selectedItem" there, then when user selects an item, this attribute (selectedItem) of the form will be set and it can be carried to the next action after user clicks submit button. Does this make sense ?
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Yes,It should work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ActionForm question (struts 1)