1

我有一个接受名称的输入文本框,以相反的顺序处理名称,然后将其输出到另一个文本框。每当我输入值并单击页面上的任意位置(意味着从文本框失去焦点)时,输出文本框将自动更新。

当我打开源代码时,我发现了类似下面的代码,我可以知道 ajax 对 inputtext 组件有什么作用吗?

<h:inputText id="name" value="#{helloBean.name}">
  <f:ajax render="printMyName"/>
</h:inputText>

<h:outputText id="printMyName" value="#{helloBean.reverseName}"/>
4

1 回答 1

5

Taken from Learning JSF2: Ajax in JSF – using f:ajax tag

Sending an Ajax request

JSF comes with one tag to send an Ajax request, the tag is called f:ajax. This tag is actually a client side behavior. Being a behavior implies it’s never just used by itself on a page, it is always added as a child tag (behavior) to another UI component (or can even wrap several components). Let’s use a small echo application to demonstrate usage of this tag.

<h:form> 
  <h:panelGrid> 
      <h:inputText value="#{bean.text}" > 
         <f:ajax event="keyup" render="text"/> 
      </h:inputText> 
      <h:outputText id="text" value="#{bean.text}" /> 
   </h:panelGrid> 
</h:form>

Code snippet above takes care of firing an Ajax request based on onkeyup event. Notice the actual event name is keyup. This takes care of firing an Ajax request. Next we need to figure out how to do partial view rendering.

Attribute Description event:

String on which event Ajax request will be fired. If not specified, a default behavior based on parent component will be applied. The default event is action for ActionSource (ie: button) components and valueChange for EditableValueHolder components (ie: input). action and valueChange are actual String values that can be applied applied event attribute.

于 2012-11-04T10:04:31.360 回答