Olá Galera ,
Sou novo aqui no fórum e estou passando por um problema, quando incluo dinamicamente uma pagina,
usando ui:include as actions associadas aos botões não estão sendo invocadas no Managed Bean,alguem tem alguma idéia
do que eu possa estar fazendo de errado??
Abraços
Segue abaixo o meu código:
pagina principal (hello.xhtml)
<!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">
<f:view>
<h:head>
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
</h:head>
<h:body>
<h:form id="helloForm">
<p:growl id="messages" />
<p:panelGrid id="panelGridPrincipal" styleClass="panelGridPrincipal">
<p:row>
<p:column colspan="2">
<div style="width: 100%; height: 25px; padding-left: 230px;">
<p:commandButton id="botao1" value="Bookmark" styleClass="button"/>
<p:commandButton id="botao2" value="Bookmark2" styleClass="button"
actionListener="#{mainBean.renderMenuVendas}" update="panel" >
<f:setPropertyActionListener value="2" target="#{mainBean.menuType}" />
</p:commandButton>
</div>
</p:column>
</p:row>
<p:row>
<p:column id="column2" style="width:200px;background-color: #5A5858;">
<h:panelGrid id="panel" styleClass="panelGridMenu" columns="1">
<p:menu id="dynamicMenu" model="#{mainBean.sideMenu}" rendered="#{mainBean.showMenuVendas}" style="width:189px;margin-right:100%;"/>
<p:spacer width="50px" height="700px" />
</h:panelGrid>
</p:column>
<p:column style="background-color: white">
<p:outputPanel id="outputPanelConteudo">
<ui:include src="#{mainBean.paginaAtual}"/>
</p:outputPanel>
</p:column>
</p:row>
</p:panelGrid>
</h:form>
</h:body>
</f:view>
</html>
pagina incluída
<?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">
<p:dataTable id="avioes" var="aviao" value="#{listaAvioesBean.aviao}"
rowKey="#{aviao.numeroSerie}"
selection="#{listaAvioesBean.selectedAviao}" selectionMode="single"
style="width:500px">
<p:column style="width:75px">
<f:facet name="header">
<h:outputText value="Numero de Serie" />
</f:facet>
<h:outputText value="#{aviao.numeroSerie}" />
</p:column>
<p:column style="width:100px">
<f:facet name="header">
<h:outputText value="Marca" />
</f:facet>
<h:outputText value="#{aviao.marca}" />
</p:column>
<p:column style="width:75px">
<f:facet name="header">
<h:outputText value="Modelo" />
</f:facet>
<h:outputText value="#{aviao.modelo}" />
</p:column>
<p:column style="width:75px">
<p:commandButton id="insertAviao" value="Inserir" title="Visualizar">
<f:setPropertyActionListener value="#{aviao}"
target="#{listaAvioesBean.selectedAviao}" />
</p:commandButton>
<p:commandButton id="removerAviao" style="width:50px" value="P"
title="Deletar" action="#{listaAvioesBean.removerAviao}"
update=":helloForm:outputPanelConteudo">
<f:setPropertyActionListener value="#{aviao}"
target="#{listaAvioesBean.selectedAviao}" />
</p:commandButton>
</p:column>
</p:dataTable>
</html>
Managed bean (Da pagina incluída)
[code]package br.com.erp.beans;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import br.com.erp.object.Aviao;
@ManagedBean(name=“listaAvioesBean”)
@ViewScoped
public class ListaAvioesBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 8076960891132613195L;
private Aviao selectedAviao;
private List<Aviao> aviao;
public ListaAvioesBean(){
aviao = new ArrayList<Aviao>();
System.out.println("ListaAvioesBean Criado!");
}
@PostConstruct
public void init(){
System.out.println("Iniciando o bean!");
populateListAvioes();
}
public void executa(){
System.out.println("Executou");
}
private void populateListAvioes(){
Aviao av1 = new Aviao();
av1.setMarca("Marca1");
av1.setModelo("M1");
av1.setNumeroSerie("1234");
aviao.add(av1);
av1 = new Aviao();
av1.setMarca("Marca2");
av1.setModelo("M2");
av1.setNumeroSerie("1111");
aviao.add(av1);
av1 = new Aviao();
av1.setMarca("Marca3");
av1.setModelo("M3");
av1.setNumeroSerie("4321");
aviao.add(av1);
}
public Aviao getSelectedAviao() {
return selectedAviao;
}
public void setSelectedAviao(Aviao selectedAviao) {
this.selectedAviao = selectedAviao;
}
public List<Aviao> getAviao() {
return aviao;
}
public void setAviao(List<Aviao> aviao) {
this.aviao = aviao;
}
public void removerAviao(){
boolean removeu = aviao.remove(selectedAviao);
if(removeu)
System.out.println("Avião removido com sucesso");
}
public void executa(ActionEvent e){
System.out.println();
}
}
[/code]
Managed Bean (Pagina Principal)
[code]package br.com.erp.beans;
import java.io.Serializable;
import javax.el.MethodExpression;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.MethodExpressionActionListener;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;
@ManagedBean(name=“mainBean”)
@ViewScoped
public class MainBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = -5433029933311534353L;
private boolean showMenuVendas = false;
private int menuType;
private String paginaAtual;
private MenuModel sideMenu;
public MainBean(){
sideMenu = new DefaultMenuModel();
System.out.println();
}
public boolean getShowMenuVendas() {
return showMenuVendas;
}
public void setShowMenuVendas(boolean showMenuVendas) {
this.showMenuVendas = showMenuVendas;
}
public void renderMenuVendas() {
showMenuVendas = true;
}
public MenuModel getSideMenu() {
switch (menuType) {
case 1:
//createMenuVendas(sideMenu);
break;
case 2:
createMenuVendas(sideMenu);
break;
default:
break;
}
return sideMenu;
}
public void setSideMenu(MenuModel sideMenu) {
this.sideMenu = sideMenu;
}
public int getMenuType() {
return menuType;
}
public void setMenuType(int menuType) {
this.menuType = menuType;
}
private void createMenuVendas(MenuModel sideMenu){
Submenu submenu = new Submenu();
submenu.setId("submenu1");
submenu.setLabel("Dynamic Submenu 1");
MenuItem item = new MenuItem();
item.setId("menuitem11");
item.setValue("Dynamic Menuitem 1.1");
item.setProcess("@this");
//item.setActionExpression(criarAcao("#{mainBean.setPagina1}"));
item.addActionListener(new MethodExpressionActionListener(criarAcao("#{mainBean.setPagina1}")));
item.setUpdate("outputPanelConteudo");
submenu.getChildren().add(item);
sideMenu.addSubmenu(submenu);
//Second submenu
submenu = new Submenu();
submenu.setId("submenu2");
submenu.setLabel("Dynamic Submenu 2");
//Para usar o metodo setActionExpression o metodo do bean NÂO pode ter argumentos
item = new MenuItem();
item.setId("menuitem21");
item.setValue("Dynamic Menuitem 2.1");
item.setProcess("@this");
//item.setActionExpression(criarAcao("#{mainBean.setPagina2}"));
item.addActionListener(new MethodExpressionActionListener(criarAcao("#{mainBean.setPagina2}")));
item.setUpdate("outputPanelConteudo");
submenu.getChildren().add(item);
item = new MenuItem();
item.setId("menuitem22");
item.setValue("Dynamic Menuitem 2.2");
item.setProcess("@this");
//item.setActionExpression(criarAcao("#{mainBean.setPagina3}"));
item.addActionListener(new MethodExpressionActionListener(criarAcao("#{mainBean.setPagina3}")));
item.setUpdate("outputPanelConteudo");
submenu.getChildren().add(item);
sideMenu.addSubmenu(submenu);
}
public String getPaginaAtual() {
return paginaAtual;
}
public void setPaginaAtual(String paginaAtual) {
this.paginaAtual = paginaAtual;
}
private MethodExpression criarAcao(String acao) {
FacesContext context = FacesContext.getCurrentInstance();
MethodExpression methodExpression = context.getApplication().getExpressionFactory().createMethodExpression(
context.getELContext(), acao, null, new Class[]{ActionEvent.class});
return methodExpression;
}
public void setPagina1(ActionEvent e){
setPaginaAtual("pagina1.xhtml");
}
public void setPagina2(ActionEvent e){
setPaginaAtual("pagina2.xhtml");
}
public void setPagina3(ActionEvent e){
setPaginaAtual("pagina3.xhtml");
}
}
[/code]