在JSF中仅忽略无效的查询参数 [英] Ignoring only invalid query params in JSF
问题描述
我正在使用f:viewparam
将GET请求参数捕获到JSF页面.
I am using f:viewparam
to capture GET request params to a JSF page.
<f:metadata>
<f:viewParam name="id" value="#{mediaGroupController.mediaGroupId}"/>
<f:viewParam name="type" value="#{mediaGroupController.type}"/>
<f:viewParam name="limit" value="#{mediaGroupController.limit}" converter="javax.faces.Integer" >
<f:validateLongRange minimum="0"/>
</f:viewParam>
</f:metadata>
如果我输入所有有效参数(例如?id=1&type=foo&limit=10
),则此方法正常.但是,如果其中一个参数无效,例如?id=1&type=foo&limit=bar
,则有效参数(例如id和type)也将被忽略.
This works OK if I enter valid parameters for all, like ?id=1&type=foo&limit=10
. However, if one of the params is invalid, for example, ?id=1&type=foo&limit=bar
, valid params like id and type are also ignored.
在这种情况下,我只想忽略limit
参数.我该如何实现?
I would like to only ignore the limit
parameter in such a case. How can I achieve that?
推荐答案
创建一个自定义转换器,该转换器不抛出ConverterException
,但是在转换失败时仅返回null
.
Create a custom converter which don't throw a ConverterException
, but just returns null
when the conversion fails.
例如
<f:viewParam name="limit" value="#{mediaGroupController.limit}" converter="limitConverter">
使用
@FacesConverter("limitConverter")
public class LimitConverter {
@Override
public Object getAsString(FacesContext context, UIComponent component, Object value) {
return (value != null) ? String.valueOf(value) : null;
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return (value != null && value.matches("\\d+") ? Integer.valueOf(value) : null;
}
}
这篇关于在JSF中仅忽略无效的查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!