Thrax
Thrax

Reputation: 1964

JSF with Prettyfaces navigation

I'm using JSF 2.2 with PrettyFaces 3.3.3 in my Entreprise Application.

I'm wondering if the way I handle navigation is correct (AdminCompaniesController.java) :

@ManagedBean(name = "companiesBean")
@ViewScoped
@URLMappings(mappings={
    @URLMapping(id = "admin-companies-view", pattern = "/admin/company/view/#{id}", viewId = "/admin/companyView.jsf"),
    @URLMapping(id = "admin-companies-edit", pattern = "/admin/company/edit/#{id}", viewId = "/admin/companyEdit.jsf"),
    @URLMapping(id = "admin-companies-add", pattern = "/admin/company/add", viewId = "/admin/companyAdd.jsf"),
    @URLMapping(id = "admin-companies-list", pattern = "/admin/companies", viewId = "/admin/companies.jsf")
})
public class AdminCompaniesController implements Serializable
{
    @EJB
    private CompanyService companyService;
    private Collection<Company> companies = new ArrayList<>();
    private Company company;

    @PostConstruct
    public void init()
    {   

        String viewId = NavigationUtils.getViewId();

        switch (viewId) {
            case ("admin-companies-list"):
                companies = companyService.getAllCompanies();
                break;
            case ("admin-companies-add"):
                company = new Company();
                break;
            case ("admin-companies-view"):
            case ("admin-companies-edit"):
                Long id = NavigationUtils.getParameter("id", Long.class);
                company = companyService.getCompanyById(id);
                break;
    }
}

I'm using PrettyFaces parameters from annotations :

NavigationUtils.getViewId() is equivalent to PrettyContext.getCurrentInstance().getCurrentMapping().getId()

and from url :

NavigationUtils.getParameter("id", Long.class) is equivalent to FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id")

Is that the correct way to manage the CRUD operations on my Entity "Company"? I've read OCPsoft documentation (http://ocpsoft.org/docs/prettyfaces/3.3.3/en-US/html/Configuration.html) about @URLQueryParameters and @URLAction but i'm not sure how to implement them instead of my switch/case solution.

Upvotes: 0

Views: 315

Answers (1)

Lincoln
Lincoln

Reputation: 3191

As @chkal says:

You should really think about using one single bean for each mapping which basically then would mean, that you would end up with one class per page. IMHO this totally makes sense. Your current approach looks like you are creating a god class.

The above suggestion is good practice, and prevents the need for a case statement, since each @URLMapping-annotated bean will have its own explicit @URLAction or @PostConstruct method.

Additionally, you can use direct injection to pass the values of parameters into those beans without using the Request parameter map:

@URLMapping(id = "admin-companies-view", pattern = "/admin/company/view/#{ id : pageBean.id }", viewId = "/admin/companyView.jsf")

Upvotes: 1

Related Questions