Após mostrar o cenário, conto os problemas.
O cenário é o seguinte:
- myfaces 1.3
O “sisteminha” de exemplo é composto dos seguintes arquivos:
- problemaBean.java (backBean)
- destino_final.jsp
- problema1.jsp
- problema2.jsp
- faces-config.xml
- web.xml
o sistema funciona da seguinte maneira:
acessando http://localhost:8080/Exemplo/problema1.jsf
é exibido um dataTable, com links e valores…
Esse dataTable é preenchido por uma lista, que está no construtor do Bean, ou seja, o data table já “nasce” carregado com os valores e links para acionar.
Quando acionado o link EDITAR, vc é “jogado” para a página destino_final.jsp, e é exibido o numero que vc escolheu.
Até aqui maravilha, tudo funciona.
acessando http://localhost:8080/Exemplo/problema2.jsf
é exibido uma página semelhante a problema1.jsp, porém, com pequenas alterações, ou seja, contendo um botão pesquisar e SEM valores do datatable e links para acionar.
O datatable é exibido sem valores, pois ele utiliza uma lista que não é preenchida no construtor do bean.
O datatable utiliza uma lista que é preenchida através de uma action “pesquisar” no bean.
Depois de pressionado o botão pesquisar, o dataTable é preenchido.
Porém, quando pressionado os links editar… não ocorre nenhuma ação.
Baseado nisso pergunto:
1 - como faço para funcionar a navegação mantendo o bean de request?
2 - colocando a tag no faces, a tela_final.jsp, não consegue recuperar o valor escolhido.
Nem mesmo utilizando tags como savestate, a não ser que utilizei errado o savestate.
OOs fontes descritos a seguir.
Obrigado.
package bean;
import java.util.ArrayList;
import java.util.List;
public class problemaBean {
private List<String> umaLista;
private List<String> umaListaPesquisada;
private Integer clicado;
public problemaBean() {
umaLista = populaDados();
}
public List<String> getUmaLista() {
return umaLista;
}
public void setUmaLista(List<String> umaLista) {
this.umaLista = umaLista;
}
public String acao(){
return "destino_final";
}
public List<String> getUmaListaPesquisada() {
if (umaListaPesquisada ==null){
return new ArrayList<String>();
}
return umaListaPesquisada;
}
public void setUmaListaPesquisada(List<String> umaListaPesquisada) {
this.umaListaPesquisada = umaListaPesquisada;
}
//metodo pesquisar, simula a pesquisa no banco de dados
//imagine que os dados da lista esteja vindo de um bd,
//e estes dados estão sendo trazidos baseado em um parametro de entrada
public String pesquisar(){
umaListaPesquisada = populaDados();
return ""; //retorna vazio, ou seja, força a permanencia na mesma página, caso seja usado em um action no jsf
}
public Integer getClicado() {
return clicado;
}
public void setClicado(Integer clicado) {
this.clicado = clicado;
}
//este metodo simula outras camadas, como Controle, DAO, etc.
private List<String> populaDados(){
List <String> listaGerada = new ArrayList<String>();
for (int i = 1; i < 21; i++) {
listaGerada.add(Integer.toString(i));
}
return listaGerada;
}
}
--------------------------- problema1.jsp
<%@ page contentType=“text/html;charset=windows-1252”%>
<%@ taglib uri=“http://java.sun.com/jsf/html” prefix=“h”%>
<%@ taglib uri=“http://java.sun.com/jsf/core” prefix=“f”%>
<%@ taglib uri=“http://myfaces.apache.org/tomahawk” prefix=“t”%>
<f:view>
Exemplo - Cenário 1 - Navegação funciona.
<f:subview id=“cabecalhos”>
<jsp:include page=“cabecalho_jsf.jsp” />
</f:subview>
<body>
<h:form>
Lista de valores
<br>
A lista é carregada no construtor do Bean
<br>
Logo, a navegação funciona, ou seja, quando ocorre um clique em editar
<br> é executado uma action, e vc é direcionado para a pagina destino_final.
<h:dataTable var="dados" value="#{mngProblemaBean.umaLista}">
<h:column id="col1">
<t:commandLink id="lnk1" action="#{mngProblemaBean.acao}" value="EDITAR" >
<t:updateActionListener property="#{mngProblemaBean.clicado}" value="#{dados}"></t:updateActionListener>
</t:commandLink>
</h:column>
<h:column id="col2">
<h:outputText value="#{dados}" />
</h:column>
</h:dataTable>
</h:form>
</body>
</html>
</f:view>
------------------------------------- problema2.jsp
<%@ page contentType=“text/html;charset=windows-1252”%>
<%@ taglib uri=“http://java.sun.com/jsf/html” prefix=“h”%>
<%@ taglib uri=“http://java.sun.com/jsf/core” prefix=“f”%>
<%@ taglib uri=“http://myfaces.apache.org/tomahawk” prefix=“t”%>
<f:view>
Exemplo - Cenário 2 - Navegação NÃO funciona.
<f:subview id=“cabecalhos”>
<jsp:include page=“cabecalho_jsf.jsp” />
</f:subview>
<body>
<h:form>
Lista de valores após pesquisar
<br>
<h:commandButton alt="Pesquisar" action="#{mngProblemaBean.pesquisar}" />
<br>
A lista é carregada após ser executado o método pesquisar no bean.
<br>
Logo, a navegação Não funciona, ou seja, quando ocorre um clique em editar
<br> TEORICAMENTE é (ou deveria ser hehe) executado uma action, e vc NÃO é direcionado para a pagina destino_final.
<h:dataTable var="dados" value="#{mngProblemaBean.umaListaPesquisada}">
<h:column id="col1">
<t:commandLink id="lnk1" action="#{mngProblemaBean.acao}"
value="EDITAR">
<t:updateActionListener property="#{mngProblemaBean.clicado}"
value="#{dados}"></t:updateActionListener>
</t:commandLink>
</h:column>
<h:column id="col2">
<h:outputText value="#{dados}" />
</h:column>
</h:dataTable>
</h:form>
</body>
</html>
</f:view>
--------------------------- destino_final.jsp
<%@ page contentType=“text/html;charset=windows-1252”%>
<%@ taglib uri=“http://java.sun.com/jsf/html” prefix=“h”%>
<%@ taglib uri=“http://java.sun.com/jsf/core” prefix=“f”%>
<%@ taglib uri=“http://myfaces.apache.org/tomahawk” prefix=“t”%>
<f:view>
-------------------------- >>>> Destino final <<<<<<<<<<<<<-----------------
<f:subview id=“cabecalhos”>
<h:outputText value=“Faz de conta que vc foi para a página de editar” />
<h:outputText value=" ------------------ Destino final ----------------------- " />
<h:outputText value=" ------------------ " />
<h:outputText value=" ------------------ " />
<h:outputText value=" ------------------ " />
<h:outputText value=" ------------------ Você escolheu o número: " />
<h:outputText value="#{mngProblemaBean.clicado}" />
</f:subview>
<body>
</body>
</html>
</f:view>
---------------------- faces-config.xml
<?xml version="1.0"?> mngProblemaBean bean.problemaBean request<navigation-rule>
<navigation-case>
<from-outcome>destino_final</from-outcome>
<to-view-id>/destino_final.jsf</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
----------------------------- web.xml
<?xml version="1.0"?>
Exemplo
State saving method: “client” or “server” (= default)
See JSF Specification 2.5.2
javax.faces.STATE_SAVING_METHOD
server
This parameter tells MyFaces if javascript code should be allowed in the
rendered HTML output.
If javascript is allowed, command_link anchors will have javascript code
that submits the corresponding form.
If javascript is not allowed, the state saving info and nested parameters
will be added as url parameters.
Default: "true"
org.apache.myfaces.ALLOW_JAVASCRIPT
true
This parameter tells MyFaces if javascript code should be allowed in the
rendered HTML output.
If javascript is allowed, command_link anchors will have javascript code
that submits the corresponding form.
If javascript is not allowed, the state saving info and nested parameters
will be added as url parameters.
Default: “false”
Setting this param to true should be combined with STATE_SAVING_METHOD "server" for
best results.
This is an EXPERIMENTAL feature. You also have to enable the detector filter/filter mapping below to get
JavaScript detection working.</description>
org.apache.myfaces.DETECT_JAVASCRIPT
false
If true, rendered HTML code will be formatted, so that it is “human readable”.
i.e. additional line separators and whitespace will be written, that do not
influence the HTML code.
Default: "true"
org.apache.myfaces.PRETTY_HTML
true
If true, a javascript function will be rendered that is able to restore the
former vertical scroll on every request. Convenient feature if you have pages
with long lists and you do not want the browser page to always jump to the top
if you trigger a link or button action that stays on the same page.
Default: "false"
org.apache.myfaces.AUTO_SCROLL
true
Format: 10 - 10 bytes
10k - 10 KB
10m - 10 MB
1g - 1 GB</description>
uploadThresholdSize
100k