aspose file tools*
The moose likes Struts and the fly likes In struts2 delete is not Working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "In struts2 delete is not Working" Watch "In struts2 delete is not Working" New topic
Author

In struts2 delete is not Working

Devi Ramadass
Greenhorn

Joined: Sep 20, 2008
Posts: 17
Hai,

I am new to Struts2, i trying on the CRUD(Struts2/mySql). First,I want to delete the selected record, but i don't know how get the record id.
My Jsp code is :
----------------

<s:iterator value="employees" id="employee">
<tr class="nowrap">
<td class="nowrap"> <s roperty value="firstName"/></td>
<td class="nowrap"> <s roperty value="lastName"/></td>
<td class="nowrap"> <s roperty value="designation"/> </td>
<td class="nowrap"> <s roperty value="id"/>
<s:url id="delete" action="delete">
<s aram name="employee.EMPID" value="id"/>
</s:url>
<s:a href="%{delete}">Delete</s:a></td>

</s:iterator>

In Jave Code is :
-----------------

public class EmployeeList extends ActionSupport //implements SessionAware
{
private static final long SerialVersionUID = 1L;
private int id;
private Employee employee;
Map session;
public List<Employee> employees;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
public String execute() throws Exception
{
try
{
//employee = new Employee();
employees= new ArrayList<Employee>();
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql:///hibernatetutorial","root", "");
String query = new String("select * from employee");
stmt = con.createStatement();
rs = stmt.executeQuery(query);
while (rs.next())
{
employee=new Employee(rs.getInt("EMPID"),rs.getString("FirstName"),rs.getString("LastName"),rs.getString("Designation"));
employees.add(employee);
}
}
catch(Exception e)
{
System.err.println("Exception: " + e.getMessage());
}
return SUCCESS;
}

public String deleteEmployee()
{
//how i get the selecting record id to delete.
return SUCCESS;
}

public List<Employee> getemployees()
{
return employees;
}
public void setemployees(List<Employee> employees)
{
this.employees = employees;
}

public Employee getEmployee()
{
return employee;
}
public void setEmployee(Employee employee)
{
this.employee = employee;
}
}

Struts.xml
----------
<struts>
<package name="pages" namespace="/pages" extends="struts-default">
<action name="iteratorTag" class="pages.EmployeeList">
<result>/pages/iteratorTag.jsp</result>
</action>
<action name="delete" method="deleteEmployee" class="pages.EmployeeList">
<result name="success">/pages/Delete.jsp</result>
</action>
</package>
</struts>

Any one knows please help me.
Thanking You
Devi Ramadass
[ September 22, 2008: Message edited by: Devi Ramadass ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60056
    
  65

"devi rv", please check your private messages for an important administrative matter.

Also, please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
[ September 21, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Devi Ramadass
Greenhorn

Joined: Sep 20, 2008
Posts: 17
Hay,

Any One knows please answer it will be helpful for me.

Regards,
Devi Ramadass
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
At what point are you losing your row ID? Is it not showing up on your screen? Is it not populating your action class?
Devi Ramadass
Greenhorn

Joined: Sep 20, 2008
Posts: 17
Hi,

Thanks for your reply, i am getting the particular empid(record id) inside execute method through this statement "employee.getId();".But i could not get the same id inside deleteEmployee() method.How can i get the id in deleteEmployee() method.Because i want to delete particular record.

Regards,
Devi Ramadass
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
I haven't used s:url before but it looks like you're set up to populate a property on your employee object named EMPID. Does your employee object contain a property with this name (or setters and getters) for it. A look at the code for this class might help.
Devi Ramadass
Greenhorn

Joined: Sep 20, 2008
Posts: 17
Hi,

Thank you so much for your help, i have the accessor/mutator methods for EMPID. Please look at the below code and suggest.

Employee.java
public class Employee
{
private String firstName;
private String lastName;
private String designation;
private int id;
public Employee()
{
}
public Employee(int id,String firstName,String lastName,String designation)
{
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.designation = designation;
}
//setter and getter methods for firstName,lastName,designation
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
}

Regards,
Devi Ramadass
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
In your employee class I don't see a field named EMPID, nor do I see a getter or setter for it. So I think the s:param you are using in your s:url tag is the problem. It think it should look more like this:



see if that will populate your employee object corretly.
Devi Ramadass
Greenhorn

Joined: Sep 20, 2008
Posts: 17
Hi,

I am corrected the param tag, now it's working.Thank You Tom.

Regards,
R.Devi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: In struts2 delete is not Working
 
Similar Threads
Deleting Record in a Web Application.
JSF Datatable equivalent in Struts2
In the list,delete a paricular record.
Interceptor messes up action class in Struts2
struts2:Displaying dtatbase values in jsp