SeamRemote and @WebRemote
Think of the scenario that you develop a Seam based application and you want to do some AJAX calls / requests to your bean but you don’t want to use the Richfaces/A4J based AJAX functionality (maybe because you have some large forms and don’t want to post the complete form data). In such a case you can use SeamRemoting. There are some steps to perform before you can use SeamRemoting.
- Make sure that the jboss-seam-remoting.jar is in your classpath.
- Add
<servlet>
<servlet-name>Seam Resource Servlet</servlet-name>
<servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Seam Resource Servlet</servlet-name>
<url-pattern>/seam/resource/*</url-pattern>
</servlet-mapping>
to your web.xml. After that you can create a backing bean, eg something like this:
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.remoting.WebRemote;
import com.hp.ci.web.persis.action.BaseAction;
import com.hp.ci.web.persis.common.exception.BusinessException;
@Name("remoteTestClass")
public class RemoteTestClass extends BaseAction{
@WebRemote
public String testMethod() {
String[] array = {"Hello world", "Foobar.com", "John Doe was here", "Another random string"};
int randomIndex = (int)(Math.random() * array.length);
return array[randomIndex];
}
}
You simply expose the method via the @WebRemote annotation. To use this method now in your UI (e.g., a JSF site), you can include something like this:
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:rich="http://richfaces.org/rich"
xmlns:a4j="http://richfaces.org/a4j">
<ui:define name="errmessage">
<rich:messages globalOnly="true" errorClass="messageError"
fatalClass="messageFatal" infoClass="messageInfo" warnClass="messageWarn"/>
</ui:define>
...
<p/>
<h:panelGrid>
<h:panelGroup>
<h:outputText value="AJAX / Seam Remoting test"/>
<p/>
<s:remote include="remoteTestClass"/>
<a href="Javascript:callMe()">Invoke ajax method</a>
<s:div id="testDIV">
</s:div>
<script type="text/javascript">
//<![CDATA[
function callMe() {
Seam.Component.getInstance("remoteTestClass").testMethod(myCallBackM);
}
function myCallBackM(result) {
document.getElementById("testDIV").innerHTML=result;
};
//]]>
<script>
<hr/>
</h:panelGroup>
<h:panelGrid>
...
</ui:composition>
The <s:remote …> tag includes the needed Javascript files. Instead of this you can include the Javascript files in this way:
<script type="text/javascript" src="/TestApp/seam/resource/remoting/resource/remote.js"> </script>
<script type="text/javascript" src="/TestApp/seam/resource/remoting/interface.js?remoteTestClass">
To use more than one ‘remote class/backing bean’ method use the ‘request parameter notation’, e.g.: v