wood burning stoves 2.0*
The moose likes Struts and the fly likes Problem with 2 buttons in the same form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem with 2 buttons in the same form" Watch "Problem with 2 buttons in the same form" New topic
Author

Problem with 2 buttons in the same form

Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
Hi everybody,
I am working with a database that has a table of 50000 rows.I've made a form that makes researchs on this table and I am able to show all the results from the table for every research by submitting a button in the form.
Now,I need to get relevant informations as the number of rows and the total and average values for some field of this table.I have thought that a solution is to put 2 button in the form.By submitting the first button I get all the results of the researchs,if I submit the second button I get the relevant informations.
Well,the question is:
Should I create another Action to handle these relevant informations?
I have also read something about the LookupDispatchAction class.Can it work for my goal?
Thanks for any answer,
Mattia
saravanaprabu baskaran
Greenhorn

Joined: Jun 13, 2006
Posts: 7
Hi,

You can have one action class for both the buttons But, your action class should extend DispatchAction.

The action class will have one dedicated method for each button.

On clicking the button corresponding method of the action class will be called based on the parameter you pass(Parameter will have the exact method name, you will specify it in struts config file)


With Regards,<br />Saravanaprabu Baskaran<br /> <br />SCJP 92 %<br />SCWCD 98 %
Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
Hi Mr Baskaran,
can your advice work even if I don't have to use javascript in my project?
Thanks,
Mattia
saravanaprabu baskaran
Greenhorn

Joined: Jun 13, 2006
Posts: 7
Hi,

Ya its struts nothing to do with javascript.
Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
Hi Mr Baskaran,
can your advice work even if I don't have to use javascript in my project?
Thanks,
Mattia
Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
Hi Mr Baskaran,
now I am trying to do this:
Page.jsp
<html:form action="joblogs">
......
<html:submit value="results" />
<html:submit value="total" />
</html:form>

struts-config.xml
<action input="/joblogsForm.jsp"
type="com.myapp.struts.JoblogsAction"
scope="request"
path="/joblogs"
validate="true"
name="JoblogsActionForm"
parameter="dispatch" >
<forward path="/results.jsp" name="success"/>
<forward path="/totals.jsp" name="total"/>
</action>

JoblogsAction.java
public class JoblogsAction extends DispatchAction {

private final static String SUCCESS = "success";
private final static String TOTAL = "total";

For the moment I don't know how to continue and I don't know how to use the parameter "dispatch" defined in struts-config-xml file.I am reading some page for informations.Should I make to methods in my Action class to handle the 2 buttons?
Thanks,
Mattia
saravanaprabu baskaran
Greenhorn

Joined: Jun 13, 2006
Posts: 7
Hi,

Try the following code:

Page.jsp
--------
<html:form action="joblogs">
......
<a href="joblogs.do?dispatch=calculateResult"><img name="result"></a>
<a href="joblogs.do?dispatch=calculateTotal"><img name="total"></a>
</html:form>

struts-config.xml
------------------
<action path="/joblogs"
type="com.myapp.struts.JoblogsAction"
scope="request"
validate="true"
name="JoblogsActionForm"
parameter="dispatch" >

<forward path="/results.jsp" name="success"/>
<forward path="/totals.jsp" name="total"/>
</action>

JoblogsAction.java
--------------------
public class JoblogsAction extends DispatchAction {

private final static String SUCCESS = "success";
private final static String TOTAL = "total";

public ActionForward calculateResult(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
{
----- your action code here---
return (mapping.findForward(SUCCESS));
}
public ActionForward calculateTotal(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
{
----- your action code here---
return (mapping.findForward(TOTAL));
}
}
Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
Hi Mr Baskaran,
your advices were good for my project.
My project is working fine.
Thank you
Mattia
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with 2 buttons in the same form
 
Similar Threads
Adding new elements to table in struts 1.2
Maintaining the page position
Webflow2: not pre-populating the form on back button
Submitting a table in a form
populate a form with rows and save it back to DB?