简体   繁体   English

如何使用 Thymeleaf 更改输入值?

[英]How to change input value with Thymeleaf?

I'm building Spring application and faced a problem.我正在构建 Spring 应用程序并遇到问题。 I got class @Entity Balance it has field private Long assets;我得到了 class @Entity Balance它有字段private Long assets; In html form I did this to connect this field and input block:在 html 表单中,我这样做是为了连接该字段和输入块:

<input type="text" th:field="${balance.assets}">

Assest field stores amount of money in thousands (like 123456 = 123 456 000$) Assest 字段以千为单位存储金额(如 123456 = 123 456 000$)

Every thing works fine, but now I need, to print and read input in millions but store in thousands.一切正常,但现在我需要打印和读取数百万但存储数千的输入。 For example user types 123,456 = 123 456 000$, and in Balance it should store 123456. And I really don't want to change assets field type from Long to Double .例如用户类型 123,456 = 123 456 000$,在Balance中它应该存储 123456。我真的不想将assets字段类型从Long更改为Double To print in millions I can just do this:要打印数百万,我可以这样做:

<span th:if="${balance.assets}" th:text="${#numbers.formatDecimal(balance.assets / 1000.0, 0, 'WHITESPACE', 3, 'DEFAULT')}"></span>

I can't figure out how to read it in millions.我无法弄清楚如何以数百万阅读它。

How can I make in html file?如何在 html 文件中制作?

Thanks谢谢

Thymeleaf is server-side template engine, it is used to generate html page on server before it is sent to client browser. Thymeleaf 是服务器端模板引擎,用于在服务器上生成 html 页面,然后再发送到客户端浏览器。 You can modify displayed value in thymeleaf, but one it is on client side there is no thymeleaf, there is only generated html page.您可以修改 thymeleaf 中的显示值,但其中之一是在客户端没有 thymeleaf,只有生成的 html 页面。 If you want to modify value after user submits a form you can do it only with:如果您想在用户提交表单后修改值,您只能使用:

  1. javascript - by on form submit event which will edit value befor sending form, javascript - 通过表单提交事件,它将在发送表单之前编辑值,
  2. javascript - by adding hidden field with values in thousands, and on change event to update it when visible input in milions is changed javascript - 通过添加值以千为单位的隐藏字段,并在更改事件时在更改以百万为单位的可见输入时更新它
  3. spring controller - transform value after form submission, you can add aditional requestparam @RequestParam(name = "amountInMillions") Double amountInMilions only to feed it in controller to your balance entity balance.assets = amountInMilions*1000 spring controller - transform value after form submission, you can add aditional requestparam @RequestParam(name = "amountInMillions") Double amountInMilions only to feed it in controller to your balance entity balance.assets = amountInMilions*1000

i'd suggest going with 3rd option as its less client dependent especially 1-st solution may cause issues if js would be disabled in client browser.我建议使用第 3 个选项,因为它对客户端的依赖性较小,尤其是第 1 个解决方案如果在客户端浏览器中禁用 js 可能会导致问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM