Set Bean dynamically according to the registration screen

Asked

Viewed 303 times

2

I’m starting a project with JSF, but I’m still "crawling" on the Framework, and starting with Java now as well. I already work with programming, but not with Java... hehe

In my project, I want to have a standard template for signups, and for each sign-up screen, I will have a different bean.

I wonder if there is any way for me to "say" to the CRUD buttons that, in a given template, the User Bean manager is, in another template is the Students, and so on.

Basically the code would look like this

-- default-crud.xhtml --
<h:body>
    <div id="my-default-crud-div">
        <p:commandButton value="Create" actionListener="#{UserOrStudentBean.create}" />
        <p:commandButton value="Read" actionListener="#{UserOrStudentBean.read}" />
        <p:commandButton value="Update" actionListener="#{UserOrStudentBean.update}" />
        <p:commandButton value="Delete" actionListener="#{UserOrStudentBean.delete}" />
        <ui:insert name="my-default-crud-frame">

        </ui:insert>
    </div>
</h:body>

-- users.xhtml --
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://primefaces.org/ui"
                template="default-crud.xhtml">

    <ui:define name="my-default-crud-frame">
        <p:outputLabel value="#{User.anyStuff}"/>
    </ui:define>
</ui:composition>

-- students.xhtml --
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://primefaces.org/ui"
                template="default-crud.xhtml">

    <ui:define name="my-default-crud-frame">
        <p:outputLabel value="#{Student.anyStuff}"/>
    </ui:define>
</ui:composition>

-- default bean --
@ManagedBean
public class DefaultCRUDBean {
    public boolean beginCreate(){
        return true;
    }

    public void create(){        
        // do some stuffs
        if (beginCreate()){
            endCreate();
        }
    }

    public void endCreate(){

    }

    // other implementations
}

public class User extends DefaultCRUDBean{
    @Override
    public boolean beginCreate() {
        boolean validated = false;
        // check some stuffs
        validated = true;
        // --
        return validated;
    }
}

public class Student extends DefaultCRUDBean{
    @Override
    public boolean beginCreate() {
        boolean validated = false;
        // check some stuffs
        validated = true;
        // --
        return validated;
    }
}

Thank you!

1 answer

0

With Facelets this became easy, note in my case is an example you need, below we have a standard template for Cruds:

<?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:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui">
<ui:composition template="/layout/template.xhtml">
    <ui:define name="content">
        <h:form id="pesquisa" rendered="#{!controller.mudarPagina}">
            <p:messages id="messages" showDetail="true" autoUpdate="false" closable="false" globalOnly="true" />
            <p:panel header="#{tituloPagina}">
                <ui:insert name="camposPesquisa" />
            </p:panel>
            <p:toolbar>
                <f:facet name="left">
                    <h:panelGrid columns="2">
                        <p:commandButton action="#{controller.pesquisar}" value="#{msg['pesquisar']}" ajax="false" update="resultadoPesquisa" icon="ui-icon-search"/>
                        <p:commandButton action="#{controller.novo}" value="#{msg['novo']}" ajax="false" update="cadastro" icon="ui-icon-plusthick"/>
                    </h:panelGrid>
                </f:facet>
            </p:toolbar>
            <p:spacer height="0px" />
            <p:dataTable id="resultadoPesquisa" var="bean" value="#{controller.lista}" paginator="true" rows="10"
                rendered="#{!empty controller.lista}" paginatorPosition="top">
                <p:column styleClass="botoesGrid">
                    <p:commandButton icon="ui-icon-pencil" action="#{controller.editar(bean.id)}" update="cadastro" ajax="false" title="#{msg['editar']}"/>
                    <p:commandButton icon="ui-icon-trash" action="#{controller.excluir(bean)}" process="@this" update="pesquisa" title="#{msg['remover']}">
                        <p:confirm header="#{msg['cabecalho.apagar.registro']}" message="#{msg['apagar.registro']}" icon="ui-icon-alert" />
                    </p:commandButton>
                    <p:confirmDialog global="true" showEffect="exploud" hideEffect="fade">
                        <p:commandButton value="#{msg['sim']}" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
                        <p:commandButton value="#{msg['nao']}" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
                    </p:confirmDialog>
                </p:column>
                <ui:insert name="resultado" />
                <f:facet name="footer">
                    <h:outputText value="#{msg['total.registros']}: #{controller.lista.size()}" />
                </f:facet>
            </p:dataTable>
        </h:form>
        <h:form id="cadastroEdicao" rendered="#{controller.mudarPagina}">
            <p:panel header="#{empty idEntidade ? msg['cadastrando'] : msg['atualizando']} #{tituloPagina}">
                <ui:insert name="cadastroEdicao" />
            </p:panel>
            <p:toolbar>
                <f:facet name="left">
                    <h:panelGrid columns="4">
                        <p:commandButton action="#{controller.cancelar}" value="#{msg['voltar']}"
                            update="cadastro,pesquisa,:pesquisa:resultadoPesquisa" immediate="true" ajax="false" icon="ui-icon-arrowreturnthick-1-w"/>
                        <p:commandButton action="#{controller.gravar}" value="#{msg['salvar']}"
                            update="cadastro,pequisa,:pesquisa:resultadoPesquisa" ajax="false" icon="ui-icon-disk"/>
                        <p:commandButton actionListener="#{controller.novo}" value="#{msg['novo']}" ajax="false" rendered="#{idEntidade != null}" />
                        <p:commandButton action="#{controller.salvarENovo}" value="#{msg['salvar.novo']}" ajax="false" update="cadastro" icon="ui-icon-plusthick"/>
                    </h:panelGrid>
                </f:facet>
            </p:toolbar>
        </h:form>
    </ui:define>
