0

我只想在 SelectOneMenu 列表中检索所选用户的登录名,这是代码:

我的 xhtml 页面:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:f="http://java.sun.com/jsf/core"      
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:p="http://primefaces.org/ui">

<h:head>
    <title>JSF 2.0 Test</title>
</h:head>
<h:body>
    <h3>JSF 2.0 Test</h3>
    <h:form>

    <br></br>
 <p:commandButton id="modalDialogButton" value="Modal" onclick="dlg2.show();"  
     type="button"/>  
<p:dialog id="modalDialog" header="Modal Dialog" widgetVar="dlg2" modal="true" 
     height="100">  
     <h:outputText value="This is a Modal Dialog." />  

      <p:selectOneMenu value="#{adminMB.selectedUser}" converter="userConverter" 
       effect="fade">  
            <f:selectItem itemLabel="Choose Administor" />  
            <f:selectItems value="#{adminMB.users}" var="user" itemValue="#{user}" 
            itemLabel="#{user.userLogin}" />  
       </p:selectOneMenu>  

      <p:commandButton id="Add"  value=" Add "  actionListener="#  
      {adminMB.createProject}"></p:commandButton>

        </p:dialog>  

    </h:form>
</h:body>
 </html>

adminMB.createProject 方法:

 public void createProject(ActionEvent actionEvent){

  System.err.println("created project admin login :"+this.selectedUser.getUserLogin());
                                               }

用户转换器类:

 package tn.talan.testFramework.converter;

 import java.util.ArrayList;
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
 import javax.faces.convert.Converter;
 import javax.faces.convert.FacesConverter;

 import org.objectweb.salome_tmf.data.User;
 import org.objectweb.salome_tmf.ihm.admin.AdministrationFinal;

 import tn.talan.testFramework.managedBean.AdminMB;

 @FacesConverter (value="userConverter")//(forClass= AdminMB.class)

 public class UserConverter implements Converter {

@Override public Object getAsObject(FacesContext context, UIComponent component, 
    String value) {

    User selectedUser = null;
    ArrayList<User>  usersList;
    String userLogin;
    AdministrationFinal  administration=new AdministrationFinal();
    administration.onInit();
    usersList = administration.getAllUsers();
    if (usersList!=null){
    for(int i=0 ;i<usersList.size();i++)
    {   selectedUser=usersList.get(i);
        userLogin=selectedUser.getLoginFromModel();

    System.err.println(userLogin);
    if  (userLogin==value)
        return userLogin;
    }
    }
    return null;
}

 @Override
 public String getAsString(FacesContext context, UIComponent component, Object  value)    
     {
     if (value == null) return ""; 
     return ((User) value).getUserLogin();

 }

   } 

因此,当我单击“添加”按钮时,将调用 AdminMB Bean 的 createProject 方法,并且出现此错误:

错误输出:

26 août 2012 13:58:52 javax.faces.event.MethodExpressionActionListener processAction
GRAVE: Réception de 'java.lang.NullPointerException' lors de l'invocation du listener d'action '#{adminMB.createProject}' du composant 'Add'
26 août 2012 13:58:52 javax.faces.event.MethodExpressionActionListener processAction
GRAVE: java.lang.NullPointerException
    at tn.talan.testFramework.managedBean.AdminMB.createProject(AdminMB.java:205)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:264)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:144)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:84)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:773)
    at javax.faces.component.UICommand.broadcast(UICommand.java:296)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1255)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:334)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

26 août 2012 13:58:52 com.sun.faces.context.AjaxExceptionHandlerImpl log
GRAVE: JSF1073 : javax.faces.event.AbortProcessingException intercepté durant le traitement de INVOKE_APPLICATION 5 : UIComponent-ClientId=j_idt8:Add, Message=/primeFacesDialog.xhtml @31,97 actionListener="#{adminMB.createProject}": java.lang.NullPointerException
26 août 2012 13:58:52 com.sun.faces.context.AjaxExceptionHandlerImpl log
GRAVE: /primeFacesDialog.xhtml @31,97 actionListener="#{adminMB.createProject}": java.lang.NullPointerException
javax.faces.event.AbortProcessingException: /primeFacesDialog.xhtml @31,97 actionListener="#{adminMB.createProject}": java.lang.NullPointerException
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:178)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:84)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:773)
    at javax.faces.component.UICommand.broadcast(UICommand.java:296)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1255)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:334)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
4

2 回答 2

1

替换public void createProject(ActionEvent actionEvent){

public void createProject(){
    System.out.println(selectedUser});
}

并添加process="modalDialog"到您的按钮

于 2012-08-26T13:47:17.350 回答
1

如果正如您在上面的对话中所暗示的那样,所选值已成功转换,则下一个可用的罪魁祸首是您的托管 bean。具体来说,托管 bean 的范围。如果您使用的是@RequestScopedbean,那么您肯定会遇到这样的问题。把你的 bean 改成 a@ViewScoped看看会发生什么

于 2012-09-04T03:53:17.203 回答