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

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

问题描述

我正在使用 JSF 2 prettyfaces 开发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")
public class NavegableAppView extends SystemNavegable {

/**

基本上显示了我的系统中安装的应用程序的详细信息.该bean可以通过两种方式进行实例化,传递#{appId}参数,该参数指示要加载的应用程序的ID,,不带该参数,在这种情况下,bean将从该ID中恢复该ID.一个@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" />的方法,该方法可以工作,但仅限于结果<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仍将尝试使用参数名称,并希望它可以找到您想要的内容.显然,在这种情况下,没有名为#{appId}的bean值.

您实际上是在混合两种试图解决同一问题的功能.您的<f:metadata><f:viewParam>定义的作用与PrettyFaces的路径参数和操作方法相同.您应该使用一种或另一种机制.如果您真的想将它们混合在一起,那么就像您所说的,您应该是

然后,您需要确保使用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}.这是所有文档中的全部.

最后一个选项只是删除<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")
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样式的导航,在其中指定视图ID和URL的所有参数,则可以进行混合匹配(只要使用的是 named path- 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天全站免登陆