Redirecionar a index.jsp para xhtml

49 respostas
E

Pessoal boa noite, ou bom dia rsrs.

Então eu criei um projeto JSF e criei uma index.jsp, como faço pra chamar essa jsp redirecionando para meu xhtml?

49 Respostas

H

Assim:

<jsp:forward page="/nomeDaPagina.jsf"/>

Se a sua pagina.xhtml estiver em um diretório diferente da sua index, você deve colocar também o nome do diretório/nomeDaPagina.jsf.

E

henriquejhc:
Assim:

<jsp:forward page="/nomeDaPagina.jsf"/>

Se a sua pagina.xhtml estiver em um diretório diferente da sua index, você deve colocar também o nome do diretório/nomeDaPagina.jsf.

Mas eu coloco essa tag no web.xml?

B

não, na página index.jsp.

E

Eu tenho que fazer so isso?

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<jsp:forward page="/pages/Cadastro.jsf"/> // aqui a linha que o amigo me passou

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

</body>
</html>

Aqui o teste que queria subir:

<<!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:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">

	<!-- 
		Replace path to template, title, header and body
		with actual data.
	 -->
	<ui:composition template="/WebContent/pages/Cadastro.xhtml">
			<ui:define name="pageTitle">Title</ui:define>
			<ui:define name="pageHeader">Header</ui:define>
			<ui:define name="body">
			
				<form jsfc="h:form" id="Form">				
				
				<div class = "retangle_top"/>
								
				<h: inputText id = "#{textNome} value = "Nada" />
								
				
				</form>
			</ui:define>
	</ui:composition>

</html>
H

Na index.jsp, coloque apenas isso.

&lt;jsp:forward page="/pages/Cadastro.jsf"/&gt;
E

henriquejhc:
Na index.jsp, coloque apenas isso.

&lt;jsp:forward page="/pages/Cadastro.jsf"/&gt;

Fiz o que falou mas continua na mesma. Olha a pagina de erro:


H

O erro é de página não encontrada. Se suas páginas são .jsf, de ser Cadastro.jsf.

E

Minhas paginas sao xhtml.

H

Se você fosse digitar a url no browser, como você escreveria?
cadastro.xhtml?

E

henriquejhc:
Se você fosse digitar a url no browser, como você escreveria?
cadastro.xhtml?

omg rs, .jsf. Mas eu coloquei jsf no redirecionamento.

H

Mas na imagem que você postou está com Cadastro.jsp.

E

Eu tenho uma index.jsp que contém a linha: <jsp:forward page="/pages/Cadastro.jsf"/>

e minha pagina Cadastro é xhtml, mas coloquei jsf para o redirecionamento.

H

Estranho. Colocando apenas aquela linha na sua index.jsp, era para funcionar. Tem alguma estrutura de diretórios para as páginas?

E

henriquejhc:
Estranho. Colocando apenas aquela linha na sua index.jsp, era para funcionar. Tem alguma estrutura de diretórios para as páginas?

Minha pagina Cadastro.xhtml esta na seguinte estrutura: Everton\WebContent\pages\Cadastro.xhtml

Agora uma pergunta, precisa configurar algum arquivo, como web-xml ou faces config pra funcionar isso?

H

Nada demais.

web.xml.

&lt;welcome-file-list&gt;
        &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
&lt;/welcome-file-list&gt;

E a sua index.jsp deve ter apenas essa linha:

&lt;jsp:forward page="/pages/Cadastro.jsf"/&gt;
E

eh =/ então ta tenso, ja nao sei mais o que fazer.

O web.xml ta assim:

<?xml version="1.0"?>
<web-app version="2.5" 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-app_2_5.xsd">
 
 <display-name>TesteWeb</display-name>
 
 <welcome-file-list>   
        <welcome-file>index.jsp</welcome-file>   
