How format number and put in input

2019-08-16 03:09发布

问题:

I have an input number and I must acept vlaue with 34,34 two decimal number so my code is:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ page isELIgnored="false"%>
    <input type="number" min="0" name="price" pattern="0.00"
                        step=".01" required
                        value="<fmt:formatNumber type = "number" 
                        pattern="0.00" value = "${home.price}" />"

The problem is that the inpput is empty is not loaded the value, because if I do

<label><fmt:formatNumber type = "number" 
                    pattern="0.00" value = "${home.price}</label>

the number is printed in the correct form. Anyone can help me?

回答1:

Use the var attribute on fmt:formatNumber to store the outcome and use it in your input, and make sure you are using a locale which provides numbers which the browser can work with:

<fmt:setLocale value="en"/>
<fmt:formatNumber type="number" 
                  pattern="0.00" value="${home.price}"
                  var="myNum"/>
<input type="number" min="0" name="price" pattern="0.00"
                    step=".01" required
                    value="${myNum}"/>

Especially the locale makes this quite ugly, so I really recommend to have a look at jsf.

See also:

  • Localization of input type number (JSP)
  • JSTL formatNumber for JSP custom pattern independent from language
  • Formatting a double in JSF


标签: jsp jstl