具有必需属性的 PrettyFaces 错误 [英] PrettyFaces error with required attribute

查看:19
本文介绍了具有必需属性的 PrettyFaces 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JSF 2prettyfaces 开发一个 Web 应用程序.我用漂亮的注释注释了我的一个 @ViewScoped bean.这就是我所拥有的:

@ManagedBean@ViewScoped@URLMapping(parentId = "app-list", id = "app-view", pattern = "/detail/#{appId}",viewId = "/system/manage_app/content/app_detail/app_detail.xhtml")公共类 NavegableAppView 扩展 SystemNavegable {/**

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

页面/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"模板="/templates/general_template.xhtml"><ui:define name="元数据"><f:元数据><f:viewParam id="appId" name="appId"value="#{navegableAppView._ParamApp}" required="false"/><f:event type="preRenderView"listener="#{navegableAppView.initialize}"/></f:元数据></ui:define><ui:define name="general_content"><p:面板><!--更多内容-->

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

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

 HTTP 500 - PrettyFaces: 构建 URL 时发生异常对于 MappingId <app-view >, 所需值 <#{appId} >为空

所以我的目标 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 无论如何都会尝试使用参数名称,并希望它能找到您想要的.在这种情况下,显然没有名为 #{appId} 的 bean 值.

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

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

然后,您需要确保返回带有 appId 参数的有效 JSF2 导航字符串,例如:

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

PrettyFaces 将了解您正在重定向到已映射的 URL,并将对该 URL 执行出站重写,并将您发送到正确的 /detail/#{addId}.这是文档中的所有内容.p>

最后的选择是简单地删除 <f:metadata><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")公共类 NavegableAppView 扩展 SystemNavegable {@URLAction公共字符串初始化(){if ( appId != null ) {this.item = appsDB.findById(appId);返回空值;}//在此处添加一条消息,找不到项目 {..}."返回漂亮:应用程序列表";}

这是您使用 PrettyFaces 初始化页面的方式.归根结底,如果您使用 pretty:mappingId 导航,您应该使用 PrettyFaces 功能.如果您打算使用普通的 JSF2 样式导航,在其中指定视图 ID 和 URL 的所有参数,那么您可以混合匹配(只要您使用 named 路径-URL 映射中的参数.

我希望这会有所帮助.

I'm developing a web application using JSF 2 and prettyfaces. I annotated one of my @ViewScoped beans with pretty annotations. That's what I have:

@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 {

/**

Basically that shows the details of an application which is installed in my system. This bean can be instanced in two ways, passing #{appId} param, which indicates the id of the application which I want to load, or without that param, in this case the bean will recover this id from a @SessionScoped bean.

That's how the page /system/manage_app/content/app_detail/app_detail.xhtml is managing the parameter:

<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-->

The problem here is I want NavegableAppView bean to be created, with or without param. I have tried this way <p:button value="prueba" outcome="pretty:app-view" /> which works but limits me to do nothing more than outcome and also <p:commandButton value="prueba2" action="pretty:app-view" ajax="false" />, which is the equivalent to call an action method and return the navigation case (that's what really I want).

First choice creates the bean properly and loads the value from the session. Second case, is giving me this error:

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

So my target bean is not getting constructed. I have tried adding the parameter manually to the navigation case: return pretty:app-view?appId=1 and it works, but I want the target bean to recover it from the session itself. Do I have to call a redirect or something like that in my action method?

Pool your ideas.

解决方案

So you are actually running into one of the "edge" effects of PrettyFaces. The "appId" parameter you have defined is actually treated both a parameter name, and also an EL bean value location for building links.

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

When you use the postback-action navigation functionality of PrettyFaces, it requires an EL bean name. Now, it just so happens that since you have not provided one in your @URLMapping annotation, that PrettyFaces is going to try to use the parameter name anyway, and hope that it can find what you want. In this case, obviously, there is no bean value called #{appId}.

You're really mixing two types of functionality that attempt to solve the same problem. Your <f:metadata> and <f:viewParam> definitions are doing the same thing that PrettyFaces does with its path-parameters and action methods. You should use one or the other mechanism. If you really want to mix them, then as you said, you should be invoking actual navigation from your <h:commandButton>, like so:

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

Then, you'll need to make sure you return a valid JSF2 navigation string WITH the appId parameter, such as:

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

PrettyFaces will then understand that you are redirecting to a URL that has been mapped, and will perform outbound rewriting on the URL, and send you to the proper, /detail/#{addId} instead. This is all in the docs.

The final option would simply to remove the <f:metadata> and <f:viewParam>, and use the built-in PrettyFaces functionality for managing view parameters instead. Simply removing the metadata and updating your @URLMapping to this, would solve your problem:

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

This is how you initialize pages using PrettyFaces. When it comes down to it, if you are using pretty:mappingId navigation, you should only use PrettyFaces features. If you are going to use normal JSF2-style navigation where you specify the view-id and all the parameters of the URL, then you can mix-and match (as long as you are using named path-parameters in your URL mappings.

I hope this helps.

这篇关于具有必需属性的 PrettyFaces 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