Converter.getAsObject返回null - JSF

时间:2015-10-28 15:27:20

标签: jsf ejb converter

我有一个xhtml页面,我从中选择一个Employee并写入几个输入字段。然后我将该信息传递给方法,以便员工更新。我在网上查看,发现我需要一个转换器,以便jsf可以正确使用我的对象。

奇怪的是它让我EmployeeDAOImpl.findEmployee返回null,即使我测试了方法并且它正确执行。所以我猜错误来自我的转换器类或xhtml页面。任何想法如何解决这个问题?

我所做的就是让所有员工,选择一个,将其存储在emp变量中,并将其与其他数据一起传递给服务。我向受雇者展示了他们的用户名(.findEmployee)。

错误

java.lang.NullPointerException
at com.dao.implementation.EmployeeDAOImpl.findEmployee(EmployeeDAOImpl.java:63)
at com.converters.EmployeeConverter.getAsObject(EmployeeConverter.java:35)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:171)
at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:201)
at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:318)
at org.primefaces.component.selectoneradio.SelectOneRadioRenderer.getConvertedValue(SelectOneRadioRenderer.java:41)
at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1045)
at javax.faces.component.UIInput.validate(UIInput.java:975)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1248)
at javax.faces.component.UIInput.processValidators(UIInput.java:712)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1258)
at javax.faces.component.UIForm.processValidators(UIForm.java:253)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1258)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1195)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)

服务

@Named("updateEmployeeService")
@RequestScoped
public class UpdateEmployeeService
{
@EJB
private EmployeeDAO emDAO;

private String password;
private String fullName;
private String role;

private Employee emp;

private List<Employee> employees;

@PostConstruct
public void init(){
    try {
        employees = emDAO.getAllEmployees();
    } catch (EmployeeException ex) {
        Logger.getLogger(UpdateEmployeeService.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public void updateEmployee() {
    try {
        emDAO.updateEmployee(emp, password, fullName, role);
    } catch (EmployeeException ex) {
        Logger.getLogger(UpdateEmployeeService.class.getName()).log(Level.SEVERE, null, ex);
    }
}

... getters and setter }

转换器

@ManagedBean
@FacesConverter(value = "employeeConverter")
public class EmployeeConverter implements Converter
{

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
    EmployeeDAO emDAO = new EmployeeDAOImpl();
    Object obj = null;
    try {
        obj = emDAO.findEmployee(value);
    } catch (EmployeeException ex) {
        Logger.getLogger(EmployeeConverter.class.getName()).log(Level.SEVERE, null, ex);
    }
    return obj;
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value)
{
    if(value instanceof Employee){
        return ((Employee)value).getUsername();
    }
    else{
        throw new IllegalArgumentException();
    }
}    
}

XHTML

<h:form>
        <h:panelGrid columns="2" >
            <p:outputLabel for="employee" value="Employees : " />
            <p:selectOneRadio id = "employee" value="#{updateEmployeeService.emp}" 
                              layout="grid" columns="2" converter="#{employeeConverter}">
                <f:selectItems value="#{updateEmployeeService.employees}" var="e" 
                               itemLabel="#{e.username}" />
            </p:selectOneRadio>
        </h:panelGrid>


        <p:panelGrid columns="2"  >
            <h:outputText value = "Password :" />
            <p:password value= "#{updateEmployeeService.password}" required="true" />
            <h:outputText value = "Full Name :" />
            <h:inputText value= "#{updateEmployeeService.fullName}" required="true" />
            <h:outputText value="Role:" />
            <h:inputText value ="#{updateEmployeeService.role}" required="true" />

            <h:commandButton action="#{updateEmployeeService.updateEmployee()}" value="Update Employee" />          

        </p:panelGrid>
    </h:form>

0 个答案:

没有答案