如何在Java中将参数值传递给Conversion类? [英] How do I pass a parameter value to a Conversion class in java?

查看:103
本文介绍了如何在Java中将参数值传递给Conversion类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将值传递给JSF/SEAM中的转换类

public class ValueConverter implements Converter {

public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
    if (StringUtils.isNotBlank(value)) {
    // logic etc here.

我的xhtml是:

<f:converter converterId="ValueConverter">
<f:attribute name="theMaxOrderSize" id="maxorder" value="#{_cartItem.item.maxOrderSize}"/>
</f:converter>

如何在Java中将参数值传递给Conversion类?我是从错误开始吗?我认为我正在使用JSF 1.2.

解决方案

Bhesh是完全正确的.您应该在 Validator 中进行验证工作. /p>

对于具体问题,将<f:attribute><f:converter>中移出(如果您正在听我们的话,则从<f:validator>移至)输入组件,然后使用

使用

Object theMaxOrderSize = component.getAttributes().get("theMaxOrderSize");
// ...

(其中componentvalidate()方法的UIComponent自变量,它表示父输入组件)

您可以将其强制转换为Integer#{_cartItem.item.maxOrderSize}表示的任何对象类型.

I am trying to pass a value to a conversion class in JSF/SEAM

public class ValueConverter implements Converter {

public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
    if (StringUtils.isNotBlank(value)) {
    // logic etc here.

My xhtml is:

<f:converter converterId="ValueConverter">
<f:attribute name="theMaxOrderSize" id="maxorder" value="#{_cartItem.item.maxOrderSize}"/>
</f:converter>

How do I pass a parameter value to a Conversion class in java? Am I starting off wrong? I am using JSF 1.2 I think..

解决方案

Bhesh is entirely right. You should be doing the validating job inside a Validator.

As to the concrete problem, move the <f:attribute> out of the <f:converter> (or <f:validator> if you're listening to us) into the input component and then use UIComponent#getAttributes() to obtain it. E.g.

<h:inputText ...>
    <f:validator validatorId="valueValidator" />
    <f:attribute name="theMaxOrderSize" id="maxorder" value="#{_cartItem.item.maxOrderSize}"/>
</h:inputText>

with

Object theMaxOrderSize = component.getAttributes().get("theMaxOrderSize");
// ...

(where component is the UIComponent argument of the validate() method, it represents the parent input component)

You can cast it to Integer or whatever object type the #{_cartItem.item.maxOrderSize} represents.

这篇关于如何在Java中将参数值传递给Conversion类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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