2015-08-21 35 views
0

我在我的JSF应用程序中使用了在Wildfly 8.2.1上运行的Primefaces 5.2的@ConversationScoped CDI托管Bean。隐式导航只在我的应用程序中工作过一次。首先,我有我的index.xhtml其中有一个按钮,叫我管理的bean,CiudadManagedBean:隐式导航不起作用

<!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" 
     xmlns:p="http://primefaces.org/ui"> 

<ui:composition template="/plantilla/template.xhtml"> 

    <ui:define name="contenido"> 
    <h:form> 
     <p:commandButton action="#{ciudadMB.cargarCiudades()}" 
     value="Mostrar ciudades" /> 
    </h:form> 
    </ui:define> 
</ui:composition> 
</html> 

接下来,这里是CiudadesManagedBean。正如你所看到的,我开始@PostConstruct方法的对话,它加载时,我打电话从的index.xhtml cargarCiudades列表,最后它加载ciudades/lista.xhtml:

package com.saplic.fut.beans; 

import java.io.Serializable; 
import java.util.List; 

import javax.annotation.PostConstruct; 
import javax.enterprise.context.Conversation; 
import javax.enterprise.context.ConversationScoped; 
import javax.inject.Inject; 
import javax.inject.Named; 

import com.saplic.fut.daos.CiudadDAO; 
import com.saplic.fut.entity.Ciudad; 

@Named(value="ciudadMB") 
@ConversationScoped 
public class CiudadManagedBean implements Serializable { 

    private static final long serialVersionUID = 7339176875158769385L; 

    private Ciudad instance; 
    private List<Ciudad> cities; 
    private Integer idCity; 

    @Inject 
    private CiudadDAO ciudadDAO; 

    @Inject 
    private Conversation conversation; 

    @PostConstruct 
    public void inicializarConversacion() { 
     if(conversation.isTransient()) 
      conversation.begin(); 
    } 

    public Ciudad getInstance() { 
     return instance; 
    } 

    public String cargarCiudades() { 
     setCities(ciudadDAO.cargarCiudades()); 
     return "ciudades/lista"; 
    } 

    public String cargar() { 
     Ciudad flt = new Ciudad(); 
     flt.setId(getIdCity()); 
     setInstance(ciudadDAO.cargarDetalle(flt)); 
     if(getInstance() == null) 
      setInstance(new Ciudad()); 
     return "ciudades/detalle"; 
    } 

    public String guardar() { 
     ciudadDAO.guardar(getInstance()); 
     setCities(ciudadDAO.cargarCiudades()); 
     return "ciudades/lista"; 
    } 

    public String actualizar() { 
     ciudadDAO.actualizar(getInstance()); 
     setCities(ciudadDAO.cargarCiudades()); 
     return "ciudades/lista"; 
    } 

    public String eliminar() { 
     ciudadDAO.guardar(getInstance()); 
     setCities(ciudadDAO.cargarCiudades()); 
     return "ciudades/lista"; 
    } 

    public void setInstance(Ciudad instance) { 
     this.instance = instance; 
    } 

    public List<Ciudad> getCities() { 
     return cities; 
    } 

    public void setCities(List<Ciudad> cities) { 
     this.cities = cities; 
    } 

    public Integer getIdCity() { 
     return idCity; 
    } 

    public void setIdCity(Integer idCity) { 
     this.idCity = idCity; 
    } 

} 

最后,XHTML其中i打电话给我的方法cargar()。我想调用该方法,然后加载ciudades/detalle.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:ui="http://java.sun.com/jsf/facelets" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:p="http://primefaces.org/ui"> 

<ui:composition template="/plantilla/template.xhtml"> 

    <ui:define name="contenido"> 
    <h:form id="frm1"> 

     <p:dataTable id="tablita" resizableColumns="true" rowIndexVar="rowIdx" 
     paginator="true" rows="10" paginatorPosition="bottom" value="#{ciudadMB.cities}" 
     var="ciu" > 
      <p:column headerText="Código pais"> 
       <p:outputLabel value="#{ciu.codigoPais}" /> 
      </p:column> 
      <p:column headerText="Distrito"> 
       <p:outputLabel value="#{ciu.distrito}" /> 
      </p:column> 
      <p:column headerText="Nombre"> 
       <p:outputLabel value="#{ciu.nombre}" /> 
      </p:column> 
      <p:column headerText="Población"> 
       <p:outputLabel value="#{ciu.poblacion}" /> 
      </p:column> 
      <p:column headerText="Accion"> 
       <p:commandLink action="#{ciudadMB.cargar()}" ajax="false" value="Ver" process="@form"> 
        <f:setPropertyActionListener value="#{ciu.id}" target="#{ciudadMB.idCity}" /> 
       </p:commandLink> 
      </p:column> 
     </p:dataTable> 
    </h:form> 
    </ui:define> 
