Boa tarde,
Estou com o seguinte problema: estou trabalhando com jsf tenho 2 páginas:
Login e Cadastro:
A página Login tem 2 campos “usuário” e “senha”, quando eu acionar o botão confirmar da página Login eu gostaria que enviasse para a página Cadastro o nome do usuário. Estou mapeando o faces-config.xml mais não está funcionando:
Classe Bean
public class Login implements Serializable{
public String usuario;
public String senha;
public Login(){
}
public Login(String usuario, String senha){
this.usuario = usuario;
this.senha = senha;
}
public String getUsuario(){
return usuario;
}
public void setUsuario(){
this.usuario = usuario;
}
public String getSenha(){
return senha;
}
public void setSenha(){
this.senha = senha;
}
}
faces-config.xml
<managed-bean>
<managed-bean-name>logincliente</managed-bean-name>
<managed-bean-class>eei.bean.Login</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>usuario</property-name>
<property-class>java.lang.String</property-class>
<value>#{usuario}</value>
</managed-property>
</managed-bean>
Cadastro.jsp
<h:outputText binding="#{Cadastro.jotUsuario}" id="jotUsuario" value="#{logincliente.usuario}"/>
O problema é que o valor usuario vem como null.
OBS quando eu entro no faces-config.xml e deixo o value estático(UsuarioTeste), funciona o problema é quando eu quero resgatar de um campo textfield.
Alguém poderia me ajudar?
Abraço.
vc ta querendo fazer uma tela de login é isso?? Post o codigo da pagina que tem o form, o bean, o faces-config.
aqui vai amigo:
Login.jsp
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : Page1
Created on : 19/06/2008, 14:04:10
Author : amgarcia
-->
<jsp:root version="2.1" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:ice="http://www.icesoft.com/icefaces/component" xmlns:jsp="http://java.sun.com/JSP/Page">
<jsp:directive.page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"/>
<f:view>
<html>
<head>
<ice:outputStyle binding="#{Login.outputStyle1}" href="./resources/stylesheet.css" id="outputStyle1"/>
<ice:outputStyle binding="#{Login.outputStyle2}" href="./xmlhttp/css/xp/xp.css" id="outputStyle2"/>
</head>
<body style="-rave-layout: grid">
<ice:form binding="#{Login.htmlForm1}" id="htmlForm1">
<h:inputText binding="#{Login.jtfLogin}" id="jtfLogin" style="height: 24px; left: 288px; top: 120px; position: absolute; width: 144px"/>
<h:commandButton action="#{Login.jbtConfirmar_action}" binding="#{Login.jbtConfirmar}" id="jbtConfirmar"
style="height: 24px; left: 312px; top: 216px; position: absolute; width: 72px" value="Confirmar"/>
<h:inputText binding="#{Login.jtfSenha}" id="jtfSenha" style="height: 24px; left: 288px; top: 168px; position: absolute; width: 144px"/>
</ice:form>
</body>
</html>
</f:view>
</jsp:root>
Login.java(Bean)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bean;
import java.io.Serializable;
/**
*
* @author amgarcia
*/
public class Login implements Serializable{
public String usuario;
public String senha;
public Login() {
}
public Login(String usuario, String senha) {
this.usuario = usuario;
this.senha = senha;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
public String getUsuario() {
return usuario;
}
public void setUsuario(String usuario) {
this.usuario = usuario;
}
}
Login.java
/*
* Login.java
*
* Created on 19/06/2008, 14:04:10
* Copyright amgarcia
*/
package AplicacaoWebForum;
import com.icesoft.faces.component.style.OutputStyle;
import com.icesoft.faces.component.ext.HtmlForm;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import javax.faces.FacesException;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.component.html.HtmlInputText;
/**
* <p>Page bean that corresponds to a similarly named JSP page. This
* class contains component definitions (and initialization code) for
* all components that you have defined on this page, as well as
* lifecycle methods and event handlers where you may add behavior
* to respond to incoming events.</p>
*/
public class Login extends AbstractPageBean {
// <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
private int __placeholder;
/**
* <p>Automatically managed component initialization. <strong>WARNING:</strong>
* This method is automatically generated, so any user-specified code inserted
* here is subject to being replaced.</p>
*/
private void _init() throws Exception {
}
private OutputStyle outputStyle1 = new OutputStyle();
public OutputStyle getOutputStyle1() {
return outputStyle1;
}
public void setOutputStyle1(OutputStyle os) {
this.outputStyle1 = os;
}
private OutputStyle outputStyle2 = new OutputStyle();
public OutputStyle getOutputStyle2() {
return outputStyle2;
}
public void setOutputStyle2(OutputStyle os) {
this.outputStyle2 = os;
}
private HtmlForm htmlForm1 = new HtmlForm();
public HtmlForm getHtmlForm1() {
return htmlForm1;
}
public void setHtmlForm1(HtmlForm f) {
this.htmlForm1 = f;
}
private HtmlInputText jtfLogin = new HtmlInputText();
public HtmlInputText getJtfLogin() {
return jtfLogin;
}
public void setJtfLogin(HtmlInputText hit) {
this.jtfLogin = hit;
}
private HtmlCommandButton jbtConfirmar = new HtmlCommandButton();
public HtmlCommandButton getJbtConfirmar() {
return jbtConfirmar;
}
public void setJbtConfirmar(HtmlCommandButton hcb) {
this.jbtConfirmar = hcb;
}
private HtmlInputText jtfSenha = new HtmlInputText();
public HtmlInputText getJtfSenha() {
return jtfSenha;
}
public void setJtfSenha(HtmlInputText hit) {
this.jtfSenha = hit;
}
// </editor-fold>
/**
* <p>Construct a new Page bean instance.</p>
*/
public Login() {
}
/**
* <p>Callback method that is called whenever a page is navigated to,
* either directly via a URL, or indirectly via page navigation.
* Customize this method to acquire resources that will be needed
* for event handlers and lifecycle methods, whether or not this
* page is performing post back processing.</p>
*
* <p>Note that, if the current request is a postback, the property
* values of the components do <strong>not</strong> represent any
* values submitted with this request. Instead, they represent the
* property values that were saved for this view when it was rendered.</p>
*/
public void init() {
// Perform initializations inherited from our superclass
super.init();
// Perform application initialization that must complete
// *before* managed components are initialized
// TODO - add your own initialiation code here
// <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
// Initialize automatically managed components
// *Note* - this logic should NOT be modified
try {
_init();
} catch (Exception e) {
log("Page1 Initialization Failure", e);
throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
}
// </editor-fold>
// Perform application initialization that must complete
// *after* managed components are initialized
// TODO - add your own initialization code here
}
/**
* <p>Callback method that is called after the component tree has been
* restored, but before any event processing takes place. This method
* will <strong>only</strong> be called on a postback request that
* is processing a form submit. Customize this method to allocate
* resources that will be required in your event handlers.</p>
*/
public void preprocess() {
}
/**
* <p>Callback method that is called just before rendering takes place.
* This method will <strong>only</strong> be called for the page that
* will actually be rendered (and not, for example, on a page that
* handled a postback and then navigated to a different page). Customize
* this method to allocate resources that will be required for rendering
* this page.</p>
*/
public void prerender() {
}
/**
* <p>Callback method that is called after rendering is completed for
* this request, if <code>init()</code> was called (regardless of whether
* or not this was the page that was actually rendered). Customize this
* method to release resources acquired in the <code>init()</code>,
* <code>preprocess()</code>, or <code>prerender()</code> methods (or
* acquired during execution of an event handler).</p>
*/
public void destroy() {
}
public String jbtConfirmar_action() {
bean.Login vLogin = new bean.Login("adad","12345");
return "case1";
}
}
Cadastro.jsp
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : Cadastro
Created on : 19/06/2008, 14:07:18
Author : amgarcia
-->
<jsp:root version="2.1" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:ice="http://www.icesoft.com/icefaces/component" xmlns:jsp="http://java.sun.com/JSP/Page">
<jsp:directive.page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"/>
<f:view>
<html>
<head>
<ice:outputStyle binding="#{Cadastro.outputStyle1}" href="./resources/stylesheet.css" id="outputStyle1"/>
<ice:outputStyle binding="#{Cadastro.outputStyle2}" href="./xmlhttp/css/xp/xp.css" id="outputStyle2"/>
</head>
<body style="-rave-layout: grid">
<ice:form binding="#{Cadastro.htmlForm1}" id="htmlForm1">
<h:outputText binding="#{Cadastro.jotUsuario}" id="jotUsuario"
style="position: absolute; left: 264px; top: 96px; width: 120px; height: 24px" value="#{logincliente.usuario}"/>
<h:commandButton action="#{Cadastro.button1_action}" binding="#{Cadastro.button1}" id="button1"
style="position: absolute; left: 336px; top: 192px" value="Submit"/>
</ice:form>
</body>
</html>
</f:view>
</jsp:root>
faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="1.2"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
<managed-bean>
<managed-bean-name>SessionBean1</managed-bean-name>
<managed-bean-class>AplicacaoWebForum.SessionBean1</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>Login</managed-bean-name>
<managed-bean-class>AplicacaoWebForum.Login</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>RequestBean1</managed-bean-name>
<managed-bean-class>AplicacaoWebForum.RequestBean1</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>ApplicationBean1</managed-bean-name>
<managed-bean-class>AplicacaoWebForum.ApplicationBean1</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>Cadastro</managed-bean-name>
<managed-bean-class>AplicacaoWebForum.Cadastro</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>logincliente</managed-bean-name>
<managed-bean-class>bean.Login</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>usuario</property-name>
<value>#{usuario}</value>
</managed-property>
</managed-bean>
<navigation-rule>
<from-view-id>/Login.jsp</from-view-id>
<navigation-case>
<from-outcome>case1</from-outcome>
<to-view-id>/Cadastro.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
Ok acho que eu entedi o que vc deve fazer é o seguinte:
primeiro faça um metodo que valida o usuario por exemplo:
public String validarUser(){
if (getLogin().equals("") || getSenha().equals("")){
return "false";
}else
return "true";
}
Logo após vá em faces config e na aba navegation rule
e coloque a pagina e os links, um contendo True e outro link contendo false, o link true ele vai direcionar para a pagina seguinte, ou seja, a pagina de que vem apos usuario correto, o outro vai apara um pagina de erro ou retorna para ele mesmo com alguma menssagem do tipo, senha ou usuario invalido.
pega o valor do campo inputtext e coloca na outra pagania como um outputtext vc entendeu???