我在我的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>
你暗示,它正常工作与JSF托管bean。但你没有明确证实你的问题。请澄清。另外,您的bean实际上是会话作用域而不是对话作用域。这很混乱。 – BalusC
对不起,你是对的。我更改为@ SessionScoped来检查它是否是@ ConversationScopes特有的问题,但它也不起作用。我会纠正我的问题。 –
好的,总结一下,无论bean管理框架如何,也不管bean的作用域如何,操作方法在任何情况下都可以正确调用,但导航结果的返回字符串会被完全忽略,因为它会返回到同一页面? – BalusC