Spring MVC WebApplicationContext Hierarchy

In Spring MVC, the DispatcherServlet is the central servlet that receives requests and dispatches them to the appropriate controllers. In a Spring MVC application, there can be any number of DispatcherServlets for various purposes (for example, handling user interface requests, RESTful-WS requests, and so on), and each DispatcherServlet has its own WebApplicationContext configuration, which defines the servlet-level characteristics, such as controllers supporting the servlet, handler mapping, view resolving, i18n, theming, validation, type conversion and formatting, and so on. Underneath the servlet-level WebApplicationContext configurations, Spring MVC also maintains a root WebApplicationContext, which includes the application-level configurations such as backend data source, security, service and persistence layer configuration, and so on. The root WebApplicationContext will be available to all servlet-level WebApplicationContext. Let’s look at an example. Let’s say in an application we have two DispatcherServlets. One servlet is to support the user interface (we call it the application servlet), and the other is to provide services in the form of RESTful-WS to other applications (we call it the RESTful servlet). In Spring MVC, we will define the configurations for both the root WebApplicationContext and the WebApplicationContext for the two DispatcherServlets. Figure 17-2 shows the WebApplicationContext hierarchy that will be maintained by Spring MVC for this scenario.

spring-mvc-webapp-context

 

MVC Design Pattern

spring-mvc

 

Figure 17-1 illustrates a commonly used web application pattern, which can be treated as an enhancement to the traditional MVC pattern. A normal view request is handled as follows:
1. Request: A request is submitted to the server. On the server side, most frameworks (for example, Spring MVC, Struts, and so on) will have a dispatcher (in the form of a servlet) to handle the request.

2. Invokes: The dispatcher dispatches the request to the appropriate controller based on the HTTP request information and the web application configuration.

3. Service Call: The controller interacts with the service layer.
4. Response: The controller updates the model and, based on the execution result, returns the corresponding view to the user.

In addition, within a view, Ajax calls will happen. For example, say the user is browsing data within a grid. When the user clicks the next page, instead of a full page refresh, the following flow will happen:

1. Request: An XMLHttpRequest is prepared and submitted to the server. The dispatcher will dispatch the request to the corresponding controller.
2. Response: The controller interacts with the service layer, and the response data will be formatted and sent to the browser. No view is involved in this case. The browser receives the data and performs a partial update of the existing view.

In the Spring Framework, the Spring MVC module provides comprehensive support for the MVC pattern, with support for other features (for example, theming, i18n, validation, type conversion and

formatting, and so on) that ease the implementation of the presentation layer.
In the following sections, we will discuss the main concepts of Spring MVC. Topics include Spring MVC’s WebApplicationContext hierarchy, a typical request-handling life cycle, and configuration.

How to fix an android application crash? How to analyze a crash using logcat?

Unlike Java, Android doesn’t display its runtime exceptions in its console. It displays it using its LogCat. Android provides you a debugging tool called DDMS (Dalvik Debug Monitor Server) which contains a LogCat. LogCat is integrated into the DDMS which outputs the messages that you print out using a Log class.

What is LogCat?

LogCat is a command used for looking into the logs generated from various programs running on Android application environment. This logcat can be used from ADB shell to view the logs. The following is the syntax for using logcat command:

[adb] logcat [<option>] … [<filter-spec>] …,

The DDMS Perspective

The DDMS perspective in Eclipse lets you access all the features of DDMS. We use the LogCat feature of DDMS for Debugging our applictaion.  To access the DDMS perspective in eclipse, please go to Window->Open Perspective->DDMS. If DDMS does not appear go to Window->Open Perspective->Other->Select DDMS from the Open Perspective window. Once the DDMS perspective appears on the Ecilpse Click on it which opens the below screen.

Image

How to open LogCat?

you can find the LogCat window by switching to  DDMS perspective(refer the above screen) or you can go to window->Show View->Other->Android->click LogCat->Now you will be automatically switched to DDMS perspective where you can find LogCat.

Image

The Android Logging provides a mechanism for collecting and viewing system debug output. Logcat displays Logs generated by system and also by your application. Every android log message has a tag and Priority associated with it.

  • Tag:  string indicating from which component the message originates.
  • Priority: is one of the character values ordered from lowest to highest priority. LogCat has different levels of logging.
  • V-Verbose(lowest priority)
  • D-Debug
  • I-Info
  • W-Warning
  • E-Error
  • F-Fatal
  • S-Silent

