вторник, 9 октября 2012 г.

JSF Tutorials: Using SEAM 3 Remoting


In this tutorial I am going to give an overview of Seam 3 Remoting module and give a simple example of how to use it.

Dependency:
Seam Conversation module

The basic idea behind Seam remoting module is to give a developer a tool to work with his Beans without using a JSF actionListeners or something else. Everything is made in background by AJAX. All you need is to execute a JavaScript Function like the following one:

Seam.createBean('myBean').myMethod(arguments,callbackFunction);

Basically you are creating an "instance" of your object on the client side and executing its methods via AJAX.

So lets see an example of how can we use Seam 3 Remoting:

1. Configuration

Put Seam libraries into your project we will definitely need seam-remoting.jar and seam-conversation.jar.

2. Server side:


import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import org.jboss.seam.remoting.annotations.WebRemote;
@Named
@RequestScoped
public class TestBean {     @WebMethod // Enables this method for use in client side     public String sayHello(String name) {         return "Hello, " + name;     } }

Nothing else is needed on server side.
2. Client side:
You have to add needed .JS libraries into your view:

<script type="text/javascript" src="seam/resource/remoting/resource/remote.js?compress=false"></script> // core remoting library
<script type="text/javascript" src="seam/resource/remoting/interface.js?testBean"></script>
// your Bean instance
Also you can also write interface.js?yourFirstBean&yourSecondBean
Next you can make use of Seam Remoting:
3. Usage:
function sayHello() {
  var name = prompt("Please enter your name:");
  Seam.createBean('myBean').myMethod(name,callbackFunction);
}
function callbackFunction(result) {
  alert(result);
}

Best regards,
NETLink community member

Комментариев нет:

Отправить комментарий