1

This is my JSP file where I am able to insert data in to database properly, but I am unable to get a list of objects I have inserted in the database.

<%@ taglib uri="/struts-tags" prefix="s"%>

<html>

  <table border="1px" cellpadding="8px" >
    <tr>    
        <th>First Name</th>
        <th>Last Name</th>
        <th>Phone Number</th>
        <th>DepartmentID</th>
        <th>Department Name</th>
        <th>Salary</th>
        <th>Delete</th>

    </tr>
    <s:iterator value="employeeList" var="emp">
        <tr>    
            <td><s:property value="firstName" /></td>
            <td><s:property value="lastName" /></td>
            <td><s:property value="phoneNumber" /></td>
            <td><s:property value="departmentId" /></td>
            <td><s:property value="departmentName" /></td>
            <td><s:property value="salary" /></td>
            <td><a href="delete?id=<s:property value="id"/>">delete</a>       
          </td> 
              </tr>
    </s:iterator>
  </table>
  <br />

</body>
</html>

This is my Action Class

enter code here
package com.indus.training.action;

 import java.util.List;

 import com.indus.training.model.Employee;
  import com.indus.training.service.EmployeeService;
 import com.opensymphony.xwork2.ActionSupport;

  public class EmployeeAction extends ActionSupport {

/**
 * 
 */
private static final long serialVersionUID = 316991296006785831L;

private Integer id;
private String firstName;
private String lastName;
private String departmentName;
private Integer departmentId;
private Integer salary;
private String phoneNumber;

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getDepartmentName() {
    return departmentName;
}

public void setDepartmentName(String departmentName) {
    this.departmentName = departmentName;
}

public Integer getDepartmentId() {
    return departmentId;
}

public void setDepartmentId(Integer departmentId) {
    this.departmentId = departmentId;
}

public Integer getSalary() {
    return salary;
}

public void setSalary(Integer salary) {
    this.salary = salary;
}

public String getPhoneNumber() {
    return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}

Employee emp = new Employee();
EmployeeService empSvc = new EmployeeService();

public String execute() {
    this.employeeList = empSvc.getEmployeeList();
    return SUCCESS;
}

public String add() {

    empSvc.saveEmployee(getEmp());
    this.employeeList = empSvc.getEmployeeList();
    return SUCCESS;

}

public Employee getEmp() {
    return emp;
}

public void setEmp(Employee emp) {
    this.emp = emp;
}

public EmployeeService getEmpSvc() {
    return empSvc;
}

public void setEmpSvc(EmployeeService empSvc) {
    this.empSvc = empSvc;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

List<Employee> employeeList;
public String delete (){
    empSvc.deleteEmployee(getId());
    return SUCCESS;

}



public List<Employee> getEmployeeList() {
    return employeeList;
}

public void setEmployeeList(List<Employee> employeeList) {
    this.employeeList = employeeList;
}

     }

this is my struts.xml file

enter code here
 <?xml version="1.0" encoding="UTF-8"?>

 <!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

 <struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />

<!-- Add your package and namespace here -->
<package name="default" namespace="/" extends="struts-default">


    <!--Add your actions here -->
    <action name="welCome" class="com.indus.training.action.EmployeeAction">
        <result name="success">/WelCome.jsp</result>
    </action>
    <action name="deleteAction" class="com.indus.training.action.EmployeeAction"
        method="delete">
        <result name="success">/Home.jsp</result>

    </action>

    <action name="saveDetails" class="com.indus.training.action.EmployeeAction"
        method="add">
        <result name="success">/Home.jsp</result>
        <result name="input">/Home.jsp</result>
    </action>
    <!-- <action name="welCome" class="com.indus.training.action.WelCome"> 
        <result name="success">/WelCome.jsp</result> </action> -->


    <!-- Actions end -->

</package>
    </struts>
4

1 回答 1

0

使用这些应该重定向到操作的映射,结果会从服务层返回对象列表。

<action name="saveDetails" class="com.indus.training.action.EmployeeAction" method="add">
  <result type="redirectAction">welCome</result>
  <result name="input">/Home.jsp</result>
</action>
<action name="deleteAction" class="com.indus.training.action.EmployeeAction" method="delete">
  <result type="redirectAction">welCome</result>
</action>
于 2013-07-16T09:18:01.900 回答