이 콘텐츠는 선택한 언어로 제공되지 않습니다.

24.2. The Seam object


Client-side component interaction is performed with the Seam JavaScript object defined in remote.js. This is used to make asynchronous calls against your component. It is split into two areas of functionality: Seam.Component contains methods for working with components and Seam.Remoting contains methods for executing remote requests. The easiest way to become familiar with this object is to start with a simple example.

24.2.1. A Hello World example

Procedure 24.1. Hello World Example

  1. To show you how the Seam object works, we will first create a new Seam component called helloAction:
    @Stateless 
    @Name("helloAction") 
    public class HelloAction implements HelloLocal { 
        public String sayHello(String name) { 
            return "Hello, " + name; 
        } 
    }
    
    Copy to Clipboard Toggle word wrap
  2. We will also need to create a local interface for our new component. In particular, note the @WebRemote annotation, as this is required to make our method accessible via remoting:
    @Local 
    public interface HelloLocal { 
        @WebRemote 
        public String sayHello(String name); 
    }
    
    Copy to Clipboard Toggle word wrap
  3. This is all the server-side code we require. Next, create a new web page and import the helloAction component:
    <s:remote include="helloAction"/>
    Copy to Clipboard Toggle word wrap
  4. Add a button to the page to make this an interactive user experience:
    <button onclick="javascript:sayHello()">Say Hello</button>
    Copy to Clipboard Toggle word wrap
  5. You will also need script that performs an action when the button is clicked:
    <script type="text/javascript">
    function sayHello() {
         var name = prompt("What is your name?");
         Seam.Component.getInstance("helloAction").sayHello(name, sayHelloCallback);
    }
    
    function sayHelloCallback(result) {
        alert(result);
    }
    </script>
    Copy to Clipboard Toggle word wrap
  6. Now deploy your application and browse to your page. Click the button, and enter a name when prompted. A message box will display the "Hello" message, confirming the call's success. (You can find the full source code for this Hello World example in Seam's /examples/remoting/helloworld directory.)
You can see from the JavaScript code listing that we have implemented two methods. The first method prompts the user for their name, and makes a remote request. Look at the following line:
Seam.Component.getInstance("helloAction").sayHello(name, sayHelloCallback);
Copy to Clipboard Toggle word wrap
The first section of this line (Seam.Component.getInstance("helloAction")) returns a proxy, or stub, for our helloAction component. The remainder of the line (sayHello(name,sayHelloCallback);) invokes our component methods against the stub.
The whole line invokes the sayHello method of our component, passing in name as a parameter. The second parameter, sayHelloCallback, is not a parameter of our component's sayHello method — it tells the Seam Remoting framework that, once a response to the request is received, the response should be passed to the sayHelloCallback JavaScript method. (This callback parameter is optional; you can leave it out if you are calling a method with a void return type, or if the result of the request is not important.)
When the sayHelloCallback method receives the response to our remote request, it displays an alert message with the result of our method call.
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat