Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

In struts2 delete is not Working

 
Devi Ramadass
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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 ]
 
Devi Ramadass
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hay,

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

Regards,
Devi Ramadass
 
Tom Rispoli
Ranch Hand
Posts: 349
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 349
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 349
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Regards,
R.Devi
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic