开发者

Cannot store a number with f:convertNumber into a Map

开发者 https://www.devze.com 2023-03-01 12:10 出处:网络
I have a strange situation where i the f:convertNumber doesnt work when trying to store it to a untyped Map.

I have a strange situation where i the f:convertNumber doesnt work when trying to store it to a untyped Map.

I expect the number to be stored as a java.lang.Double object in the Map.


Here's the xhtml excerpt :

<h:inputText value="#{tInputBean.generalMap['myNumber']}" id="testNumber">
    <f:convertNumber />
</h:inputText>
<p:message for="testNumber" />

And here's the bean :

@Named("tInputBean")
@Scope("view")
public class TInputBean {
    private Log log = LogFactory.getLog(TInputBean.class);
    private Map generalMap = new HashMap();

    // .. and the setter getter
    ....
}

And here's my log :

DEBUG PhaseTracker - BEFORE PHASE RESTORE_VIEW 1
DEBUG PhaseTracker - AFTER PHASE RESTORE_VIEW 1
DEBUG PhaseTracker - BEFORE PHASE APPLY_REQUEST_VALUES 2
DEBUG DebugUtil - ============== start debugMap parameter map : ==============
DEBUG DebugUtil - testNumber='123123123'
DEBUG DebugUtil - javax.faces.partial.execute='SaveHeader testNumberPanel RPBPanel'
DEBUG DebugUtil - SaveHeader='SaveHeader'
DEBUG DebugUtil - javax.faces.partial.render='messages RPBPanel DebugVersionHeader testNumberPanel'
DEBUG DebugUtil - javax.faces.source='SaveHeader'
DEBUG DebugUtil - j_idt42='j_idt42'
DEBUG DebugUtil - javax.faces.ViewState='7088371747667351331:-3641627426454744246'
DEBUG DebugUtil - transNum='TRX003'
DEBUG DebugUtil - javax.faces.partial.ajax='true'
DEBUG DebugUtil - ============== end debugMap parameter map : ==============
DEBUG PhaseTracker - AFTER PHASE APPLY_REQUEST_VALUES 2
DEBUG PhaseTracker - BEFORE PHASE PROCESS_VALIDATIONS 3
DEBUG PhaseTracker - AFTER PHASE PROCESS_VALIDATIONS 3
DEBUG PhaseTracker - BEFORE PHASE RENDER_RESPONSE 6
DEBUG PhaseTracker - AFTER PHASE RENDER_RESPONSE 6

And after the render, <p:message for="testNumber" /> is replaced with : {0}: Conversion error occurred.


The things i notice is :

  1. There's no exception 开发者_JAVA百科happening while the conversion error occurs. Nothing is handled by my exceptionhandler which captures and log all exceptions. No exception trace.
  2. I cannot specify a type like java.lang.Double in the <f:convertNumber> tag.

Should i make my own converter for this to work ? Im already imagining things like :

<h:inputText value="#{tInputBean.generalMap['myNumber']}" id="testNumber">
    <f:converter type="MyDoubleConverter" />
</h:inputText>
<p:message for="testNumber" />

Please share your opinion on this matter. Thank you !


UPDATE


I've built a simple converter that suits my need, and so far it's been working ok. Here's the code, please share your opinion on this :

@FacesConverter(value="Double")
public class DoubleConverter implements Converter {
    private Log log = LogFactory.getLog(DoubleConverter.class);

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        //log.debug("locale : " + context.getViewRoot().getLocale());
        DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(context.getViewRoot().getLocale());
        //log.debug("VALUE == " + value);
        String result = value == null || value.toString().trim().length() == 0 ? null : format.format(value);
        //log.debug("RESULT == " + value);
        return result;
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        //log.debug("locale : " + context.getViewRoot().getLocale());
        //log.debug("STRING VALUE == " + value);
        DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(context.getViewRoot().getLocale());
        try {
            Object result = ((value == null) || (value.trim().length() == 0)) ? null : format.parse(value).doubleValue();
            //log.debug("RESULT == " + result);
            return result;
        } catch (Exception e) {
            log.error(e,e);
            throw new ConverterException(e);
        }
    }
}

And here's how my jsf make use of it :

<h:inputText value="#{tInputBean.generalMap['myNumber']}" 
   id="testNumber" converter="Double"/>
<p:message for="testNumber" />


You should try this:

<f:converter converterId="javax.faces.Double"/>

If you want to format you can use a converter for numbers:

<f:convertNumber type="number" maxIntegerDigits="3"/>

Details about number formatting you can find here and here.


Finally i made my own simple converter for this purpose, and the code has been posted at the bottom of the original post.

Thank you !

0

精彩评论

暂无评论...
验证码 换一张
取 消