gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:33:23 24 4
gpt4 key购买 nike

这是我的 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>

这是我的 Action 类

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>

最佳答案

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

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

关于java - 在 Struts 2 中看不到对象列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17663825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com