wood burning stoves 2.0*
The moose likes Struts and the fly likes using Multiple submit buttons. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "using Multiple submit buttons." Watch "using Multiple submit buttons." New topic
Author

using Multiple submit buttons.

lubna kausar
Ranch Hand

Joined: May 06, 2006
Posts: 51
i m using multiple submit buttons on my jsp
it includes buttons for ADD, EDIT and DELETE
now i want to ask how to let them executed in a single ACTION class???
i read somewhere that we can have the methods of the names that can be bound with the Buttons on JSP but doing that in struts 1.2.9 is not working...
i just want to know the exact way of doing this...
i m attaching my code of the jsp n Action here please do let me know whether it is right way to do it or not???

NOTE : PLEASE CONSIDER 'lessthan' and 'greaterthan' signs as required coz its not allowing me to add that signs...
and Execute is actually execute
public class UserAction extends Action {
/**
*
*/
public UserAction() {
super();
}



/* (non-Javadoc)
* @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/

public UserBean createBean(HttpServletRequest request)
{
String password=null;
String contactNo=request.getParameter("countryCode")+'-'+request.getParameter("areaCode")+'-'+request.getParameter("phoneNo");
password=PasswordUtil.generatePassword();
UserBean ub = new UserBean();
ub.setUserName(request.getParameter("userName"));
ub.setUserPassword(password);
ub.setRoleId((int) 2);
//ub.setRoleId(Integer.parseInt(request.getParameter("roleId")));
ub.setUserEmailID(request.getParameter("userEmailID"));
ub.setUserAlias(request.getParameter("userAlias"));
ub.setFirstName(request.getParameter("firstName"));
ub.setLastName(request.getParameter("lastName"));
ub.setContactNo(contactNo);
ub.setLocation(request.getParameter("location"));
ub.setExtensionNo(request.getParameter("extensionNo"));
ub.setMobileNo(request.getParameter("mobileNo"));
return ub;
}

public ArrayList createBean(ArrayList userList)
{
return userList;
}

public ActionForward Execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
ArrayList userList=new ArrayList();
userList=UserBusiness.findAll();
request.setAttribute("users",userList);
return mapping.findForward("success");
}

public ActionForward save(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
boolean res=false;
boolean res1=false;
UserBean ub=createBean(request);
res1=UserBusiness.validateUser(request,ub);
if(res1){
res=UserBusiness.addRecord(ub);
}
if(res)
{
return mapping.findForward("success");
}
else
{
return mapping.findForward("faliour");
}
}
/**
* Method edit
* @param ActionMapping mapping
* @param ActionForm form
* @param HttpServletRequest request
* @param HttpServletResponse response
* @return ActionForward
* @throws Exception

*/
public ActionForward edit(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
boolean res=false;
boolean res1=false;
UserBean ub=createBean(request);
res1=UserBusiness.validateUser(request,ub);
if(res1)
{
res=UserBusiness.updateRecord(ub);
}
if(res)
{
return mapping.findForward("success");
}
else
{
return mapping.findForward("faliour");
}
}

/**
* Method edit
* @param ActionMapping mapping
* @param ActionForm form
* @param HttpServletRequest request
* @param HttpServletResponse response
* @return ActionForward
* @throws Exception
*/
public ActionForward delete(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
boolean res=false;
UserBean ub=createBean(request);
res=UserBusiness.updateRecord(ub);
if(res)
{
return mapping.findForward("success");
}
else
{
return mapping.findForward("faliour");
}
}
}
[ May 09, 2006: Message edited by: lubna ]
Soumya Saha
Ranch Hand

Joined: Apr 14, 2006
Posts: 64
you can use this code for different cases...but all the buttons are only buttons not submit...u can try the same logic with submit button also..it should work in Action Class..
(Greater than signs replaced with "<" , Forward slash replaced with "<" , onclick replaced with here)
JSP
"<" button class="pgBtn" id="Save" name="Add" value="Add" type="button" here="javascript:save('add');""<" Save"<" "<" button"<"
"<" button class="pgBtn" id="Save" name="Edit" value="Edit" type="button" here="javascript:save('Edit');""<" Save"<" "<" button"<"
"<" button class="pgBtn" id="Save" name="Delete" value="Delete" type="button" here="javascript:save('del');""<" Save"<" "<" button"<"


JS:
function save(param){
document.forms[0].submit();
if(param=='add'){
document.forms(0).Delete.disabled=true;
document.forms(0).Edit.disabled=true;
}
...similar for other cases..
}

