问题描述
我最近遇到了一种情况,我想使用<h:inputHidden="#{v.index}/>
从ui:repeat varStatus="v"
设置索引值.如果尝试此操作,并使用AJAX execute
组件,则会出现该属性不可写的异常.环顾四周时,我发现inputHidden
支持readonly="true"
属性,该属性可以使错误消失.
I have recently encountered a case where I wanted to set the index value from ui:repeat varStatus="v"
using <h:inputHidden="#{v.index}/>
. If you try this and execute
the component using AJAX you will get an exception that the property is not writeable. While looking around I found that inputHidden
supports a readonly="true"
attribute that does just that making the error go away.
有没有为什么不在文档中的原因? (即特殊,直通,其他)使用安全吗?
Is there a reason why this is not in documentation? (i.e. special, passthrough, other)Is it safe to use?
推荐答案
进行一些挖掘后 Core JavaServer Faces 3e 要这样说:
After doing some digging Core JavaServer Faces 3e had this to say :
因此,为什么readonly
不在inputHidden
的文档中以及为什么未在HTML中呈现readonly
的原因,是因为inputHidden
不支持它.当我尝试将readonly
作为属性添加到inputHidden
'(带有错误消息的红色波浪线)时,Netbeans也确认了这一点.当我提交的表单中没有为放置在inputHidden
中的bean属性定义setter
的表单时,我能够得到该异常.由于inputHidden
在提交表单时(针对我的情况)会调用setter
,并且由于我的代码中未定义任何内容,因此它当然会抛出该异常,即:
So, the reason why readonly
is not in the docs for inputHidden
and also why it's not being rendered in your HTML is because inputHidden
does not support it. This was also confirmed by Netbeans when I tried to add readonly
as an attribute to inputHidden
'(red squiggly lines with an error message). I was able to get that exception when I submitted a form with no setter
defined for my bean property that was placed in inputHidden
. Since inputHidden
calls the setter
when a form is submitted (for my case) and since none is defined in my code, it will of course throw that exception, namely:
javax.el.PropertyNotWritableException: /index.xhtml @14,56 value="#{bean.x}": The class 'Bean' does not have a writable property 'x'.
至于为什么当您这样做时它仍然有效,我不知道.由于您担心安全性,因此建议您按照BalusC的说明进行操作,并只需使用<input type="hidden">
或为该属性定义setter
即可.不要那样用.
As for why it works when you do that I have no idea. Since you're worried about safety, I would suggest you do as BalusC says and simply use <input type="hidden">
or you define a setter
for that property. Don't use it like that.
这篇关于为什么JSF文档中没有InputHidden只读属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!