Tim

一枚野生程序员~

  • 主页
  • 分类
  • 标签
  • 归档
  • 关于
所有文章 工具

Tim

一枚野生程序员~

  • 主页
  • 分类
  • 标签
  • 归档
  • 关于

Apache Freemarker的一个小坑

阅读数:次 2020-02-21
字数统计: 801字   |   阅读时长≈ 4分

xxx.png
前段时间用到了Apache Freemarker来做前端页面,发现一个问题,就是JavaBean的Long类型和Integer类型向模板中注入的时候,只要是注入input类型,那么大
于999的数字则不会被显示,这个问题我也问了Apache官方的论坛:https://issues.apache.org/jira/browse/FREEMARKER-132 ,果然是官方站点呀,反馈还是很快的,系统还自动发了邮件!

给出的回复是在需要加上一个 ? c 于是我试了一下:

代码是这样的,非常简单的一个JavaBean

1
2
3
4
5
6
7
package xpu.tim.freemarker_bug;

@Data
public class Person {
private String name;
private Long age;
}

一个简单的Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package xpu.tim.freemarker_bug;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

@Controller
@RequestMapping("/")
public class IndexController {
@GetMapping
public ModelAndView index(Map<String, Object> map){
Person person = new Person("AAA", 1001L);
map.put("person", person);
return new ModelAndView("index", map);
}
}

一个index.ftl

1
2
<input value="${person.age}" type="number">
<input value="${person.name}" type="text">

在person.age <= 999 的时候一切正常,person.age > 999了就无法在输入框显示,通过查看页面源代码发现如果数字是1000的话:源代码内容是1,000 而不是1000,根据解决办法修改index.ftl为:

1
2
<input value="${person.age?c}" type="number">
<input value="${person.name}" type="text">

果然修改后一切正常,最后查阅官方文档得到如下结论:
https://freemarker.apache.org/docs/ref_builtins_number.html#ref_builtin_c
​

This built-in converts a number to string for a “computer language” as opposed to for human audience. That is, it formats with the rules that programming languages used to use, which is independent of all the locale and number format settings of FreeMarker. It always uses dot as decimal separator, and it never uses grouping separators (like 3,000,000), nor exponential form (like 5E20), nor superfluous leading or trailing 0-s (like 03 or 1.0), nor + sign (like +1). It will print at most 16 digits after the decimal dot, and thus numbers whose absolute value is less than 1E-16 will be shown as 0. This built-in is crucial because be default (like with ${x}) numbers are converted to strings with the locale (language, country) specific number formatting, which is for human readers (like 3000000 is possibly printed as 3,000,000). When the number is printed not for human audience (e.g., for a database record ID used as the part of an URL, or as invisible field value in a HTML form, or for printing CSS/JavaScript numerical literals) this built-in must be used to print the number (i.e., use ${x?c} instead of ${x}), or else the output will be possibly broken depending on the current number formatting settings and locale (like the decimal point is not dot, but comma in many countries) and the value of the number (like big numbers are possibly “damaged” by grouping separators).
If the incompatible_improvements FreeMarker configuration setting is set to 2.3.24 or higher (also if it’s set to 2.3.20 or higher and you are outside a string literal), this built-in will return “INF”, “-INF” and “NaN” for positive/negative infinity and IEEE floating point Not-a-Number, respectively. These are the XML Schema compatible representations of these special values. (Earlier it has returned what java.text.DecimalFormat did with US locale, none of which is understood by any (common) computer language.)

即在默认情况下,比如${x}数字被转换成具有地区语言、国家特定数字格式的字符串,这是针对我们人来说的,比如3000000可能打印为 3,000,000,但是显示成这样input标签就无法解析,所以还是需要显示成 3000000 就必须使用 ${x?c}这种形式!

赏

谢谢你请我喝咖啡

支付宝
微信
  • 本文作者: Tim
  • 本文链接: https://zouchanglin.cn/103657947.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 许可协议。转载请注明出处!
  • JavaWeb
  • 模板引擎
  • 前端
  • Web开发

扫一扫,分享到微信

Cgroups在Docker中的应用
Linux Cgroups的资源控制
目录,不存在的…
© 2017-2021 Tim
本站总访问量次 | 本站访客数人
  • 所有文章
  • 工具

tag:

  • 生活
  • Android
  • 索引
  • MySQL
  • 组件通信
  • Nginx
  • JavaSE
  • JUC
  • JavaWeb
  • 模板引擎
  • 前端
  • Linux
  • 计算机网络
  • Docker
  • C/C++
  • JVM
  • 上传下载
  • JavaEE
  • SpringCloud
  • Golang
  • Gradle
  • 网络安全
  • 非对称加密
  • IDEA
  • SpringBoot
  • Jenkins
  • 字符串
  • vim
  • 存储
  • 文件下载
  • Mac
  • Windows
  • NIO
  • RPC
  • 集群
  • 微服务
  • SSH
  • 配置中心
  • XML
  • Chrome
  • 压力测试
  • Git
  • 博客
  • 概率论
  • 排序算法
  • 分布式
  • 异常处理
  • 文件系统
  • 哈希
  • openCV
  • 栈
  • 回溯
  • SpringCore
  • 流媒体
  • rtmp
  • 面向对象
  • Vue
  • ElementUI
  • 软件工程
  • 异步
  • 自定义UI
  • ORM框架
  • 模块化
  • 交互式
  • Jsoup
  • Http Client
  • LRUCache
  • RabbitMQ
  • 消息通信
  • 服务解耦
  • 负载均衡
  • 权限
  • 多线程
  • 单例模式
  • Protobuf
  • 序列化
  • Python
  • m3u8
  • 堆
  • 二叉树
  • 自定义View
  • 观察者模式
  • 设计模式
  • 线程池
  • 动态扩容
  • 高可用
  • GC
  • ffmpeg
  • SpringMVC
  • REST
  • Redis
  • 缓存中间件
  • UML
  • Maven
  • Netty
  • 高性能网络
  • IPC通信
  • IO
  • Stream
  • 发布订阅
  • SQLite
  • Hash
  • 集合框架
  • 链表
  • Lambda
  • 汇编语言
  • 组件化
  • Router
  • 开发工具

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia-plus根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • 思维导图
  • PDF工具
  • 无损放大
  • 代码转图
  • HTTPS证书