// then only when add is clicked add will be executed..and other will be disabled..

Action
String btnPressedadd = null;
String btnPressededit = null;
String btnPresseddelete = null;

btnPressedadd = request.getParameter("Add");
btnPressededit = request.getParameter("Edit");
btnPresseddelete = request.getParameter("Delete");

// Default page load Condition:
if(btnPressedadd != null && btnPressededit != null && btnPresseddelete != null)
{
.. logic for default
}

if(btnPressedadd != null && btnPressedadd.equalsIgnoreCase("Add"))
{
.. logic for add
}
// Same for Others
Best of Luck..

lubna kausar
Ranch Hand

Joined: May 06, 2006
Posts: 51
hi there i tried ur code but giving error on clicking the button...
error that i get is
object expected

m writing here the code of my JSP do let me know wats wrong here...
""<""!-- ""<""!DOCTYPE html PUBLIC "-"/""/"W3C"/""/"DTD XHTML 1.0 Transitional"/""/"EN"

"http:"/""/"www.w3.org"/"TR"/"xhtml1"/"DTD"/"xhtml1-transitional.dtd">
""<""%@ page language="java" contentType="text"/"html; charset=UTF-8" %>
""<""%@ taglib uri=""/"WEB-INF"/"struts-html.tld" prefix="html" %>
""<""!--""<""%@ taglib uri=""/"WEB-INF"/"struts-logic.tld" prefix="logic" %>-->
""<""%@ taglib uri=""/"WEB-INF"/"struts-bean.tld" prefix="bean" %>
""<""html:html xhtml="true" locale="true">


""<""head>

""<""meta http-equiv="Content-Type" content="text"/"html; charset=UTF-8" "/">

""<""title>UserInfo""<"""/"title>
""<""html:base"/">
""<""LINK rel="stylesheet" type="text"/"css" href=""/"body1.css" "/">
""<"""/"head>
""<""script language="javascript">
function getButton(pressedButton)
{
document.forms[0].submit();
if(pressedBotton=='edit')
{
documents.forms(0).delete.disabled=true;
documents.forms(0).save.disabled=true;
}
if(pressedBotton=='delete')
{
documents.forms(0).edit.disabled=true;
documents.forms(0).save.disabled=true;
}
if(pressedBotton=='save')
{
documents.forms(0).edit.disabled=true;
documents.forms(0).delete.disabled=true;
}
}
""<"""/"script>

""<""body>
""<""html:errors"/">
""<""html:form action=""/"user">
""<""table align=center>
""<""tr>
""<""td>
""<""table align=center border=0 valign=center class="Body_subtitletext" >
""<""caption>
USER INFORMATION""<"""/"caption>
""<""tr>
""<""td align=right>""<""font color=blue>* ""<"""/"font>User Name:  ""<"""/"td>
""<""td>""<""html:text property="userName" size="40" maxlength="50""/">""<"""/"td>
""<"""/"tr>
""<""tr>
""<""td align=right>""<""font color=blue>* ""<"""/"font>Role:  ""<"""/"td>
""<""td>""<""html:radio property="roleId" value="1">Admin"<""/"html:radio>
"<"html:radio property="roleId" value="2">Normal"<""/"html:radio>"<""/"td>
"<""/"tr>

"<"tr>

"<"td align=right>"<"font color=blue>* "<""/"font>Email Address:  "<""/"td>
"<"td>"<"html:text property="userEmailID" size="40" maxlength="50""/">@patni.com"<""/"td>

"<""/"tr>


"<""/"table>

"<"table align=center border=0 valign=center class="Tablestyle" >
"<"tr class="Body_subtitletext" >
"<"td align=right colspan=1>User Alias:"<""/"td>
"<"td colspan=4>"<"html:text property="userAlias" size="40" maxlength="50""/">"<""/"td>
"<"tr>"<"td>"<""/"td>"<""/"tr>"<"tr>"<"td>"<""/"td>"<""/"tr>"<"tr>"<"td>"<""/"td>"<""/"tr>
"<""/"tr>
"<"tr class="Body_subtitletext" >
"<"td align=right colspan=1>Full Name:"<""/"td>
"<"td colspan=4>
"<"html:text property="firstName" size="20" maxlength="30""/">  
"<"html:text property="middleName" size="2" maxlength="2""/">  
"<"html:text property="lastName" size="20" maxlength="30""/">  "<""/"td>
"<""/"tr>
"<"tr>"<"td>"<""/"td>"<""/"tr>"<"tr>"<"td>"<""/"td>"<""/"tr>"<"tr>"<"td>"<""/"td>"<""/"tr>

