Creating a Managed Bean

Hi,

I am using JSF, ADF, JDeveloper and Oracle 10g Database.

I have a jsp called login.jsp - this has a form that where the agent enters their username.

<h:form>

<af:inputText id="agentName" label="Agent Name" value="#{loginBean.username}"/>

<af:commandButton action="#{loginBean.login}" text="Login"/>

</h:form>

When the user presses the command button, the loginbean.login( ) method is called.

public String login(){

AgentBean agent =new AgentBean();

AgentService agentService =new AgentService();

System.out.println("UN"+username);

agent = agentService.loginAgent(username);

if(agent ==null){

return"fail";

}

else{

return"success";

}

}

The agent = agentService.loginAgent(username);

gets an instance of the AgentBean which is populated with data retrieved from the database in AgentBeanDAO class.

My question is... Is the way I create the AgentBean correct? When I try to reference it using

FacesContext facesContext = FacesContext.getCurrentInstance();

ValueBinding valueBinding = facesContext.getApplication().createValueBinding("#{agentBean}");

AgentBean agent = (AgentBean) valueBinding.getValue(facesContext);

it is null. I am referencing it in EmployeeBean to get the employee details based on their ID.

My faces-config.xml is...

<managed-bean>

<managed-bean-name>employeeBean</managed-bean-name>

<managed-bean-class>uk.co.rias.bean.EmployeeBean</managed-bean-class>

<managed-bean-scope>request</managed-bean-scope>

<managed-property>

<property-name>agentBean</property-name>

<property-class>uk.co.rias.bean.AgentBean</property-class>

<value>#{agentBean}</value>

</managed-property>

</managed-bean>

<managed-bean>

<managed-bean-name>agentBean</managed-bean-name>

<managed-bean-class>uk.co.rias.bean.AgentBean</managed-bean-class>

<managed-bean-scope>session</managed-bean-scope>

</managed-bean>

<managed-bean>

<managed-bean-name>loginBean</managed-bean-name>

<managed-bean-class>uk.co.rias.bean.LoginBean</managed-bean-class>

<managed-bean-scope>request</managed-bean-scope>

</managed-bean>

In conclusion, I suppose my questions are:

1. Do I create the bean in the right place

2. Do I need to create the bean withnew MyBean()

3. Am I referencing the bean correctly in EmployeeBean

Any help would be much appreciated. I have scoured the forum looking for an answer but to no avail.

I think I have included everything necessary for an answer, but if you require any more info, please ask and I'll provide it.

Many Thanks,

John

[3730 byte] By [jpcm24a] at [2007-11-27 2:30:19]
# 1

Which bean exactly are you talking about? The AgentBean?

Strictly looking it's just a DTO (Data Transfer Object) which also follows the base javabean spec (having encapsulated private properties which are accessible by getters and setters only). The DTO's don't need to be definied in the faces-config.xml. You can just access them by the accessors of the backing bean which is declared as managed bean in the faces-config.xml.

BalusCa at 2007-7-12 2:43:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hi

You are creating that bean is not correct

Because that bean is already created once your application start.

So you can use you bean like .

HttpSession session = FacesContext.getCurrentInstance().getExternalContext().getSession();

YourBean yb = (YourBean)session.getParameter("YourBeanName");

and once work done reset into session

With Best

Gunjan-Bohraa at 2007-7-12 2:43:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Hi Balus

Cheers - I have implemented a DTO of AgentBean (changed it to AgentDTO for naming conventions) which is created within the LoginBean. This now allows access through the LoginBean (getters and setters) and the LoginBean is defined in the faces-config.xml

I hope that this was what you meant!

I've given you my Duke Stars - spend them wisely!

Thanks

John

jpcm24a at 2007-7-12 2:43:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...