Retrieving the value of the selected item in a Sel

2019-09-07 22:04发布

问题:

i just want to retrieve the login of the selected User in the SelectOneMenu List here's the code :

My xhtml page :

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

The adminMB.createProject method :

 public void createProject(ActionEvent actionEvent){

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

The UserConverter Class :

 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();

 }

   } 

So when i click on the "Add" button , the createProject method of the AdminMB Bean is invoked and i'm getting this error :

Error output:

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)

回答1:

If the selected value is being converted successfully as you've implied in your conversation above, the next available culprit is your managed bean. Specifically, the scope of your managed bean. If you're using a @RequestScoped bean, you're certain to run into problems like this. Change your bean to a @ViewScoped and see what happens



回答2:

replace public void createProject(ActionEvent actionEvent){ with

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

and add process="modalDialog" to your button