"<"tr class="Body_subtitletext" >
"<"td align=right>Contact No:"<""/"td>
"<"td colspan=2>"<"html:text property="countryCode" size="5" maxlength="5""/">
"<"html:text property="areaCode" size="6" maxlength="6""/">
"<"html:text property="phoneNo" size="15" maxlength="15""/">
"<""/"td>
"<""/"tr>
"<"tr>"<"td>"<""/"td>"<""/"tr>"<"tr>"<"td>"<""/"td>"<""/"tr>"<"tr>"<"td>"<""/"td>"<""/"tr>

"<"tr class="Body_subtitletext" >
"<"td align=right>Location:"<""/"td>
"<"td colspan=2>"<"html:text property="location" size="20" maxlength="30""/">
"<""/"td>
"<""/"tr>
"<"tr>"<"td>"<""/"td>"<""/"tr>"<"tr>"<"td>"<""/"td>"<""/"tr>"<"tr>"<"td>"<""/"td>"<""/"tr>

"<"tr class="Body_subtitletext" >
"<"td>"<"align=right>Extension No:"<""/"td>
"<"td colspan=2>"<"html:text property="extensionNo" size="20" maxlength="30""/">
"<""/"td>
"<""/"tr>
"<"tr>"<"td>"<""/"td>"<""/"tr>"<"tr>"<"td>"<""/"td>"<""/"tr>"<"tr>"<"td>"<""/"td>"<""/"tr>

"<"tr class="Body_subtitletext" >
"<"td align=right>Mobile No:"<""/"td>
"<"td colspan=2>"<"html:text property="mobileNo" size="20" maxlength="30""/">
"<""/"td>
"<""/"tr>
"<"tr>"<"td>"<""/"td>"<""/"tr>"<"tr>"<"td>"<""/"td>"<""/"tr>"<"tr>"<"td>"<""/"td>"<""/"tr>

"<"tr>"<"hr>"<""/"tr>
"<""/"table>"<"br>
"<"table align=center>
"<"tr class="Body_subtitletext" >

"<"td align=center>"<"html:reset styleClass="BUTTON""/">"<""/"td>

"<"td align=center>
"<"html:button styleClass="BUTTON" property="editsubmit" value="Edit" onklick="javascript:getButton('edit');""/">
"<""/"td>

"<"td align=center>
"<"html:button styleClass="BUTTON" property="deletesubmit" value="Delete" onklick="javascript:getButton('delete');""/">
"<""/"td>

"<"td align=center>
"<"html:button styleClass="BUTTON" property="savesubmit" value="Save" onklick="javascript:getButton('save');""/">
"<""/"td>

"<"td align=center>"<"html:cancel styleClass="BUTTON""/">"<""/"td>

"<""/"tr>
"<""/"table>
"<""/"td>
"<"td>
"<""/"td>
"<""/"tr>
"<""/"table>
"<""/"html:form>
"<""/"body>
"<""/"html:html>
ram gaurav
Ranch Hand

Joined: Mar 29, 2006
Posts: 208
Hey ..

The think which you want to do can be achieve by extending DispatchAction class not the Action class , which you have done in your code.

If you can do then fine otherwise tell me , i will paste here an example for you.

Best of Luck
Regards
Gaurav
Soumya Saha
Ranch Hand

Joined: Apr 14, 2006
Posts: 64
Hi,
It should support name and id even if it is struts..and JSP.
anyway you can try this(Onclick replaced with here)

"<"td width="17%""<"
"<"html:button class="BUTTON" name="Save" value="Save" here="javascript:getButton('save');""<"Save"<"/html:button"<"
"<"/td"<"

...if it does not wotk then give the exact error..Object expected can be only there it it is not getting the property of the button...
Soumya Saha
Ranch Hand

Joined: Apr 14, 2006
Posts: 64
Make sure u replace the Html escape sequence : "&:lt" with proper tags.(Greater than and less than symbols where ever applicable...).check thoroughly for compilation errors...
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
"lubna",

Welcome to JavaRanch. We don't have many rules here but we do have a naming policy that we strictly adhere to. Please re-read this naming convention and change your display name in order to comply. Thanks in advance.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: using Multiple submit buttons.