Adding session bean to to requested bean using annotation JSF

Adding session bean to to requested bean using annotation JSF

One major part on JSF would separation of concerns even for beans. As a rule of thumb beans related to model are session beans and those which have actions to be taken care of are requested one.

So, In this particular scenario we would have two beans. Basically we don’t want to include any logic inside the session bean, rather we would add session bean as a member variable to request bean.

Lets take a simple registration process.

The requested bean which will be responsible for actions would look like

package com.enderase.beans;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import com.enderase.model.Contractor;

@ManagedBean
@RequestScoped
public class ActionListeners implements Serializable{
	
	@ManagedProperty(value="#{contractorBean.contractor}")
	private Contractor contractor;
	
	public void setContractor(Contractor contractor){
		this.contractor = contractor;
	}
	
	public Contractor getContractor() {
		return this.contractor;
	}
	
	private static final long serialVersionUID = 1L;
	
	
	/**
	 * Action handler for Contractor save.
	 * @return String, next
	 */
	public String registerContractor() {
		Contractor contractor = this.contractor;
		if (contractor != null) {
			try {
				FileWriter fileWriter = new FileWriter("/tmp/name.note");
				BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
				bufferedWriter.write("Name "+contractor.getName()+" Email "+contractor.getEmail()
						+contractor.getState());
				bufferedWriter.close();
			} catch (Exception ex) {
				//log the exception here
			}
		}
		return "navigated";
	}
}

So the key thing here would be the @ManagedProperty part.
That would inject the session bean into the request bean without creating any instance of it.

*Don’t for get to add getter and setter for the session bean you are adding otherwise you would get an error.

The session bean would be a simple holder of model

package com.enderase.beans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.enderase.model.Contractor;

@ManagedBean
@SessionScoped
public class ContractorBean {
	
	private Contractor contractor;
	
	public ContractorBean() {
		this.contractor = new Contractor();
	}
	public Contractor getContractor(){
		return this.contractor;
	}
	
	public void setContractor(Contractor contractor){
		this.contractor = contractor;
	}
}

Where the contractor would be a simple POJO file

Passing composite object parameter to jersey in Restful Java web

J2EE Maven Eclipse Hello World Tutorial Part Two

how to get user input and process it in nodejs tutorial

how to display html files from node js example tutorial

using express package in node – installation and usage example tutorial

a hello world tutorial using node js with http web server

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*