</welcome-file-list>  
 
 
 <context-param>
  <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
  <param-value>server</param-value>
 </context-param>
 <listener>
  <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
 </listener>
 <!-- Faces Servlet -->
 <servlet>
  <servlet-name>Faces Servlet</servlet-name>
  <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <!-- Faces Servlet Mapping -->
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>*.jsf</url-pattern>
 </servlet-mapping>
 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>
</web-app>
H

E como ta sua index? Ele ta chamando essa index?

E

A index ta igual vc falou:

<jsp:forward page="/pages/Cadastro.jsf"/>
H

A página index está sendo chamada? Coloca um código qualquer para ver se ela é executada.

E

Ta sim coloquei um breakpoint e subi o JBoss no debug ele entrou la :frowning:

E

verifica no seu web.xml se a sua extensão reconhecida para faces é .jsf mesmo:

<servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping>

verifica no seu web.xml se o default suffix está para .xhtml mesmo, ou para .jsp (qualquer coisa posta ele aqui para vermos)

<context-param>
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
    <param-value>.xhtml</param-value>
</context-param>

Outra coisa, se você quiser fazer o tomcat ler para “server:8080/seuprojeto/” algum arquivo específico, é só você alterar, no web.xml, o parâmetro welcome-file-list

<welcome-file-list>  
   <welcome-file>login.jsp</welcome-file>  
 </welcome-file-list>
E

Opa evufuji nao tinha essas configuraçoes nao, coloquei mas nao funcionou, vou colocar aqui o web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>Everton</display-name>
  
  <context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
  </context-param>
  
  <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>*.jsf</url-pattern>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
  
  <context-param>   
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>   
    <param-value>.xhtml</param-value>   
  </context-param> 
  
  <welcome-file-list>     
    <welcome-file>index.jsp</welcome-file>     
  </welcome-file-list>  
    
</web-app>

minha index index.jsp

<jsp:forward page="/pages/Cadastro.jsf"/>

minha Pagina cadastro que eu queria subir:

<<!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:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">

	<!-- 
		Replace path to template, title, header and body
		with actual data.
	 -->
	<ui:composition template="">
			<ui:define name="pageTitle">Title</ui:define>
			<ui:define name="pageHeader">Header</ui:define>
			<ui:define name="body">
			
				<form jsfc="h:form" id="Form">				
				
				<div class = "retangle_top"/>
								
				<h: inputText id = "#{textNome} value = "Nada" />
								
				
				</form>
			</ui:define>
	</ui:composition>

</html>
E

