working with two buttons in jsp

hi

My working with jsp's in struts.

I have a jsp with two buttons "Save" and "Delete".

My requirement is, on clicking either the "save button" or "Delete button" in jsp, it should call the corresponding code in the Action class.

wht is the logic required to call the corresponding code in the Action class ?

Thanks

[352 byte] By [goodprograma] at [2007-11-27 2:17:20]
# 1

Hi I hope this solves your problem:

use a hidden variable on the page containing the Delete and the Save buttons.

On the click of the Delete or the Save button, assign a value in the hidden variable using the Javascript function.

Use the onClick event for the two buttons. Then call a Javascript function which assigns the value "Save" for the click of the Save button and the value "Delete" on the click of the Delte button. Then submit the form.

In the Action class first access the hidden variable and depending upon the value in the hidden variable execute the specific blocks of code using if-else.

Is this enough?

JHyenaa at 2007-7-12 2:15:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

<script>

function funSave(){

document.form[0].ID.value=Id;

form[0]..action="<%=request.getContextPath()%>/division/editDesignation.app?save=save";

form[0]..method="post";

form[0]..submit();

}

function funDelete(){

function funSave(){

document.form[0].ID.value=id;

form[0]..action="<%=request.getContextPath()%>'<%=request.getContextPath%>.app?save=save";

form[0]..method="post";

form[0]..submit();

}

</script>

<html:button name="save" onclick="funSave()"/>

<html:button name="delete" onclick="funDelete()"/>

U just mention the method names as save,delete in ur action class

nani_2007a at 2007-7-12 2:15:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

[nobr]hi

Thanks nani and jHyena for ur reply , but its not working, can u tell me where i went wrong, how to set them in jsp and action class.

This is my Action class

public class EditUserDetailsAction extends Action {

/*

* Generated Methods

*/

/**

* Method execute

* @param mapping

* @param form

* @param request

* @param response

* @return ActionForward

*/

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) throws DaoException {

System.out.println("in execute() befor try/catch--");

//**************************the problem is here calling in if()/else, how should i call i?**************

if() {

try{

EditUserDetailsForm editUserDetailsForm = (EditUserDetailsForm) form;

System.out.println(" execute()--");

DaoImplementer daoImpl = new DaoImplementer();

Integer editUid= (Integer) editUserDetailsForm.getUser_Id();

String editName= (String) editUserDetailsForm.getName();

String editTitle= (String) editUserDetailsForm.getTitle();

String editEmail= (String) editUserDetailsForm.getEmail();

Integer editTelephone = (Integer) editUserDetailsForm.getTelephone();

String editUserName = (String) editUserDetailsForm.getUserName();

String editPassword= (String) editUserDetailsForm.getPassword();

daoImpl.registerUser(editUid, editName, editTitle, editEmail, editTelephone, editUserName, editPassword);

System.out.println("sent to model components");

System.out.println("-forwarding to edituserdetail.jsp-");

return mapping.findForward("editUserDetails");

}

catch (Exception e) {

System.out.println("@@@ Exception in EditUserDetailsAction class--@@@@");

throw new DaoException("Error adding userDetails: ",e);

}

}

return mapping.findForward("addUserDetails");

}

}

Here is the jsp

<html:html locale="true">

<head>

<html:base />

<title>editUserDetails.jsp</title>

<script type="text/javascript">

function save_display(){

var x = confirm("Save: Are you sure ?")

if( x == true ){

confirm("Save: OK")

}

else{

text("Save: Cancelled")

}

}

function delete_display(){

var y = confirm("Delete: Are you sure ?")

if( y == true ){

confirm("Delete: OK")

}

else{

alert("Delete: Cancelled")

}

}

function funSave(){

document.form[0].ID.value = id;

form[0].ID.value.action="<%=request.getContextPath()%>/division/editDesignation.app?save=save";

form[0].ID.value.method="post";

form[0].ID.value.submit();

}

function funDelete(){

function funSave(){

document.form[0].ID.value=id;

form[0].ID.value.action="<%=request.getContextPath()%>.app?save=save";

form[0].ID.value.method="post";

form[0].ID.value.submit();

}}

</script>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

<html:javascript formName="editUserDetailsForm"/>

<html:form action="/editUserDetails" onsubmit="return validateEditUserDetailsForm(this);">

user_Id : <html:text property="user_Id"/><html:errors property="user_Id"/><br/>

name : <html:text property="name"/><html:errors property="name"/><br/>

title : <html:text property="title"/><html:errors property="title"/><br/>

telephone : <html:text property="telephone"/><html:errors property="telephone"/><br/>

email : <html:text property="email"/><html:errors property="email"/><br/>

userName : <html:text property="userName"/><html:errors property="userName"/><br/>

password : <html:password property="password"/><html:errors property="password"/><br/>

<html:submit property="save" value="Save" onclick ="funSave()"/>

<html:cancel property="delete" value="delete" onclick="funDelete()"/>

</html:form>

</body>

pls let me know how to set the hidden variables within the above jsp .

and in Action class how to call it.

I would be very thankful to u guys.

Thanks

[/nobr]

goodprograma at 2007-7-12 2:15:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
problem is in fundelete()funsave()dont write the code given by me directly i had given sample code. u just use form.action u need not pass id or something there i used because i am sendind ids and i am savind u just go through that one once
nani_2007a at 2007-7-12 2:15:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Hi guys,

Thanks for ur valuable suggestions, the problem solved.

But the approach was not with the hidden fields in the jsp.

Its just simple. As every field in the form has a property associated and we refer them with setters and getters correspondingly in the formBean class.

So just create the setter and getter property for the button fields too in your formBean class.

private boolean saveButton;

public void setSaveButton(boolean save){

this.saveButton = true; // if the button clicked is savebutton set to true

}

public boolean isSaveButton(){

return saveButton;

}

-

now , in ur reset() set to

saveButton = false;

in action class--

if(formbean.isSaveButton)

Regards

goodprograma at 2007-7-12 2:15:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...