隐式导航不起作用

时间:2015-08-21 07:02:46

标签: jsf jsf-2 primefaces navigation

我的JSF应用程序中有一个@ConversationScoped CDI Managed Bean,使用在Wildfly 8.2.1上运行的Primefaces 5.2。隐式导航只在我的应用程序中运行一次。首先,我有我的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;
    }

}

最后,我调用我的方法cargar()的xhtml。我想调用该方法,然后加载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()并执行其中的代码,但它会忽略返回&#34; ciudades / detalle&#34; 。因此,隐式导航第一次工作(当我在index.xhtml上,我点击按钮,它会带我到lista.xhtml)但是当我点击一个链接转到detalle.xhtml时,它会忽略它。它只是刷新lista.xhtml。此外,我尝试使用@SessionScoped和@RequestScoped(总是使用javax.enterprise.context注释代替JSF注释,并删除Conversation对象和初始化)。

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

使用@RequestScoped如果我单击commandLink,它会刷新页面但不会调用action方法。如果我在数据表之外放置一个伪commandLink,它会调用action方法但是再次忽略String返回。

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

编辑:我也将注释更改为 import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; 检查它是否与CDI注释有关,但它也没有用,所以它不是CDI的问题。我是否必须激活配置中的某些内容才能使其正常工作?这是在Wildfly 8.2.1上运行的Eclipse Dynamic Web Project 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>

2 个答案:

答案 0 :(得分:2)

您正在使用相对导航。试试

return "detalle";

而不是

return "ciudades/detalle";

或者改为使用绝对导航:

return "/ciudades/detalle";

答案 1 :(得分:0)

最后,我试了一下:我还没有设置JSF参数&#34; PROJECT_STAGE&#34;在我的web.xml文件中,所以我将其设置为开发模式:

<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";
    }