如何在Java中将参数值传递给Conversion类?

编程入门 行业动态 更新时间:2024-10-23 15:17:00
本文介绍了如何在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>移至)输入组件,然后使用 UIComponent#getAttributes() 即可获取.例如

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

使用

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

(其中component是validate()方法的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类?

本文发布于:2023-11-23 19:44:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622660.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   参数   如何在   Conversion   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!