Android Bundle vs Parcel vs Message

A Bundle is functionally equivalent to a standard Map. The reason we didn’t just use a Map is because in the contexts where Bundle is used, the only things that are legal to put into it are primitives like Strings, ints, and so on. Because the standard Map API lets you insert arbitrary Objects, this would allow developers to put data into the Map that the system can’t actually support, which would lead to weird, non-intuitive application errors. Bundle was created to replace Map with a typesafe container that makes it explicitly clear that it only supports primitives.

A Parcel is similar to a Bundle, but is more sophisticated and can support more complex serialization of classes. Applications can implement the Parcelable interface to define application-specific classes that can be passed around, particularly when using Services. Parcelables can be more sophisticated than Bundles, but this comes at a cost of significantly higher overhead.

Bundle and Parcel are both data serialization mechanisms, and for the most part both are used when application code is passing data across processes. However, because Parcel is much higher overhead that Bundle, Bundles are used in the more common places like the onCreate method, where overhead must be as low as possible. Parcels are most commonly used to allow applications to define Services with logical APIs that can use application-meaningful classes as method arguments and return values. If we required Bundle there, it would result in really clunky APIs. You should in general still keep your Service APIs as simple as possible, because primitives will serialize more efficiently than custom Parcelable classes.

The Message class is very different. A Message is really just a way for an application to send messages between threads, and is not related to data serialization. Since accesses to Views must be single-threaded, the MessageHandler and related Message classes are commonly used to make it easier to pass messages between threads.

Android Activity Life Cycle

Every Android application runs inside its own process. Processes are started and stopped to run an application and also can be killed to conserve memory and resources. Activities, in turn, are run inside the main UI thread of the application’s process.

Once an activity is launched, it goes through a lifecycle, a term that refers to the steps the activity progresses through as the user (and OS) interacts with it. There are specific method callbacks that let you react to the changes during the activity lifecycle.

The activity lifecyle has four states.

04fig07

  • When the activity is on the foreground of the application, it is the running activity. Only one activity can be in the running state at a given time.
  • If the activity loses focus but remains visible (because a smaller activity appears on top), the activity is paused.
  • If the activity is completely covered by another running activity, the original activity is stopped. When an activity stops, you will lose any state and will need to re-create the current state of the user interface when the activity is restarted.
  • While the activity is paused or stopped, the system can kill it if it needs to reclaim memory. The user can restart the activity.

While the application moves through the different states, the android.app.Activity lifecycle methods (or callbacks) get called by the system. These callbacks are as follows.

  • onCreate(Bundle savedInstanceState)is called when the activity is created for the first time. You should initialize data, create an initial view, or reclaim the activity’s frozen state if previously saved (this is covered later). The onCreate callback is always followed by onStart.
  • onStart() is called when the activity is becoming visible. This is an ideal place to write code that affects the UI of the application, such as an event that deals with user interaction. This callback is normally followed by onResume but could be followed by onStop if the activity becomes hidden.
  • onResume() is called when the activity is running in the foreground and the user can interact with it. It is followed by onPause.
  • onPause() is called when another activity comes to the foreground. The implementation needs to be quick, because the other activity cannot run until this method returns. The onPausecallback is followed by onResume if the activity returns to the foreground, or by onStop if the activity becomes invisible.
  • onStop() is called when the activity is invisible to the user; either a new activity has started, an existing activity has resumed, or this activity is getting destroyed. The onStop callback is followed by onRestart if the activity returns to the foreground.
  • onRestart() is called when the activity is being restarted, as when the activity is returning to the foreground. It is always followed by onStart.
  • onDestroy() is called by the system before the activity is destroyed, either because the activity is finishing or because the system is reclaiming the memory the activity is using.

Android Important Useful links

Android Splash screen size generator

XML to JSON Generator

Google Maps with JavaScript

Quick Bootstrap customized buttons

AngularJS with D3 Data Visualization

Http Status Codes

Crosswalk Android

Cordova for AngularJS

Screencasting for Android, iPhone and Windows

Ionic framework

Annotation based Android Development

Google based AngularJS libraries

Roboguice framework for android

Android opensource based libraries

Rest API Reference

JSON Object Viewer

CSSPre Convert

LessToCss,  LessToCssAnother 

Android String Resources

One poor coding practice that really gets our blood boiling is when developers include raw string content inline in code. Sure, there are occasionally good reasons to do this, especially when writing debug code, but generally speaking, if your Android application relies on string content that is displayed on the screen, this data belongs in the Android resources, not in the code itself.

