我正在使用 SEAM 和 JBOSS 制作一个基于 Web 的应用程序。我正在尝试为下拉框制作转换器,但是每当我在转换器类中使用 entityManager 时,我都会收到 NullPointerException。我花了几天时间试图找到这个帮助,任何帮助都将不胜感激
这是我的一些代码。如果还需要,请告诉我:
转换器类:
package edu.uwrf.iss.flowershop.entity;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.ServletContext;
import javax.swing.JOptionPane;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Scope;
public class EmpConverter implements Converter {
@In
private EntityManager entityManager;
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
int num = Integer.parseInt(arg2);
entityManager.refresh(getClass());
Query query = entityManager.createQuery("SELECT e FROM FlowerStoreDelivery WHERE e.deliveryId LIKE :num")
.setParameter("num", num);
JOptionPane.showMessageDialog(null, query.getResultList());
FlowerStoreEmployee emp = entityManager.find(FlowerStoreEmployee.class, arg2);
return emp;
}
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) throws ConverterException {
return Integer.toString(((FlowerStoreEmployee)arg2).getEmployeeId());
}
}
这是使用转换器的地方:
<my:dropdown label="Employee ID" id="emp" value="#{deliveryPort.emp}"required="false">
<f:converter converterId="EmpConverter" />
<s:selectItems value="#{deliveryPort.empList}" var="emp" label="#{emp.employeeId} #{ emp.nameFirst}"/>
</my:dropdown>