I need to display the table containing a list of users (id
, name
, etc.) and place the buttons to delete specified user in the last column. Something like this:
############################
| ID | name | ... | button |
| ID | name | ... | button |
############################
I've wrote code like this:
<form action="/Struts/DeleteUser.do" name="myForm" id="myForm" method="post">
<display:table name="sessionScope.AllUsersForm.usersList">
<display:column property="id" title="ID" />
<display:column property="name" title="Name" />
...........
<display:column title="Delete">
<input type="submit" value="Delete user" />
</display:column>
</display:table>
<form>
So how can I identify pressed button in my Action class? I've already tried to place a hidden field into section with button and change it's value but nothing happened.
UPDATED:
I've already solved problem. I've used this:
<display:table name="sessionScope.AllUsersForm.usersList"
<%-- This ==> --%> id="item" <%-- <=== --%> >
........
<input type="submit" value="Delete user"
onclick="document.getElementById('pressedButton').value = ${item.id}"/>
And created hidden field:
<input type="hidden" name="pressedButton" id="pressedButton" />
You could easy do it by giving a name to the button, for example
<input name="action" type="submit" value="Delete user" />
now in the bean that in, your case form bean associated to the action, you should create a property
private String action;
public void setAction(String action){
this.action = action;
}
public String getAction(){
return action;
}
Struts 1 used commons beanutils to populate form bean. When you submit the form the field values are populated via setters. The button field is not exception from the rules. So, its value will also be set to the action
property. Then you could check it via
if (action != null && action.equals("Delete user")){
System.out.println("Button is: "+action);
}
The most natural Struts 1 way to identify and execute button events is to dispatch the message key value along with button. For example
<html:submit><bean:message key="button.delete"/></html:submit>
The "button.delete"
is a message resource key, should be in MessageResource.properties
file
button.delete = Delete user
The action should implement LookupDispatchAction
and override
@Override
public Map getKeyMethodMap() {
Map<String, String> map = new HashMap<String, String>();
map.put("button.delete", "delete");
return map;
}
then when action is executed it will dispatch to the method specified in the map
. In this example method delete
.