JSF 2.0 + Ajax hello world example
In JSF 2.0,coding Ajax is just like coding a normal HTML tag,it’s extremely easy. In this tutorial,you will restructure the last JSF 2.0 hello world example,so that,when the button is clicked,it will make an Ajax request instead of submitting the whole form. 1. JSF 2.0 PageA JSF 2.0 xhtml page with Ajax support. helloAjax.xhtml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
<h:body>
<div><div class="ads-in-post hide_if_width_less_800">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 728x90 - After1stH4 -->
<ins class="adsbygoogle hide_if_width_less_800" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-2836379775501347" data-ad-slot="7391621200" data-ad-region="mkyongregion"></ins>
<script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>
</div></div><h2>JSF 2.0 + Ajax Hello World Example</h2>
<h:form>
<h:inputText id="name" value="#{helloBean.name}"></h:inputText>
<h:commandButton value="Welcome Me">
<f:ajax execute="name" render="output" />
</h:commandButton>
<div><div class="ads-in-post hide_if_width_less_800">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 728x90 - After2ndH4 -->
<ins class="adsbygoogle hide_if_width_less_800" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-2836379775501347" data-ad-slot="3642936086" data-ad-region="mkyongregion"></ins>
<script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>
</div></div><h2><h:outputText id="output" value="#{helloBean.sayWelcome}" /></h2>
</h:form>
</h:body>
</html>
In this example,it make the button <h:commandButton value="Welcome Me">
<f:ajax execute="name" render="output" />
</h:commandButton>
<h:outputText id="output" value="#{helloBean.sayWelcome}" />
In the
2. ManagedBeanSee the full HelloBean.javapackage com.mkyong.common;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;
@ManagedBean
@SessionScoped
public class HelloBean implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSayWelcome(){
//check if null?
if("".equals(name) || name ==null){
return "";
}else{
return "Ajax message : Welcome " + name;
}
}
}
3. How it work?Access the URL : When the button is clicked,it makes an Ajax request and pass the text box value to the server for processing. After that,it refresh the (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- oracle分析函数over partition by 和group by的区别
- 在C中帮助do / while和switch语句
- react native:Command /bin/sh failed with exit code 1 报
- reactjs – React组件中的意外标记’=’
- c# – 将System.Windows.Forms.Label转换为自定义类型
- NoSQL数据库MongoDB的日常使用小结(一)
- xcode – Keychain adhoc发行版
- XML中的<![CDATA []]>是什么意思?
- Ben Forta:网络浏览器和Flash的分界线在变化 (1)
- ruby-on-rails – 如何使用Yard来记录rails枚举类型