Agora quando eu tenho acessar a pagina pede pra mim fazer download do arquivo =[

olha a imagem


E

Em algum lugar você troca o mime type que o server informa ao navegador? Tipo, no seu template tem alguma metatag que fale o formato, algo como isso.

http-equiv="Content-Type" content="text/html; charset=utf-8" />

ou mesmo em algum bean seu.

E

Eu tirei o “<” do inicio, mas continuo do mesmo jeito, sobre o mime type não sei o que é =[

E

evefuji:
Em algum lugar você troca o mime type que o server informa ao navegador? Tipo, no seu template tem alguma metatag que fale o formato, algo como isso.

http-equiv="Content-Type" content="text/html; charset=utf-8" />

ou mesmo em algum bean seu.

nao tem nao

E

então defina no seu web.xml os mime types para cada extensão de arquivo:

<!-- MIME mapping -->
<mime-mapping>
        <extension>doc</extension>
        <mime-type>application/vnd.ms-word</mime-type>
</mime-mapping>
<mime-mapping>
        <extension>dsp</extension>
        <mime-type>text/html</mime-type>
</mime-mapping>
<mime-mapping>
        <extension>gif</extension>
        <mime-type>image/gif</mime-type>
</mime-mapping>
<mime-mapping>
        <extension>htm</extension>
        <mime-type>text/html</mime-type>
</mime-mapping>
<mime-mapping>
        <extension>html</extension>
        <mime-type>text/html</mime-type>
</mime-mapping>
<mime-mapping>
        <extension>jnlp</extension>
        <mime-type>application/x-java-jnlp-file</mime-type>
</mime-mapping>
<mime-mapping>
        <extension>jpeg</extension>
        <mime-type>image/jpeg</mime-type>
</mime-mapping>
<mime-mapping>
        <extension>jpg</extension>
        <mime-type>image/jpeg</mime-type>
</mime-mapping>
<mime-mapping>
        <extension>js</extension>
        <mime-type>application/x-javascript</mime-type>
</mime-mapping>
<mime-mapping>
        <extension>pdf</extension>
        <mime-type>application/pdf</mime-type>
</mime-mapping>
(etc... o jsf vc deveria colocar como text/html)
E

coloquei todos, mas ta abrindo a pg pra fazer dl =[

ta gerando no console ao final:

Buildfile: C:\Users\everton\Desktop\eclipse-jee-helios-SR1-win32-x86_64\eclipse\plugins\org.eclipse.jst.server.generic.jboss_1.6.1.v200904151730\buildfiles\jboss323.xml deploy.j2ee.web: [jar] Building jar: C:\Users\everton\Documents\web\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\Everton.war [move] Moving 1 file to C:\Program Files\jboss-5.0.0.CR2-jdk6\jboss-5.0.0.CR2\server\default\deploy BUILD SUCCESSFUL Total time: 10 seconds

Ta gerando o deploy normal

R

Olá.
Lembro de ter passado por esse erro,mas não consegui simular aqui

esse é meu web.xml q funciona:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5"&gt;
	&lt;display-name&gt;projetoBase&lt;/display-name&gt;
	&lt;context-param&gt;
		&lt;param-name&gt;com.sun.faces.expressionFactory&lt;/param-name&gt;
		&lt;param-value&gt;org.jboss.el.ExpressionFactoryImpl&lt;/param-value&gt;
	&lt;/context-param&gt;
	  
	&lt;context-param&gt;
		&lt;param-name&gt;javax.faces.CONFIG_FILES&lt;/param-name&gt;
		&lt;param-value&gt;/WEB-INF/faces-config.xml&lt;/param-value&gt;
	&lt;/context-param&gt;
	
	&lt;context-param&gt;
		&lt;param-name&gt;javax.faces.DEFAULT_SUFFIX&lt;/param-name&gt;
		&lt;param-value&gt;.xhtml&lt;/param-value&gt;
	&lt;/context-param&gt;
	&lt;context-param&gt;
		&lt;param-name&gt;javax.faces.STATE_SAVING_METHOD&lt;/param-name&gt;
		&lt;param-value&gt;client&lt;/param-value&gt;
	&lt;/context-param&gt;
	
	&lt;context-param&gt;
		&lt;param-name&gt;facelets.DEVELOPMENT&lt;/param-name&gt;
		&lt;param-value&gt;true&lt;/param-value&gt;
	&lt;/context-param&gt;
	
	&lt;context-param&gt;
		&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
		&lt;param-value&gt;
			/WEB-INF/applicationContext.xml
		&lt;/param-value&gt;
	&lt;/context-param&gt;
	
	&lt;welcome-file-list&gt;
		&lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
		&lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;
		&lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
		&lt;welcome-file&gt;default.html&lt;/welcome-file&gt;
		&lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;
		&lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;
	&lt;/welcome-file-list&gt;
	  
	&lt;context-param&gt;
		&lt;param-name&gt;org.ajax4jsf.VIEW_HANDLERS&lt;/param-name&gt;
		&lt;param-value&gt;com.sun.facelets.FaceletViewHandler&lt;/param-value&gt;
	&lt;/context-param&gt;
	
	&lt;filter&gt;
		&lt;display-name&gt;RichFaces Filter&lt;/display-name&gt;
		&lt;filter-name&gt;richfaces&lt;/filter-name&gt;
		&lt;filter-class&gt;org.ajax4jsf.Filter&lt;/filter-class&gt;
	&lt;/filter&gt;
	&lt;listener&gt;
		&lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
	&lt;/listener&gt;
	&lt;listener&gt;
		&lt;listener-class&gt;com.sun.faces.config.ConfigureListener&lt;/listener-class&gt;
	&lt;/listener&gt;
	&lt;filter-mapping&gt;
		&lt;filter-name&gt;richfaces&lt;/filter-name&gt;
		&lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
		&lt;dispatcher&gt;REQUEST&lt;/dispatcher&gt;
		&lt;dispatcher&gt;FORWARD&lt;/dispatcher&gt;
		&lt;dispatcher&gt;INCLUDE&lt;/dispatcher&gt;
	&lt;/filter-mapping&gt;

	&lt;servlet&gt;
		&lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
		&lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt;
		&lt;load-on-startup&gt;0&lt;/load-on-startup&gt;
	&lt;/servlet&gt;
	
	&lt;servlet-mapping&gt;
		&lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
		&lt;url-pattern&gt;*.faces&lt;/url-pattern&gt;
	&lt;/servlet-mapping&gt;
&lt;/web-app&gt;
E

Mas como vou adaptar seu web.xml com o meu??

E

por exemplo, esse extension filter aqui esta dando erro na hora do deploy.

<filter-mapping> <filter-name>extensionsFilter</filter-name> <url-pattern>*.jsf</url-pattern> </filter-mapping>

se eu tiro ele fica a pagina pra dl

R

Vai vendo o que se adapta ou não :smiley:

T

Everton,
Voce conseguiu pegar essa pagina jsp ?

Também estou precisando fazer isso.

Obrigado;

E

Consegui nao amigo =[, negocio ta feio kkk. Quando digito no browser http://localhost:8080/Everton, abre um dialog pra dl =[

T

Puts,

Beleza então, eu também estouy tentando aqui, qualquer coisa eu posto para nós =)

Abs

R

Everton,tu está usando RichFaces?

E

tem alguma linha mais ou menos assim response.setContentType(“text/html”); em algum constructor do seu bean?

Tenta colocar um metatag no seu template que defina o content type para text/html.

E

to não rafa.

evefuji eu nem tenho nenhum bean ainda so tenho essa pagina.

R

to não rafa.

evefuji eu nem tenho nenhum bean ainda so tenho essa pagina.

me mostra as libs do teu projeto.

E

common-annotations
commons-beanutils
commons-collections
commons-digester
commons-logging
jsf-tlds
jstl
standard

E

tá faltando libs aí, a do jsf-api, jsf-impl, facelets (não lembro se falta mais alguma)

E

ueh, eu fui dentro de web-inf\lib é tudo que ta la.

R

exatamente,tem coisa faltando ai.

E

mas como vou colocar algo? Se eu só criei o projeto e nao adicionei lib nem removi lib nenhuma.

E

baixa aqui os JAR’s: https://javaserverfaces.dev.java.net/servlets/ProjectDocumentList?folderID=10411 e pega tudo que estiver dentro da pasta lib do que vc baixou e coloca no lib do seu projeto. (a menos que você tenha o jsf-impl e o jsf-api no lib do seu tomcat, que isso não é necessário)

Mas esses jar’s deveriam causar outro erro, não o de informar o mime type errado ao server né? Você está usando Tomcat mesmo certo? (Glassfish já vem com as libs de jsf)

Edit: também existe o web.xml do seu tomcat, que informa os mime type padrão para cada extensão de arquivo. Dá uma olhada nele.

E

Eu estou usando JBoss

E

não lembro se o JBoss já tem nele as libs do jsf, tem que verificar.

Mas vc declarou os mime types no web.xml como eu disse anteriormente (eu dei exemplo, não listei todos os mime types para vc colocar lá)

H

Conseguiu resolver o problema?

T

Coloque a seguinte instrução no seu arquivo web.xml:

<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>

Criado 6 de novembro de 2010
Ultima resposta 17 de fev. de 2013
Respostas 49
Participantes 7