Showing posts with label extjs. Show all posts
Showing posts with label extjs. Show all posts

Friday, May 30, 2014

How to read JSON data (json request) from http request in a servlet or controller


If you use a javascript AJAX request which sends the http request in JSON data format and not in normal key value pair, you might need this.
One such example is the ExtJS store's, api config. For all the write operations the proxy for create/update/delete (use AJAX) sends the records in json form, in case the reader is defined as json.

Code:

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
  throws ServletException, IOException {
  
 PrintWriter out = response.getWriter();
 StringBuilder jsonString = new StringBuilder();
 
 String jsonToken = null;
 
 BufferedReader reader = request.getReader();
 while ((jsonToken = reader.readLine()) != null){
  jsonString.append(line);
 } 
 // converting the JSON String into a JSONObject, which almost work like a Map  
 try {
  JSONObject requestJsonObject = new JSONObject(jsonString); 
  // JSONObject from JSON Java API from json.org
  System.out.println(requestJsonObject.toString());
 }catch(JSONException jsone){
  jsone.printStackTrace();
 } 
  
 // further processing goes here....
} 

Interesting reads:

Tuesday, May 27, 2014

ExtJS: Panel Vs ViewPort

Working with ExtJS apps, we often get advice from the senior programmers that we should use ViewPort instead of Panel.
So, the question that we arise is when to use a ViewPort and when to use a Panel. Or, why to use ViewPort if Panel suffice the need.

Well, ViewPort is particularly useful in case you are looking to create a single web page application.
I think designers of ExtJS have put together all the essential components very nicely which allows you to develop Single Page WebApp quite elegantly.

Using viewport you can utilize availbale height and width of browser. You can use screen.availWidth and screen.availHeight to give you component proper dimension.

When I tried to do a little bit of search on google and forms, couple of interesting points came out which can help understanding answer when and why:
  1. There may be only one ViewPort for a page for obvious reason that it is specialized container which give you the entire view area of browser. But of course you can use a layout like border layout and add Panels as child component.So, in case you have a
  2. And, in cases where you do not have the liberty of using the entire view of the document/web page, use Panel instead. For example, in case your entire web page is using JQuery, but you want to keep a ExtJS grid component, you can do so using a grid panel.
     
The below forum links might useful to further reading:
The only tricky thing that I found with ViewPort is when you want to have the scrolling option, meaning the scrollbars should appear when you reduce the browser window size.
There is  autoScorll config, which is by default false. Although there implies that if you make it true it should show you the scrollbars. But, that does not happen often. The reason is something interesting to know.

If you take a look at the docs for ExtJS (4.x), it clearly states, "The Viewport does not provide scrolling, so child Panels within the Viewport should provide for scrolling if needed using the autoScroll config."


All this is little contradictory. But the problem gets solved once you provide the width/height or both for the child Panels. The scrollbar does appear after that.

So, in case you want to keep the scrolling, you need to take care of child Panels.
-----------------------------------------------------------------------------
  1. Please note the properties in EtxJS are case-sensitive, so "autscroll" won't work, it must be "autoScroll".
  2. screen.availWidth and screen.availHeight are provided by java script. You can alternatively use Ext.container.Viewport.getWidth() and Ext.container.Viewport.getHeight() as well.

Tuesday, October 2, 2012

ExtJs Tutorial #1: Hello World :)


Introduction:

I have already provided a brief introduction in my previous post. So, here we are going to setup the ExtJS API and walk through a simple example of hello world.

Pre-requisites:
  •         A browser (IE/Firefox/Chrome/Safari). Preferably, Chrome and Firefox, b'coz it comes with easy debugging tools like Chrome Developer Tools and Firebug, respectively.
  •         A web-server (If or When you are working with backend data and AJAX)
  •     ExtJS API. You can download it from http://www.sencha.com/products/extjs/download

 Typical Project Hierarchy:

 A typical project with extjs looks like below:



Please note that when you unzip the downloaded file, you will the find all the js files upfront, which are kept here in extJSLibt. I have organized it in a separate folder, which is obviously for better management of code base.

