[英]java jsp get list per selected item
我的目标是获取每个选定公司的所有员工列表。 正如您在问题末尾的jsp文件中看到的那样,我得到了错误:
{IndirectList: not instantiated}
您能帮我获取每个选定公司的所有员工列表吗?
公司.java
package analysis.data;
import java.io.Serializable;
import java.lang.Integer;
import java.lang.String;
import java.util.List;
import javax.persistence.*;
/**
* Entity implementation class for Entity: Company
*
*/
@Entity
public class Company implements Serializable {
@Id
@GeneratedValue
private Integer id;
@Column(unique=true, nullable=false)
private String lastname;
@OneToMany(mappedBy="company", fetch=FetchType.LAZY) // LAZY = fetch when needed, EAGER = fetch immediately
private List<Employee> employees;
private static final long serialVersionUID = 1L;
public Company() {
super();
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastname() {
return this.lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname.toUpperCase();
}
public List<Employee> getEmployees() {
return this.employees;
}
}
Employee.java
package analysis.data;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
/**
* Entity implementation class for Entity: Company
*
*/
@Entity
public class Employee implements Serializable {
@Id
@GeneratedValue
private Integer id;
@Column(nullable=false)
private String firstname;
@Column(nullable=false)
private String lastname;
@ManyToOne
private Company company;
private static final long serialVersionUID = 1L;
public Employee() {
super();
//nbAbsences = 0;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstname() {
return this.firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return this.lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Company getCompany() {
return this.company;
}
public void setCompany(Company company) {
this.company = company;
if (!company.getEmployees().contains(this)) {
company.getEmployees().add(this);
}
}
@Override
public String toString() {
return "[" + this.getId() + "] " + this.getFirstname() + " " + this.getLastname();
}
}
在我的controller.java中
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
....
} else if (methode.equals("get") && action.equals("/detailsCompany")) {
doDetailsCompany(request, response);
....
private void doDetailsCompany(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id = Integer.valueOf(request.getParameter("id"));
Company company = CompanyDAO.retrieveById(id);
request.setAttribute("company", company);
loadJSP(urlDetailsCompany, request, response);
}
....
在我的CompanyDAO.java中
public class CompanyDAO {
...
public static Company retrieveById(int id) {
EntityManager em = GestionFactory.factory.createEntityManager();
Company company = em.find(Company.class, id);
em.close();
return company;
}
...
public static List<Company> getAll() {
EntityManager em = GestionFactory.factory.createEntityManager();
Query q = em.createQuery("SELECT c FROM Company c");
@SuppressWarnings("unchecked")
List<Company> listCompany = q.getResultList();
return listCompany;
}
}
在这里,我选择要重定向到detailsCompany的公司名称(id),以查看该公司的所有员工:
<jsp:useBean id="companys" type="java.util.List<analysis.data.Company>" scope="request"/>
<table>
<tr>
<th>Company Name</th>
<th>Number of employees</th>
<th>Employees</th>
</tr>
<% for (Company company : companys) {%>
<tr>
<td><%=company.getNom()%></td>
<td><%=company.getEmployees().size()%></td>
<td><a href="<%= getServletContext().getContextPath()%>/do/detailsCompany?id=<%=company.getId()%>">See the list</a></td>
</tr>
<% } %>
</table>
最后在我的detailsCompany.jsp
JSP文件中,我对如何选择所选公司的员工列表感到detailsCompany.jsp
。
<jsp:useBean id="company" type="java.util.List<analysis.data.Company>" scope="request"/>
<p>Company : <%=company.getEmployees()%></p>
我得到这个{IndirectList:未实例化}而不是雇员列表。 我不确定我的代码是否正确。
问题出在
`<%=company.getEmployees()%>`
getEmployees()返回一个List对象,因此JSP调用其toString函数将导致消息“ IndirectList:未实例化”
如果您希望打印员工详细信息(即等效于其toString方法),则可能需要执行以下操作:
`<p>Company : <% List<Employee> employees = company.getEmployees();
for(i=0;i<employees.size();i++)%>
<%= employees.get(i);%>
</p>
`
您可能还需要添加一些换行符以分隔各个员工记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.