<%@page contentType="text/html" pageEncoding="ISO-8859-1"%>
<%@taglib tagdir="/WEB-INF/tags" prefix="st"%>
<%@taglib uri="/japano" prefix="jpn"%>
<st:page title="jspview example" onload="init();">
  <jsp:attribute name="head">
    <%--
    Import the japano.js library and instantiate the japano js object
    --%>
    <script type="text/javascript" src="${pageContext.request.contextPath}/japano.js"></script>
    <jpn:javascript>
      function getUser()
      {
      var url=japano.convertToURL('sample-js-jsUser.js');
      japano.get( url, function(result)
      {
      // get the marked div
      var e=document.getElementById('container');

      // check if the result is an http error
      if (japano.isError(result))
      {// yes -> show error
      e.innerHTML='<strong>'+result.code+'</strong><p>'+result.response;
      }
      else
      { // no -> dump user
      if (!result)
      result='no user logged in.';
      e.innerHTML='<strong>Current User</strong><p>'+japano.dump(result);
    }      
    });
    }
    
    var japano=null;
    
    function init()
    {
    japano=new Japano();
    }
    </jpn:javascript>
  </jsp:attribute>
  <jsp:body>
    <noscript>
      You have javascript disabled. This example only works with javascript.
    </noscript>
    <div class="box" id="container">
      <strong>Default Content</strong><p>
      This content will be dynamically replaced.      
    </div>      

    <%-- Define a button with a script that calls the server-side action and
    updates the container div. --%>
    <jpn:button fn="getUser">Get User data</jpn:button>
    &nbsp;|&nbsp;<a href="user-login.do?EMail=user@example.org&amp;password=password&amp;view=/jsview.jsp">Quick login</a>
  
    <div class="info">
      This example shows how to implement server-side actions which can be called from client-side javascript.
      It uses a cross-browser XMLHttpRequest javascript object to perform an <a href="/org/japanosample/action/JsUser.java.html">action</a>.
      When this action is performed it uses the javascript view feature to send an automatically generated javascript object of the current user to the client script.
      <p>
        The client-side javascript in the <a href="/jscall.jsp.html">current view</a> calls that <a href="/org/japanosample/action/JsUser.java.html" title="source code of the action">action</a> via a library method and
        receives the user object and dumps it to the content div. The user object is a javascript view of the result send by the action.
        The <a href="http://japano.sourceforge.net/api/org/japano/JsGenerator.html" title="javadoc of org.japano.JsGenerator">javascript view utility class</a> supports converting nested java object graphs
        with of the following type:
      </p>
      <ul>
        <li>Primitives and strings - Mapped directly to javascript objects.</li>
        <li>java beans - Mapped to associative arrays.</li>
        <li>Collections - Mapped to arrays or associative arrays.</li>    
      </ul>
      So the properties of the user bean end up as keys in an associative javascript object.
      ( e.g. the alias property of the user class is accessed with "result.alias" )
      The japano client javascript library support also sending text responses.
      <p>
      To have a working japano javascript-bridge you must follow these steps:
      </p>
      <ul>
        <li>You need to implement an <a href="/org/japanosample/action/JsUser.java.html" title="action used in this example">action</a> 
        which supplies the script with the data.</li>
        <li>The view must import the <a href="/japano.js">japano client-side javascript library</a> and instantiate the japano javascript object.</li>
        <li>The dynamic behaviour must be implemented in javascript. See the <a href="http://japano.sourceforge.net/jsapi.html">Japano javascript API</a> for details.</li>
      </ul>
      <b>Security note:</b> Be sure not to open access to data which should be kept secret. In a real world example, the
      propagation of the email address or even the password hash may be a risk, e.g.
      <p>
      Sourcecode:
      </p>
      <ul>
        <li><a href="org/japanosample/action/JsUser.java.html">Source Code of the JsUser action</a></li>
        <li><a href="jsview.jsp.html">Source Code of this view.</a></li>
        <li><a href="japano.js">Source code of the japano javascript library.</a></li>
        <li><a href="sample-js-jsUser.js">Source of the dynamic javascript object</a> (<a href="http://json.org">JSON format</a>).</li>
      </ul> 
    </div>
  </jsp:body>
</st:page>