I try to AJAXify a simple commandButton in order to send AJAX request without refreshing the whole page. My xhtml file includes the following code:
<?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:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Test</title>
</h:head>
<h:body>
<h:form id="form">
<h:inputText id="name" value="#{test.name}"></h:inputText>
<h:commandButton value="Welcome Me">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
<h2>
<h:outputText id="output" value="#{test.sayWelcome}" />
</h2>
</h:form>
</h:body>
</html>
My backing bean is the following:
import java.io.Serializable;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;
@ViewScoped
@Named("test")
public class TestBackingBean 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;
}
}
}
However, when I click on commandButton the form is submitted and the whole page is refreshed.
I would like to avoid using additional JSF frameworks.
Thank you in advance.