Also, the resources folder comes as a part of framework, which contains the necessary CSS files, images and theme related files etc. So, its important to include this folder as well.

Understanding the major js files and their uses:
When you unzip the Ext JS 4 download, you will see the following files:
1.      ext-debug.js - It provides the minimum number of core Ext JS classes needed to get up and running. This file is only for use during development. Any additional classes should be dynamically loaded as separate files using Ext.require:

For example, Ext.require('Ext.container.Viewport');
People who have worked with Dojo will be familiar with this approach.
2.      ext.js - same as ext-debug.js but minified for use in production.
3.      ext-all-debug.js - This file contains the entire ExtJS library. This can be helpful for shortening your initial learning curve, however ext-debug.js is preferred in most cases for actual application development.
This also helps you in debugging and resolving minor browser related issues or in case you want to do any changes in the default functioning of library. However, it is important to note, any changes you do should comply with the kind of license requirement/policy (http://www.sencha.com/products/extjs/license/) you have.
4.      ext-all.js - This is a minified version of ext-all-debug.js that can be used in production environments. However, it is not recommended since most applications will not make use of all the classes that it contains.
Instead, it is recommended that you create a custom build for your production environment, will cover this in a different post sometime later.
5.     bootstrap.js - This is the file which helps you to load the core js file (ext-all.js) depending upon the environment (local/dev/stage/prod) you are working on. Typically you won’t need it or need to change the code slightly as per your file names and need.


helloWorld.html:

<html>
<head>
    <title> My Application </title>

    <link rel="stylesheet" type="text/css" href="../extjs/ extJSLib/resources/css/ext-all.css">
    <script type="text/javascript" src="../extjs/ extJSLib/ext-all-debug.js"></script>
    <script type="text/javascript" src="../javascript/myApp.js"></script>
</head>
<body>
Home Page.
</body>
</html>


app.js:

Ext.onReady(function (){
      sayHello();
});

function sayHello(){
      Ext.Msg.alert('Hello World!!');

}


app.js explained:

Ext.onReady():

As per docs, Ext.onReady adds new listener to be executed when all required scripts are fully loaded. In other words, it is more like a function be executed on page laod.

Ext.Msg.alert():

As per docs, it displays a standard read-only message box with an OK button similar to the basic JavaScript alert prompt.


Monday, October 1, 2012

RUI (Rich User Interface): The buzz..and The Players!!


User Interface ‘has been’, ‘is’ and ‘will be’ one of the most important aspects of web application development. Now a day, for mobile app development as well. Also, it is one of most important criteria for ‘user/business acceptance’ as well as ‘end user delight’.

Well..a lot of pleasant sounding terms above, but, how do we accomplish this? The answer is RUI…or Rich User Interface.

Enterprises have been spending a lot in developing their own standards and APIs for RUI. However, underneath this, it’s the blend of CSS and some Java Script API.

Some of the popular RUI frameworks available in market are ExtJS, Jquery, Prototype, Flex, YUI and Dojo. Each of them has its own pros and cons. Hence, it’s important to evaluate and decide on one of them before you go ahead with actual development, as per your need. Also, keep in mind the learning curve involved with each of them.
  
One of my favourites is ExtJs. So, following this post, I am going to write more about ExtJS. For now, introducing ExtJS. Here it goes:


ExtJS (pronounced as E-X-T-J-S) is a Rich User Interface framework, provided by Sencha (http://www.sencha.com). And, this is one of most used UI frameworks in the market today.

Interestingly, it comes with both open source as well as commercial license.

It has many rich look components, which are quite extensible. And, plenty of powerful features such as AJAX, DOM Manipulation, Communication Support for REST, Good looking Charts (rendered using SVG).

Object Oriented (Class based) approach allows you to make your own custom component by extending existing components. Further, MVC based approach allows you to make your UI layer modular and extremely maintainable. What’s more? You are bound to get rid of your FAT ugly looking JSPs (or server side scripts).

Further details can be found at http://www.sencha.com/products/extjs/

Watch this space, for more on ExtJS. J



Prototype

Prototype is another creation pattern. Intent:  - Intent is to create objects by cloning existing instance  - Specify the kinds of obj...