我可以将输入文本字段值传递给bean方法而不将值绑定到bean属性吗?
<h:inputText value="#{myBean.myProperty}" />
<h:commandButton value="Test" action="#{myBean.execute()} />
我可以不在#{myBean.myProperty}
进行临时保存吗?
答案 0 :(得分:49)
将组件UIInput
绑定到视图,并使用UIInput#getValue()
将其值作为方法参数传递。
<h:inputText binding="#{input1}" />
<h:commandButton value="Test" action="#{myBean.execute(input1.value)}" />
与
public void execute(String value) {
// ...
}
请注意,这种方式已经转换并验证了通常的JSF方式。
答案 1 :(得分:14)
您可以通过获取请求并使用普通Java EE ServletRequest#getParameter来恢复表单的参数。使用此方法时,请记住设置组件的ID和名称:
<h:form id="myForm">
<h:inputText id="txtProperty" /> <!-- no binding here -->
<input type="text" id="txtAnotherProperty" name="txtAnotherProperty" />
<h:commandButton value="Test" action="#{myBean.execute()} />
</h:form>
Managed Bean:
@ManagedBean
@RequestScoped
public class MyBean {
public void execute() {
HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
String txtProperty = request.getParameter("myForm:txtProperty");
//note the difference when getting the parameter
String txtAnotherProperty= request.getParameter("txtAnotherProperty");
//use the value in txtProperty as you want...
//Note: don't use System.out.println in production, use a logger instead
System.out.println(txtProperty);
System.out.println(txtAnotherProperty);
}
}
另一个有更多信息的帖子: