为什么允许空格吗?
当我在
中设置 required="true"
时,它仍然允许空格。我一直在尝试修改 jsf-api.jar
但我无法理解如何生成新的 JAR,所以我尝试修改 isEmpty()
方法 < code>UIInput 类并编译,打开jsf-api.jar
并替换为新的,但没有起作用。
我需要的是当用户在
中写入时执行 trim()
以不允许空格。我怎样才能实现这个目标?
如果您想下载jsf-api.jar
资源,您可以这样做,只需阅读如何下载:http://javaserverfaces.java.net/checkout.html。
When I set required="true"
in a <h:inputText>
, it still allows blank spaces. I have been trying to modify the jsf-api.jar
but I could not understand how to generate new a JAR, so I tried to modify the isEmpty()
method from UIInput
class and compile it, open the jsf-api.jar
and replace it with the new one, but it did not work.
What I need is to do trim()
when the user writes in a <h:inputText>
to do not allow blank spaces. How can I achieve this?
If you want to download the jsf-api.jar
resource, you can do it, just read how to at: http://javaserverfaces.java.net/checkout.html.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是正常且自然的行为,并非 JSF 特有的行为。空格可能是完全有效的输入。
required="true"
仅在空输入中起作用,在填充输入中不起作用。然而,在 JSF 中,您只需为String
类创建一个Converter
即可自动修剪空格。将此类放在项目中的某个位置。它将通过
@FacesConverter
自动注册,并通过forClass=String.class
为每个String
条目自动调用。无需破解 JSF API/impl。这毫无意义。
That's normal and natural behaviour and not JSF specific. A blank space may be perfectly valid input. The
required="true"
only kicks in on empty inputs, not in filled inputs. In JSF you can however just create aConverter
forString
class to automatically trim the whitespace.Put this class somewhere in your project. It'll be registered automatically thanks to
@FacesConverter
and invoked automatically for everyString
entry thanks toforClass=String.class
.No need to hack the JSF API/impl. This makes no sense.
如果您想要关闭 BalusC 注释为标准 JSF 行为的答案之一的行为,您可以修改 web.xml 并包含以下内容。
这将触发 JSF 框架考虑可能更可取的值
null
,或者是 BalusC 答案的替代方案。If you want to turn off the behavior that BalusC notes as one of the answers as standard JSF behavior, you can modify the web.xml and include the following.
This will trigger the JSF framework to consider the values
null
which may be preferable, or an alternative to the answer from BalusC.