There are a number of benefits to including string content as a resource, such as:

  • It centralizes the strings used by the application in a single location that is easily managed (by the developer or a non-developer).
  • Strings can be defined as a resource once, and used throughout the code. Therefore, it will have consistent spelling, case and punctuation.
  • Strings can be internationalized easily, allowing your application to support multiple languages with a single application package file (APK).
  • Strings don’t clutter up your application code, leaving it clear and easy to maintain.

Step 1: Define String Resources for Individual Strings

String resources belong in the /res/values/strings.xml file. Define a string resource entry for each string used by your application. Name your string resources appropriately. For example, the following XML excerpt defines a number of game character race types as individual strings:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string
        name="app_name">Choose Your Own</string>
    <string
        name="race_orc">Orc</string>
    <string
        name="race_elf">Elf</string>
    <string
        name="race_troll">Troll</string>
    <string
        name="race_human">Human</string>
    <string
        name="race_halfling">Halfling</string>
    <string
        name="race_goblin">Goblin</string>

Narendra Modi Core Team

Narendra Modi, chief minister of Gujarat and BJP’s prime ministerial candidate, has been running his state government with a core team consisting of trusted officials and a handful of ministers.

The chief minister’s office has four IAS officers and a couple of them may make it to New Delhi if he becomes the PM.

Modi’s principal chief secretary Kuniyil Kailashnathan, a 1979 batch IAS officer, is arguably the most trusted of all. `KK’, as he is commonly known, is one of the key men behind Modi’s political maneuvers and it is through him that Modi controls the bureaucracy, according to sources.

The importance of the man can be judged from the fact that after he had retired on May 31 last year, he was promptly appointed on a contractual basis for two years as `Gujarat government’s chief principal secretary to chief minister.’ He assumed his duty on June 1.

KK is learnt to have played crucial roles strategizing the last two assembly elections in the state and has been involved in political liasoning on Modi’s behalf during 2014 campaign as well.

meet-narendra-modis-core-team-of-four-bureaucrats-who-drive-the-modi-machine

The other significant officer is GC Murmu, who is posted as principal secreatary to the chief minister. The 1985 batch officer has been handling the legal issues confronted by both Modi and Amit Shah.
Murmu allegedly attended a meeting at the office of Gujarat advocate general where reportedly strategies were discussed to derail the Ishrat Jahan fake encounter case. Subsequently, he was questioned by the Central Bureau of Investigation along with another IPS officer.

It is expected that both Kailashnathan and Murmu may relocate to Delhi if Modi becomes the next prime minister.

While Modi has been projecting development as the main election plank, it is a 1988 batch IAS officer AK Sharma who has been behind building the brand image for Modi through his liasoning with the industry.

Sharma, who hai ls from Azamgarh in Uttar Pradesh, is posted as the additional principal secretary to the chief minister.

Sharma had played pivotal roles in organizing Vibrant Gujarat Investor’s Summits and was also posted as the chief executive officer of Gujarat Infrastructure Development Board.

The junior most IAS in the chief minister’s office is Vijay Nehra. The 2001 batch IAS officer was posted as Ahmedabad district collector till last year.

Then he was transferred as the joint secretary (law and order) in the Home Department with additional charge of joint secretary at Modi’s office.

Later, his position was re-designated as additional secretary to the government in the chief minister’s office. Nehra, who reportedly share good rapport with Modi’s trusted lieutenant Anandi Patel is being groomed for more important roles, according to sources.

Quotes

You have to learn the rules of the game. And then you have to play better than anyone else.
– Albert Einstein

Success isn’t something that just happens – success is learned, success is practiced and then it is shared.

Sparky Anderson

The moment you recognize what is beautiful in this world, you stop being a slave.
Aravind Adiga

“Strength does not come from winning. Your struggles develop your strengths.”
Arnold Schwarzenegger

“If you don’t build your dream, someone else will hire you to help them build theirs.”
Dhirubhai Ambani

“What would life be if we had no courage to attempt anything.”
Vincent Van Gogh

“Live as if you were to die tomorrow. Learn as if you were to live forever.”
Mahatma Ghandi

“To be yourself in a world that is constantly trying to make you something else is the greatest accomplishment.”
Ralph Waldo Emerson

“To Conquer oneself is the best and noblest victory;to be vanquished by one’s own nature is the worst and most ignoble defeat.”
Plato

Daily Changes In Our Lifes