Art*_*ald 8 jsf binding components seam
在继续之前,请看这个问题
它的JSF表单再次显示如下:
<f:view>
<h:form>
<div>
<label>Id</label>
<input type="text" name="accountId"/>
</div>
<div>
<label>Amount</label>
<input type="text" name="amount"/>
</div>
<h:commandButton value="Withdraw" action="#{accountService.withdraw(param.accountId, param.amount)}"/>
</h:form>
</f:view>
Run Code Online (Sandbox Code Playgroud)
请注意,我使用的是<input type="text" name="amount">
代替<h:inputText id="amount">
.为了使用Seam EL解析器检索其值,我使用param.amount
.
碰巧的是,如果我<input type="text"
在服务器端使用并出现问题,我需要再次显示该页面.因此,它的提交值未被检索,因为它是一个普通的HTML代码.因此,我需要使用<h:inputText
JSF组件.
所以问题是:如何使用表达式语言检索<h:inputText
JSF组件值?
Bal*_*usC 18
该JSF客户机ID的由母体的cliend ID前置UINamingContainer
部件(例如h:form
,h:dataTable
,f:subview
).如果您在webbrowser中检查生成的HTML源代码(右键单击,查看源代码),那么您应该看到它们.的id
和name
所产生的输入元件被预先考虑与id
母体形式的.您需要在参数映射中使用与键相同的名称.作为分隔符,冒号:
是EL中的"非法"字符,您需要使用括号表示法param['foo:bar']
来检索它们.
<f:view>
<h:form id="account">
<div>
<label>Id</label>
<h:inputText id="id" />
</div>
<div>
<label>Amount</label>
<h:inputText id="amount" />
</div>
<h:commandButton value="Withdraw"
action="#{accountService.withdraw(param['account:id'], param['account:amount'])}"/>
</h:form>
</f:view>
Run Code Online (Sandbox Code Playgroud)
如果没有类似Seam-EL的方法参数(您显然不希望/拥有它),您还可以使用客户端ID作为键在请求参数映射中访问它们:
public void withDraw() {
Map<String, String> map = FacesContext.getCurrentInstance().getRequestParameterMap();
String id = map.get("account:id");
String amount = map.get("account:amount");
// ...
}
Run Code Online (Sandbox Code Playgroud)
不用说,这是令人讨厌的.只需按照正常的JSF方式执行,使用bean属性绑定值.
编辑:根据您编辑的最后一个问题:
所以问题是:如何
<h:inputText
使用表达式语言检索JSF组件值?
这已经得到了回答.使用JSF生成的名称作为参数名称.这通常是在的图案formId:inputId
,其中formId
是id
父的UIForm
分量和inputId
为id
所述的UIInput
组分.检查生成的HTML输出以获取生成<input type="text">
字段的确切名称.要获取参数值,请使用括号表示法['name']
,因为您不能:
在EL中使用冒号${param.formId:inputId}
.
从而:
#{param['formId:inputId']}
Run Code Online (Sandbox Code Playgroud)