Ao clicar em command button a página nova nao carrega! (resolvido! =D)

5 respostas
K

Oi gente, tudo bem?

Estou aprendendo JSF e resolvi implementar alguns exemplos simples… Mas já no primeiro estou enfrentando alguns problemas!

A aplicação que estou construindo é muito simples: na primeira página aparece “Qual o seu nome?” e uma área para escrever e um button OK. A pessoa digita seu nome e na outra página aparece “Oi (nome da pessoa)!”.

Meu problema é que, quando eu clico no button OK, nada acontece… :confused:

Gostaria de saber se eu tenho que acrescentar algum action listener… pois o tutorial que eu segui (http://www.exadel.com/tutorial/jsf/jsftutorial-kickstart.html) não fala nada sobre isso!!

Meu faces-config.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE faces-config PUBLIC
    "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
    "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>
	<managed-bean>
		<managed-bean-name>personBean</managed-bean-name>
		<managed-bean-class>testeWeb.PersonBean</managed-bean-class>
		<managed-bean-scope>request</managed-bean-scope>
	</managed-bean>
	<managed-bean>
		<managed-bean-name>doLogin</managed-bean-name>
		<managed-bean-class>testeWeb.DoLogin</managed-bean-class>
		<managed-bean-scope>request</managed-bean-scope>
	</managed-bean>
	<navigation-rule>
		<display-name>/pages/inputname</display-name>
		<from-view-id>/pages/inputname.jsp</from-view-id>
		<navigation-case>
			<from-outcome>greeting</from-outcome>
			<to-view-id>/pages/greeting.jsp</to-view-id>
		</navigation-case>
	</navigation-rule>
</faces-config>

web.xml:

<web-app id="WebApp_ID">
	<display-name>testeWeb</display-name>
	<listener>
  		<listener-class>com.sun.faces.config.ConfigureListener</listener-class> 
	</listener>
	<servlet>
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-class>
		javax.faces.webapp.FacesServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>Faces Servlet</servlet-name>
		<url-pattern>*.faces</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

inputname.jsp:

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:loadBundle basename="testeWeb.bundle.messages" var="msg"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Primeira Tentativa JSF</title>
</head>
<body>
	<f:view>
	     <h1>
		      <h:outputText value="#{msg.inputname_header}"/>
	     </h1>
	     <h:form id="helloForm">
		      <h:outputText value="#{msg.prompt}"/>
		      <h:inputText value="#{personBean.personName}" />
		      <h:commandButton action="#{doLogin.doLogin}" value="#{msg.button_text}" />
	     </h:form>
	</f:view>
</body>
</html>

greeting.jsp:

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:loadBundle basename="testeWeb.bundle.messages" var="msg"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Oi!! Primeiro Teste JSF</title>
	</head>
<body>
	<f:view>
    	<h3>
 			<h:outputText value="#{msg.greeting_text}" />,
 			<h:outputText value="#{personBean.personName}" />
         	<h:outputText value="#{msg.sign}" />
    		</h3>
    </f:view>
</body>
</html>

index.jsp

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
  <jsp:forward page="/pages/inputname.jsp" />
</body>
</html>

e, finalmente, minhas classes:

PersonBean.java

package testeWeb;

public class PersonBean {
	 String personName;
		
	   /**
	   * @return Nome Usuario
	   */
	   public String getpersonName() {
	      return personName;
	   }

	   /**
	   * @param Nome Usuario
	   */
	   public void setpersonName(String name) {
	      personName = name;
	   }
}

e DoLogin.java:

package testeWeb;

public class DoLogin {
	public String doLogin(){
		return "greeting";
	}
}

Meu projeto tem a seguinte estrutura:

testeWeb
—Java Source
------DoLogin.java
----- PersonBean.java
—testeWeb.bundle
------messages.properties
—WebContent
------META-INF
------pages
---------greeting.jsp
---------inputname.jsp
------WEB-INF
---------faces-config.xml
---------web.xml
------index.jsp

Muito obrigada… conto com a ajuda de vocês!! =]

5 Respostas

L

Tenta mudar a navigation rule do faces-config

Deixa assim:

<navigation-rule> <display-name>/pages/inputname</display-name> <from-view-id>/pages/inputname.faces</from-view-id> <navigation-case> <from-outcome>greeting</from-outcome> <to-view-id>/pages/greeting.faces</to-view-id> </navigation-case> </navigation-rule>

