jsf中转换器的作用是什么? [英] What is the purpose of converters in jsf?

查看:85
本文介绍了jsf中转换器的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎转换器通常具有两个方法 getAsObject() getAsString(),这两种方法都会转换对象或从字符串。

It appears that a converter usually has two methods getAsObject() and getAsString(), which will convert Object into or from strings.

我们为什么需要这样做?

Why do we need to do this?

例如,在 dataTable 中,我们始终可以将对象与 var 。为什么我们不能在这里做类似的事情?

For example, in dataTable we can always bind an object with var. Why can't we do similar things here?

推荐答案

最后,客户端(通常是浏览器)将收到渲染的版本。通常是通过.xhtml文件构建的JSF组件树。

In the end the client (usually a browser) will receive a rendered version of your JSF Component tree, which you usually built via .xhtml files.

浏览器不了解 BigDecimals 日期。如果您在表单的输入字段中输入 1.003,浏览器应该如何知道它是 String 还是 float 还是服务器端的 BigDecimal ?它既不知道,也无法处理。因此,服务器和浏览器之间的通信始终是通过 Strings 进行的。呈现HTML时,将使用 getAsString()方法。在处理传输的表单输入时,JSF将使用 getAsObject()将其转换回实型。

The browser doesn't know about BigDecimals or Dates for example. If you write "1.003" in an input field of a form, how should the browser know whether it will be a String, or a float or a BigDecimal on server side? It cannot know that and also couldn't handle that. Thus the communication between server and browser is always via Strings. When the HTML gets rendered, the getAsString() method will be used. When processing the transmitted form inputs, JSF will use the getAsObject() to convert it back to the real type.

为什么 var 在dataTables中工作?因为在这里,您只会走一条路。 dataTable将仅显示内容,因此可以使用通用的 toString()方法。在这种情况下,无需转换为对象。

Why does var work in dataTables? Because here you will only go "one way". A dataTable will only display something and thus the generic toString() method can be used. A conversion to the Object isn't necessary in this case.

这篇关于jsf中转换器的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