</ui:composition>
</html>

Note that I have some variables in the middle, ex: controller, tituloPagina, idEntidade, etc..

Now I will use this Templete on a page:

<?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:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui">
<ui:composition template="/layout/template-crud.xhtml">
    <ui:param name="controller" value="#{natureController}" />
    <ui:param name="idEntidade" value="#{natureController.nature.id}" />
    <ui:param name="tituloPagina" value="#{msg['nature']}" />

    <!-- TELA DE PESQUISA -->
    <ui:define name="camposPesquisa">
            <h:panelGrid id="camposPesquisa" columns="2">
                <h:outputLabel value="#{msg['nome']}: " />
                <p:inputText />
            </h:panelGrid>
    </ui:define>

    <!-- RESULTADO DA PESQUISA -->
    <ui:define name="resultado">
            <p:column headerText="#{msg['nome']}" sortBy="#{bean.nome}" style="width:12%;">
                <h:outputText value="#{bean.nome}" />
            </p:column>
            <p:column headerText="#{msg['aumenta']}" sortBy="#{bean.aumenta}">
                <h:outputText id="diminui" value="#{bean.aumenta}" style="color: #{bean.aumenta == 'Neutro' ? 'none' : 'red'}" />
            </p:column>
            <p:column headerText="#{msg['diminui']}" sortBy="#{bean.diminui}">
                <h:outputText value="#{bean.diminui}" style="color: #{bean.diminui == 'Neutro' ? 'none' : 'blue'}" />
            </p:column>
            <p:column headerText="#{msg['sabor.favorito']}" sortBy="#{bean.saborFavorito}">
                <h:outputText value="#{bean.saborFavorito}" />
            </p:column>
            <p:column headerText="#{msg['sabor.nao.favorito']}" sortBy="#{bean.saborQueNaoGosta}">
                <h:outputText value="#{bean.saborQueNaoGosta}" />
            </p:column>
    </ui:define>

    <!-- TELA DE CADASTRO E EDIÇÃO -->
    <ui:define name="cadastroEdicao">
        <h:panelGrid id="informacoesCadastro" columns="3">
            <h:outputLabel value="#{msg['nome']}" />
            <p:inputText id="nome" value="#{natureController.nature.nome}" size="20" />
            <p:message for="nome" />
            <h:outputLabel value="#{msg['aumenta']}: " rendered="true" />
            <p:selectOneMenu id="aumenta" value="#{natureController.nature.aumenta}" effect="clip">
                <f:selectItem itemLabel="" />
                <f:selectItem itemLabel="#{msg['neutro']}" itemValue="Neutro" />
                <f:selectItems value="#{enumHelper.listarStatsBase()}" var="bean" itemLabel="#{bean.nome}" itemValue="#{bean.nome}" />
            </p:selectOneMenu>
            <p:message for="aumenta" />
            <h:outputLabel value="#{msg['diminui']}: " />
            <p:selectOneMenu id="diminui" value="#{natureController.nature.diminui}" effect="clip">
                <f:selectItem itemLabel="" />
                <f:selectItem itemLabel="#{msg['neutro']}" itemValue="Neutro" />
                <f:selectItems value="#{enumHelper.listarStatsBase()}" var="bean" itemLabel="#{bean.nome}" itemValue="#{bean.nome}" />
            </p:selectOneMenu>
            <p:message for="diminui" />
        </h:panelGrid>
    </ui:define>
</ui:composition>
</html>

Note that I pass the parameters at the top of the page:

<ui:param name="controller" value="#{natureController}" />
<ui:param name="idEntidade" value="#{natureController.nature.id}" />
<ui:param name="tituloPagina" value="#{msg['nature']}" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.