JSF 2 - hiding default values on <? extends Number>

懵懂的女人 提交于 2019-11-30 18:08:49

问题


is there a way to hide the default values of my number properties? I've got a int field and on my view the <h:inputText /> field shows a 0. In addition to this, if I leave this input empty I get a NullPointerException on this.

Can I hide the default value and treat empty inputs as default value?

I'm using mojarra 2.1.2 on Tomcat 7


回答1:


The primitive int always defaults to 0. You want to use Integer instead. E.g.:

public class Entity {

    private Integer value;

    // ...
}

As to keeping it null while submitting empty data, add the following context param to your web.xml:

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>

In addition, if you're using Tomcat or a clone which utilizes Apache EL parser, add the following server startup VM argument as well to prevent it from treating Number values like primitives:

-Dorg.apache.el.parser.COERCE_TO_ZERO=false

In Eclipse, you could set it in server configuration (doubleclick the server entry in Servers view) in the Arguments tab of the Open launch configuration dialogue. In production, you could add it to the JAVA_OPTS environment variable.



来源:https://stackoverflow.com/questions/7206401/jsf-2-hiding-default-values-on-extends-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!