</ui:composition> 
</html> 

,当我点击我的数据表中的链接,它会调用ciudadMB.cargar()并执行它里面的代码,但它忽略返回“ciudades/detalle”。因此,隐式导航第一次工作时(当我在index.xhtml,我点击按钮,它需要我lista.xhtml),但是当我点击一个链接去detalle.xhtml,它忽略它。它只是刷新lista.xhtml。另外,我已经使用@SessionScoped和@RequestScoped(总是使用javax.enterprise.context注释来代替JSF注释,并删除对话对象和初始化)。

使用@SessionScoped它具有相同的行为。分页工作没有问题,但是当我点击数据表中的commandLink时,它会调用操作方法,但忽略字符串返回。

使用@RequestScoped如果我单击commandLink,它会刷新页面但不会调用操作方法。如果我在数据表外部放置了一个虚拟commandLink,它会调用操作方法,但又会忽略字符串返回。

为什么隐式导航不起作用?是否有一些CDI问题?问候。

编辑:我也改变了注释到 import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; 来检查它是否与CDI注释有关,但它也不起作用,所以它不是CDI的问题。我必须在我的配置中激活某些内容才能使其正常工作吗?这是在Wildfly 8.2.1上运行的Eclipse动态Web项目3.1,JPA 2.1和JSF 2.2。这里是我的web.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 
    <display-name>SistemaFutJsf</display-name> 
    <welcome-file-list> 
    <welcome-file>index.xhtml</welcome-file> 
    </welcome-file-list> 
    <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>/</url-pattern> 
    <url-pattern>*.xhtml</url-pattern> 
    </servlet-mapping> 
    <context-param> 
    <param-name>primefaces.THEME</param-name> 
    <param-value>cupertino</param-value> 
    </context-param> 
</web-app> 

faces-config.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<faces-config 
    xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" 
    version="2.2"> 

</faces-config> 

和CDI的beans.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" 
     version="1.1" bean-discovery-mode="annotated"> 

</beans> 
+0

你暗示,它正常工作与JSF托管bean。但你没有明确证实你的问题。请澄清。另外,您的bean实际上是会话作用域而不是对话作用域。这很混乱。 – BalusC

+0

对不起,你是对的。我更改为@ SessionScoped来检查它是否是@ ConversationScopes特有的问题,但它也不起作用。我会纠正我的问题。 –

+0

好的,总结一下,无论bean管理框架如何,也不管bean的作用域如何,操作方法在任何情况下都可以正确调用,但导航结果的返回字符串会被完全忽略,因为它会返回到同一页面? – BalusC

回答

2

您正在使用相对导航。与

return "detalle";

,而不是

return "ciudades/detalle";

一次或使用绝对导航来代替:

return "/ciudades/detalle";

+0

但是这意味着我无法将我的xhtml归类到文件夹中,例如,如果我有/ ciudades/detalle和/ paises/detalle会发生什么情况? –

+0

@BalusC你可以请这个问题进行调解吗? 奥斯卡卡尔德隆,我给你解决你的问题。你根据一个附加条件拒绝它,无论如何我在最后一行覆盖了这个必要条件“或者使用绝对导航。” – lametaweb

+0

对不起@lametaweb,当你谈到关于添加斜杠的“绝对导航”时,我没有那么明白。 –

0

最后,我想的东西:我还没有设置JSF参数“我的web.xml文件”PROJECT_STAGE“,所以我把它设置在开发模式:

<context-param> 
     <param-name>javax.faces.PROJECT_STAGE</param-name> 
     <param-value>Development</param-value> 
</context-param> 

在这之后,我又试过,我得到这个错误:

09:58:58,487 WARNING [javax.enterprise.resource.webcontainer.jsf.application] (default task-21) JSF1064: Unable to find or serve resource, /ciudades/ciudades/detalle.xhtml. 

的问题是,我使用的是相对路径。第一次,从index.xhtml移动到ciudades/lista.xhtml,它工作没有问题,因为我在根路径(http:localhost:8094/SistemaFutJsf),但之后,当我试图去ciudades/detalle.xhtml,应用程序上下文是这个(http:localhost:8094/SistemaFutJsf/ciudades),因为它找不到/ciudades/ciudades/detalle.xhtml。

所以,解决办法是增加一个斜杠(/),使路径绝对的,就像这样:

public String cargarCiudades() { 
     setCities(ciudadDAO.cargarCiudades()); 
     return "/ciudades/lista"; 
    } 

    public String cargar() { 
     Ciudad flt = new Ciudad(); 
     flt.setId(getIdCity()); 
     setInstance(ciudadDAO.cargarDetalle(flt)); 
     if(getInstance() == null) 
      setInstance(new Ciudad()); 
     return "/ciudades/detalle"; 
    }