PrettyFaces 与必需属性发生错误

2023-12-05

我正在使用开发一个网络应用程序JSF 2 and 漂亮脸蛋。我注释了我的一个@ViewScoped带有漂亮注释的bean。这就是我所拥有的:

@ManagedBean
@ViewScoped
@URLMapping(parentId = "app-list", id = "app-view", pattern = "/detail/#{appId}",
    viewId = "/system/manage_app/content/app_detail/app_detail.xhtml")
public class NavegableAppView extends SystemNavegable {

/**

基本上,它显示了我的系统中安装的应用程序的详细信息。这个 bean 可以通过两种方式实例化,传递#{appId}param,表示我要加载的应用程序的id,or如果没有该参数,在这种情况下,bean 将从@SessionScoped bean.

页面是这样的/system/manage_app/content/app_detail/app_detail.xhtml正在管理参数:

<ui:composition 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:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui"
template="/templates/general_template.xhtml">

<ui:define name="metadata">
    <f:metadata>
        <f:viewParam id="appId" name="appId"
            value="#{navegableAppView._ParamApp}" required="false" />
        <f:event type="preRenderView"
            listener="#{navegableAppView.initialize}" />
    </f:metadata>
</ui:define>

<ui:define name="general_content">
    <p:panel>
<!--More stuff-->

这里的问题是我想要NavegableAppView要创建的 bean,带或不带参数。我已经尝试过这种方式<p:button value="prueba" outcome="pretty:app-view" />这有效但限制我只能做outcome并且<p:commandButton value="prueba2" action="pretty:app-view" ajax="false" />,这相当于调用一个操作方法并返回导航案例(这就是我真正想要的).

第一个选择正确创建 bean 并从会话中加载值。第二种情况,给我这个错误:

 HTTP 500 - PrettyFaces: Exception occurred while building URL 
 for MappingId < app-view >, Required value < #{appId} > was null

所以我的目标 bean 没有被构建。我尝试将参数手动添加到导航案例中:return pretty:app-view?appId=1它可以工作,但我希望目标 bean 从会话本身中恢复它。我是否必须在操作方法中调用重定向或类似的方法?

汇集您的想法。


所以你实际上遇到了 PrettyFaces 的“边缘”效应之一。您定义的“appId”参数实际上既被视为参数名称,又被视为用于构建链接的 EL bean 值位置。

@URLMapping(parentId = "app-list", id = "app-view", pattern = "/detail/#{appId}",
viewId = "/system/manage_app/content/app_detail/app_detail.xhtml")

当您使用 PrettyFaces 的回发操作导航功能时,它需要 EL bean 名称。现在,碰巧的是,由于您还没有在您的@URLMapping注解,PrettyFaces无论如何都会尝试使用参数名称,并希望它能找到你想要的。在这种情况下,显然,没有名为的 bean 值#{appId}.

您实际上是在混合两种类型的功能来尝试解决同一问题。你的<f:metadata> and <f:viewParam>定义所做的事情与 PrettyFaces 的路径参数和操作方法相同。您应该使用其中一种机制。如果你真的想混合它们,那么正如你所说,你应该调用实际导航从你的<h:commandButton>,像这样:

<p:commandButton value="prueba2" action="#{navegableAppView.goToAppId}" ajax="false" />

然后,您需要确保返回一个有效的 JSF2 导航字符串,其中包含appId参数,例如:

public String goToAppId() {
    return "/system/manage_app/content/app_detail/app_detail.xhtml?faces-redirect=true&appId=" + appId";
}

PrettyFaces 然后会了解您正在重定向到已映射的 URL,并将对该 URL 执行出站重写,并将您发送到正确的、/detail/#{addId}反而。这是全部在文档中。

最后的选择只是删除<f:metadata> and <f:viewParam>,并使用内置 PrettyFaces 功能来管理视图参数。只需删除元数据并更新您的@URLMapping对此,可以解决您的问题:

@ManagedBean
@ViewScoped
@URLMapping(parentId = "app-list", id = "app-view", pattern = "/detail/#{appId : navegableAppView._ParamApp}",
viewId = "/system/manage_app/content/app_detail/app_detail.xhtml")
public class NavegableAppView extends SystemNavegable {

@URLAction
public String initialize() {
if ( appId != null ) {
      this.item = appsDB.findById(appId);
      return null;
    }

    // Add a message here, "The item {..} could not be found."
    return "pretty:app-list";
}

这就是使用 PrettyFaces 初始化页面的方式。归根结底,如果您正在使用pretty:mappingId导航,你应该only使用 PrettyFaces 功能。如果您打算使用普通的 JSF2 风格的导航,在其中指定 view-id 和 URL 的所有参数,那么您可以混合搭配(只要您使用namedURL 映射中的路径参数。

我希望这有帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PrettyFaces 与必需属性发生错误 的相关文章

随机推荐