Faz isso e posta novamente dizendo o q acontece.

gomesrod

Olá,

A primeira coisa: no arquivo de configuração web.xml, você define que as páginas tratadas pelo Servlet do Faces são as “*.faces”, porém suas páginas tem extensão “.jsp”. É necessário alterar um dos dois.

Também existe um pequeno erro no Bean, o nome dos métodos get/set deve começar com maiuscula (após as palavras “get” e “set”), assim:
getNome() ao invés de getnome()
setNome(…) ao invés de setnome(…)

K

oi gente!

eu troquei o que vocês sugeriram.. meus códigos ficaram assim:

faces-config.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE faces-config PUBLIC
    "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
    "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>
	<managed-bean>
		<managed-bean-name>personBean</managed-bean-name>
		<managed-bean-class>testeWeb.PersonBean</managed-bean-class>
		<managed-bean-scope>request</managed-bean-scope>
	</managed-bean>
	<managed-bean>
		<managed-bean-name>doLogin</managed-bean-name>
		<managed-bean-class>testeWeb.DoLogin</managed-bean-class>
		<managed-bean-scope>request</managed-bean-scope>
	</managed-bean>
	<navigation-rule>
		<display-name>/pages/inputname</display-name>
		<from-view-id>/pages/inputname.faces</from-view-id>
		<navigation-case>
			<from-outcome>greeting</from-outcome>
			<to-view-id>/pages/greeting.faces</to-view-id>
		</navigation-case>
	</navigation-rule>
</faces-config>

web.xml:

<web-app id="WebApp_ID" version="2.3">
	<display-name>testeWeb</display-name>
	<listener>
  		<listener-class>com.sun.faces.config.ConfigureListener</listener-class> 
	</listener>
	<servlet>
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-class>
		javax.faces.webapp.FacesServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>Faces Servlet</servlet-name>
		<url-pattern>*.faces</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

index.jsp:

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<meta http-equiv="Refresh" content="0;url=inputname.faces">  
<title>Teste</title>
</head>
<body>
  <jsp:forward page="/pages/inputname.jsp" />
</body>
</html>

Mas continuou dando o mesmo problema.. a página não muda! :/

gomesrod, obrigada pela dica do get, set! eu troquei lá!! =]

Tem mais alguma coisa que eu possa alterar?!

obrigada...
[]'s

gomesrod

Nos arquivos de configuração as páginas aparecem como “.faces” mas os arquivos são “.jsp”

Renomeie suas páginas para que fiquem de acordo com o que está nos arquivos web.xml e faces-config (ou vice-versa)

K

FUNCIONOU!! =] :-D

meus arquivos ficaram:
web.xml:

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<meta http-equiv="Refresh" content="0;url=greeting.jsf">  
<title>Teste</title>
</head>
<body>
  <jsp:forward page="/pages/inputname.faces" />
</body>
</html>

faces-config.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE faces-config PUBLIC
    "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
    "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>
	<managed-bean>
		<managed-bean-name>personBean</managed-bean-name>
		<managed-bean-class>testeWeb.PersonBean</managed-bean-class>
		<managed-bean-scope>request</managed-bean-scope>
	</managed-bean>
	<managed-bean>
		<managed-bean-name>doLogin</managed-bean-name>
		<managed-bean-class>testeWeb.DoLogin</managed-bean-class>
		<managed-bean-scope>request</managed-bean-scope>
	</managed-bean>
	<navigation-rule>
		<display-name>/pages/inputname</display-name>
		<from-view-id>/pages/inputname.jsp</from-view-id>
		<navigation-case>
			<from-outcome>greeting</from-outcome>
			<to-view-id>/pages/greeting.jsp</to-view-id>
		</navigation-case>
	</navigation-rule>
</faces-config>

index.jsp

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<meta http-equiv="Refresh" content="0;url=greeting.jsf">  
<title>Teste</title>
</head>
<body>
  <jsp:forward page="/pages/inputname.faces" />
</body>
</html>

O problema eu acho que estava no index.sjp, que, qo inves de colocar greeting.jsf eutinha colocado o inputname!!

brigada, gente, pela ajuda!!

Criado 23 de maio de 2007
Ultima resposta 24 de mai. de 2007
Respostas 5
Participantes 3