cancel
Showing results for 
Search instead for 
Did you mean: 

Adding a new managed bean

ddong
Champ in-the-making
Champ in-the-making
Hi, Guys

I am adding a new managed bean that has the properties like PersonService, NodeService, etc. And I put this bean in the org.alfresco.web.bean and declare it in the faces-config-beans file

<managed-bean>
      <description>
         The bean that backs up the entry screen
      </description>
      <managed-bean-name>EntryBean</managed-bean-name>
      <managed-bean-class>org.alfresco.web.bean.EntryBean</managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
      <managed-property>
         <property-name>nodeService</property-name>
         <value>#{NodeService}</value>
      </managed-property>
      <managed-property>
         <property-name>authenticationService</property-name>
         <value>#{AuthenticationService}</value>
      </managed-property>
      <managed-property>
         <property-name>personService</property-name>
         <value>#{PersonService}</value>
      </managed-property>
   </managed-bean>

But I found it is always "null" when I access it, is this because I don't configure some spring configuration file to declare it?? or is there any work I need to do in order to initialize it??
2 REPLIES 2

ddong
Champ in-the-making
Champ in-the-making
Here is my simple EntryBean code,

package org.alfresco.web.bean;

import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.cmr.repository.NodeService;

public class EntryBean
{
    /**user name */
   private String userName = null;
   
   /**family name */
   private String familyName = null;
   
   /**first name*/
   private String firstName = null;
   
   /**email*/
   private String email = null;
   
   /**PersonService*/
   protected PersonService personService;
   
   /**Nodeservice*/
   protected NodeService nodeService;
   
   /**AuthenticationService*/
   protected AuthenticationService authenticationService;

   // ——————————————————————————
   // Managed bean properties
   /**
    * @param familyName the userName to set
    */
   public void setUserName(String userName) {
      this.userName = userName;
   }

   /**
    * @return the userName
    */
   public String getUserName() {
      return userName;
   }

   /**
    * @param familyName the familyName to set
    */
   public void setFamilyName(String familyName) {
      this.familyName = familyName;
   }

   /**
    * @return the familyName
    */
   public String getFamilyName() {
      return familyName;
   }

   /**
    * @param firstName the firstName to set
    */
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }

   /**
    * @return the firstName
    */
   public String getFirstName() {
      return firstName;
   }

   /**
    * @param email the email to set
    */
   public void setEmail(String email) {
      this.email = email;
   }

   /**
    * @return the email
    */
   public String getEmail() {
      return email;
   }

   /**
    * @param personService the personService to set
    */
   public void setPersonService(PersonService personService) {
      this.personService = personService;
   }

   /**
    * @return the personService
    */
   public PersonService getPersonService() {
      return personService;
   }

   /**
    * @param nodeService the nodeService to set
    */
   public void setNodeService(NodeService nodeService) {
      this.nodeService = nodeService;
   }

   /**
    * @param authenticationService the authenticationService to set
    */
   public void setAuthenticationService(AuthenticationService authenticationService) {
      this.authenticationService = authenticationService;
   }

   // ——————————————————————————
   // Action Method
   /**
    * determine the UniKey User is the 1st time visit or not
    */
   public boolean firstTimeUser(String userName){
      return this.personService.personExists(userName);
   }
}

I test it in scriptlet using "EntryBean eb = (EntryBean)request.getSession().getAttribute("EntryBean");" Every time I access the eb, a Null pointer error will pop up. Although, this bean hasn't been finished yet, but I don't think this is the reason.

kevinr
Star Contributor
Star Contributor
You are not accessing the bean correctly - it will only appear in the session (or what scope is is defined against) once it has been accessed at least once - and you shouldn't really go directly to the session to get it. Instead you config it into the other bean you want to use it in, i.e. in exactly the same way you are configuring in the PersonService bean or NodeService bean into yours. Take a look at our beans (such as NavigationBean) and you'll see that's how we get access to any other beans. It might be worth reading a couple of JSF tutorials on the web.

Thanks,

Kevin