< f:viewParam&gt ;:静默忽略错误的参数 [英] <f:viewParam>: Ignore wrong parameters silently

查看:107
本文介绍了< f:viewParam&gt ;:静默忽略错误的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们刚刚开始将具有较新要求的旧jsf 1项目迁移到jsf 2.3版.目前,我正在使用viewparams的页面上工作.静默忽略错误的参数类型的正确方法是什么?

We just started to migrate an older jsf 1 project to jsf version 2.3 with new requirements. Currently I am working on pages that use viewparams. what would be the right way to silently ignore wrong parameter-types?

例如,具有页面 searchResults.xhtml 的定义:

for example with this definition for page searchResults.xhtml:

<f:metadata>
   <f:viewParam name="lastPrice" 
             value="#{ReportAgentBean.lastPrice}" converter="javax.faces.Double"/>

   <f:viewParam name="currentPrice" 
             value="#{ReportAgentBean.currentPrice}" converter="javax.faces.Double"/>

   <f:viewParam name="listPrice" 
             value="#{ReportAgentBean.listPrice}" converter="javax.faces.Double"/>
</f:metadata>

什么是最好的方法来忽略无效参数,或者如果有人发出如下请求,则将其作为null进行处理:

what would be the best approach to ignore invalid parameters or just handle them as null if someone fires a request like:

http://www.ourapp.com/reports/searchResults ?lastPrice = undefined?currentPrice = INVALIDSTRING

理想情况下会导致:

http://www.ourapp.com/reports/searchResults

感谢您的任何提示.

推荐答案

据我所知,您使用的默认Faces转换器无法接受/忽略传递给它的无效值并忽略它们.

As far as I know, the default faces converters you use cannot accept/ignore invalid values passed to it and ignore them.

为此,您需要创建自己的自定义转换器,在其中进行正确类型的转换,如果转换成功,则返回转换值,如果转换失败,则返回一些默认值.

For this to work, you need to create your own custom converters, do the casting to the right type in there and if the casting is successful, return the cast value and if it fails, return some default.

这是通常用于更复杂的参数/转换器组合的操作,其中仅存在id就会读取

This is what you'd normally do for more complex param/converter combinations where just the presence of an id would read a full complex object from somewhere

OmniFaces @Param 批注中也使用了整个概念.有趣的解决方案.如果转换失败,它们会将字段设置为null.也请参见该页面上的演示.

This whole concept is also used in the OmniFaces @Param annotation which might be an interesting solution to. They set the fields to null if conversion fails. See the demo on that page too.

这篇关于&lt; f:viewParam&gt ;:静默忽略错误的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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