检索SelectOneMenu列表中所选项的值

时间:2012-08-26 13:35:25

标签: jsf jsf-2 converter selectonemenu

我只想在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());
                                               }

UserConverter类:

 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)

2 个答案:

答案 0 :(得分:1)

替换public void createProject(ActionEvent actionEvent){
public void createProject(){
    System.out.println(selectedUser});
}

并将process="modalDialog"添加到按钮

答案 1 :(得分:1)

如果您在上面的对话中隐含了所选的值,那么下一个可用的罪魁祸首就是您的托管bean。具体来说,您的托管bean的范围。如果你使用的是@RequestScoped bean,你肯定会遇到这样的问题。将您的bean更改为@ViewScoped,看看会发生什么