在Struts 2中看不到对象列表?

时间:2013-07-15 20:58:52

标签: java hibernate jsp struts2

这是我的JSP文件,我可以正确地将数据插入数据库,但是我无法获得已插入数据库的对象列表。

<%@ 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>

这是我的动作类

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;
}

     }

这是我的struts.xml文件

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>

1 个答案:

答案 0 :(得分: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>