Building Spring MVC Framework Using IDEA

To be honest, I have working on this for at least two weeks. I read blogs, relevant books, and watched several related videos on various e-course. A large majority of them build Spring using MyEclipse which is out of my expectation. I can barely find any introduction for IDEA users, and even for those do, many are out-dated. God knows how many times I install and uninstall the framework. I wasted adding up to at least 24 hours to wait that damn Maven to configure the dependency. It drove me crazy especially when lots of homework amassed, meaning I have to work on those stuff at the midnight.

Since this is a very easy demo, and No Principle Explanation will be involved. This blog is solely used to remind me (or maybe you) how to build up a Spring MVC Framework using IDEA in a short time.

0x01 Tomcat Ready

Since it’s Spring MVC, what we’re building after all is a web application. Therefore, we first new an Java EE Project, calling it QuickDemo.



Click Finish button at the bottom, and a basic Java EE project is built up.

Now the project sidebar should look like this:


Don’t forget to edit configuration of Tomcat server!

Don’t forget to change the Application context to /, or later on all URL you visit will automatically add this QuickDemo_war_exploded beforehand.

0x02 Maven Dependencies Addition

​ Maven is a software project management and comprehension tool based on the concept of a project object model. In brief, it can be used to manage your various JARs imported from the external libraries.

​ Right click the project QuickDemo and choose to Add Framework Support:


Check the check-box of Maven.

groupID can be whatever you want(better be your company name).Mine is com.Michael.ArtifactId is your project name, here is QuickDemo. Don’t worry about everything else, just click Finish.

Waiting for several moments,you shall see sidebars become:


Open pom.xml, you shall see this:



Event Log may jump out saying that ‘Maven projects need to be imported’. Just click Import Changes is fine.

Next,we’re about to add dependencies of Spring JAR.

In pom.xml, write those content:

     <!--JUnit,used in test-->

        <!--Spring frameworks-->

            <!--Sometimes your servlet-api version may conflicts with default-->
            <!--scope means using maven ones-->

        <!--JSP View Resolver-->

        <!--finalName should be your project name-->
            <!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
                <!-- see -->

Import Changes, and done.

Just in case, in File>>Project Structure>>Artifacts, right-click and put these JARs into ROOT. Click Apply and then OK.


Now the sidebar should look like this:

All JARs are imported successfully.

0x03 Writing Configuration XML

​ The most frustrating work of all is writing the configuration XML. Right Click WEB-INF, and new an Spring Config XML,with the name of dispatcher-servlet.xml:


It should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""


Dispatcher-servlet plays an essential role in Spring MVC, it functions as a redirector, receiving requests and retrieving response and sending to other components.

​ But before we do anything, I suggest we first make some directories and packages in the src package.

​ Under src/main/java directory, new three independent packages, respectively named controllers, views,models.

​ Under WEB directory, you can new a new directory called JSP, putting your JSP that wrote beforehand under it. Mine JSP name is called index.jsp, and its content looks like this:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
  This is a quick demo.

Then, under controllers package, we can new a Java class with whatever name you want. According to the common naming principles, I’m calling it IndexController.

In, I wrote:

package controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

public class IndexController {
    @RequestMapping(value="/",method= RequestMethod.GET)
    public String index(){
        return "index";

Now the project structure should be like this:


After that, in dispatcher-servlet.xml, paste this:


    <context:component-scan base-package="controllers">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/JSP/"/>
        <property name="suffix" value=".jsp"/>
        <property name="exposeContextBeansAsAttributes" value="true"/>

And don’t forget web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""


        <!-- <async-supported>true</async-supported>-->


0x04 Run!

Ignoring all details such as ‘Why writing these codes’, we can click the Run button on the right-top corner of IDEA. There may be some trivial warnings, but as long as it’s not an error, let it go. Finally, at URL: http://localhost:8080 we should see:


It may seems easy, but for me with barely little knowledge of Spring at that time, writing XML is really a painful experience. I begin to miss the good days when writing Flask.

Java Reflect Basics

I used to believe Reflect in Java was difficult because Core Java placed relevant chapters after Exceptions and Lambda. This is rather confusing. I am not ready for it until my senior in Vidar-Team, Ash , assigned a homework to figure out three ways to enable subclasses to get access private fields or methods of their superclass. All what I can think of is writing public getter/setter methods in superclass or making modifier private to protected. It was then Ash mentioned Reflect, which I almost forgot.

In fact, Reflect is used pretty common in Java, especially dealing with JDBC. Reflect is designed to fetch or revise details of a class when you have already compile it.

Every Reflect starts with a Class.forName();

When you’re writing Reflect, the first thing you do is to define which class you want to fetch. forName(String className) method returns a Class. Open your eye and watch it carefully. It is capitalized, meaning it’s a special type. So do Method, Field, and Constructor.

I’m tired of paraphrasing Reflect documentation, while copying and pasting Java documentation looks bored. Thus, I decide it to demonstrate the usage of **Reflect **through simple codes.

Student is a class with both public and private fields and several public methods.

package example.Reflect

public class Student {
    pubic String name;
    private int id;
    private int score; 

    public int getId() {
        return id;

    public void setId(int id) { = id;

    public void getScore(){
        return score;

    public void setScore(int score){

    public void showAll(){
        System.out.println("'s ID is "".His score is"+this.score);

    private int preID(int id){
        return (1819000+id);


In order to test its Object-Oriented features, the main() method is written in another class)

public static void main( String[] args ){ try { Class c = Class.forName("example.Reflect.Student"); Field[] field = c.getFields(); for(Field f:field){ System.out.println(f); } System.out.println("##########") Methods[] method=c.getMethods(); for(Method m:method){ System.out.println(m); } } catch (ClassNotFoundException e) { e.printStackTrace(); //IDE will remind you if you forget to throw this exception } catch(NoSuchFieldException e){ e.printStackTrace(); }catch(NoSuchMethodException e){ e.printStackTrace(); } } /*result: public String ########## public java.lang.String example.Reflect.Student.getScore() public int example.Reflect.Student.getID() public void example.Reflect.Student.setScore() public void exmaple.Reflect.Student.setID() public void example.Reflect.Student.showAll() public final void java.lang.Object.wait() throws java.lang.InterruptedException public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException public boolean java.lang.Object.equals(java.lang.Object) public java.lang.String java.lang.Object.toString() public native int java.lang.Object.hashCode() public final native java.lang.Class java.lang.Object.getClass() public final native void java.lang.Object.notify() public final native void java.lang.Object.notifyAll() */

As you can see, only public field is printed, and a whole bunch of methods printed belongs to java.lang.Object, the superclass of all other classes.

So, how to fetch both public and private methods and fields of Student, leaving those in Object aside?

Easy, change getFields() to getDeclaredFields, and similarly, getMethods() to getDeclaredMethods().


private int
private int example.Reflect.Student.score
public java.lang.String
private int example.Reflect.Student.preID(int id) <---
public java.lang.String example.Reflect.Student.getScore()
public int example.Reflect.Student.getID()
public void example.Reflect.Student.setScore()
public void exmaple.Reflect.Student.setID()
How to Create a New Instance of a class using Reflect?

For those classes that only posses default constructor, newing an instance is fairly easy.

public static void main(String[] args){
        Class c=Class.forName("example.Reflect.Student");
        Student student=(Student)c.newInstance();
        //student's name uninitalized.
    }catch(ClassNotFoundException e){
}//result: null's ID is 100.His score is 100

Only thing to mention is that newInstance() returns an Object, so you have to cast it to the particular one.

What if the target class has a constructor with parameters? Introducing Constructor.

Still taking class Student as example, after I rewrite the constructor into one with parameters, other parts remaining the same:

class Student{
    public Student(String name,int id, int score){;;

In main() method:

public static void main(String[] args){
        Class c=Class.forName("example.Reflect.Student");
        //Student student=(Student)c.newInstance();<-- throw NoSuchMethodException(no default constructor!)

        Constructor constructor=c.getConstructor(String.class,int.class,int.class);
        Student student=(Student)constructor.newInstance("Michael",100,99);

    }catch(ClassNotFoundException e){
        //exception forName
    }catch(NoSuchMethodException e){
    }catch(IllegalAccessException e){
        //Some methods or constructors may be private or protected,or inaccessible
    catch(InvocationTargetException e){
        //Exceptions when newing with Constructor(with capital C)
    }catch(InstantiationException e){

}//result:Michael's ID is 100.His score is 99

Do remember java.lang.reflect.* should be imported automatically by IDE or by you yourself, and don't forget to throw these exceptions.

Back to the Very Beginning: How can a Subclass Get Access to Private Fields or Methods of its Parent?

Ladies and gentleman, hereby I, with honor, introduce you setAccessible() .

To make it easy to understand, I'll rewrite class Student all over again, and then write another subclass of it.

package example.Reflect

public class Student {
    private String name;
    private int id;
    private int score; 

    public Student(String name,int id,int score){;;

    private int IDProcess(int id){
        return (1801000+id);


I devise of a Mechanic class that extends Student.

package example.Reflect

public class Mechanic extends Student{

    public Mechanic(String name,int id,int score){


OK, here comes a problem. What if I want to add a method in Mechanic instead of Student, printing out all the information of a Mechanic instance without writing any getter/setter in Student, and no changes to the modifier private ?

1.Fetching Fields

I don't know haven't you tried, but it is really frustrating that neither nor works in this case. Without any public setter/getter, the work seems impossible. Of coarse you can copy and paste all the fields of Student to Mechanic, but that would be stupid.

It is time for to invite our protagonist today out. Reflect!Showing the code probably gives you a better understanding.

public void showName() throws  ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException{
    Class c=Class.forName("example.Reflect.Student");
    Field field=c.getDeclaredField("name");
    String name=(String)field.get(this);//must cast or it returns an Object

setAccessible()has one boolean parameter. It is set false as default until the programmer set it true.

Test it in main()

public static void main(String[] args){
   Mechanic mstudent=new Mechanic("Michael",100,100);
   try {
        } catch (ClassNotFoundException e) {
        } catch (NoSuchFieldException e) {
        } catch (IllegalAccessException e) {
}//result: Michael

As seen in showName(), field.get()method is used to retrieve the value of the field of a specified instance. Similarly, field.set() method is used to set the value of the field of the instance.

For example:

public void setName(String name) throws ...{
    ...//Class.forName().. etc,just like showName()
}// exactly the same like writing setter in parent class.

It works all the same with other fields like ID and score. Due to limited space, I'll skip them.

2.Calling Methods

Almost resemble the codes above:

public void showID(int id) throws  ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException{
    Class c=Class.forName("example.Reflect.Student");
    Methods method=c.getDeclaredMethod("IDProcess",int.class);
    //inside the parentheses:methodName and its parameters(param can be one or more)
    //Don't forget to cast, or it'll return something like example.Reflect.Mechanics@10b28a4

Test it in main():

public static void main(String[] args){
   Mechanic mstudent=new Mechanic("Michael",100,100);
   try {
        } catch (ClassNotFoundException e) {
        } catch (NoSuchMethodException e) {
        } catch (IllegalAccessException e) {
        }catch(InvocationTargetException e){
}//result: 1801100

As always, import java.lang.reflect.*. You could either wrap your thing in try/catch, or throw exceptions.

At the End of the Day

Above are just the very very basic usage of Java's Reflect. Michael is still learning and coding, on the arduous journey to become those who he admires and his mentors. Should any mistakes, even trivial typos, exists, please write your comment below to let Michael know. He'll be humbly taking your precious advise.

Why is ArrayList dynamic?

Anyone who has learned ArrayList systematically should have basic knowledge that ArrayList is a List based on dynamic array. You don’t have to worry about errors like ‘ArrayOutOfBoundary’, because the length, or size, is not static,unless you give it a boundary,or capacity when you initialize an ArrayList. How do Java achieve this function? Talk is cheap, read the code.

Before that, I believe it’s fairly essential to distinguish the differences between Capacity and size.
Capacity in numbers return the length of the Array, putting the null elements into consideration;
Size returns how many non-null elements the array contains.
For example:

int[] a = new int[10];
for(int i=0;i<5;i++){
The size of a is 5,while the capacity is 10,which never changes once you new it.

Reading Comprehension Time!

//The default_capacity of ArrayList
private static final int DEFAULT_CAPACITY = 10;

//Empty Array Elements
transient Object[] elementData;

//default initial capacity
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

private int size;

//When using the default constructor
public ArrayList() {

//size=10 if you just new the ArrayList using default constructor; 
public boolean add(E e) {
    ensureCapacityInternal(size +1);// modCount incremented
    elementData[size++] = e;
    return true;

/*initialize the ArrayList size to 10;
 * Increases the capacity to ensure that it can hold at least the
 * number of elements specified by the minimum capacity argument.
 * @param minCapacity the desired minimum capacity

private void ensureCapacityInternal(int minCapacity) {
        ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));

/*Why spend so much on pursing minimum capacity at the cost of
 *making the code so complicated?
 *To save the precious memory, I guess.

//When the programmer use the constructor with parameters that limits the size;
private static int calculateCapacity(Object[] elementData, int minCapacity) {
            return Math.max(DEFAULT_CAPACITY, minCapacity)
            /*if the size is smaller than 10, it's no big deal if you create a 10 one,
            *since you can trim it;
            *but this is not the case when it's the opposite.
        return minCapacity;

private void ensureExplicitCapacity(int minCapacity) {
        modCount++;//modCount is to record how many times the capacity has changed;
        if (minCapacity - elementData.length > 0)
            /*if the size of Array at present is larger than the capacity of Array;
             *it works well even when minCapacity is set negative
            grow(minCapacity);//literally, grow:)

// There's a upper limit of the size of ArrayList
//Why MAX_VALUE-8? Because an array need a 8 bytes(size of an int)of memory 
//to store the size of itself
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

private void grow(int minCapacity) {
    int oldCapacity = elementData.length;//oldCapacity is size
    int newCapacity = oldCapacity + (oldCapacity >> 1);
    //The new capacity is the 1.5 times larger than the old one
    if (newCapacity - minCapacity < 0)
        //make sure capacity is the smallest
        newCapacity = minCapacity;
    if (newCapacity - MAX_ARRAY_SIZE > 0)
        //Of course, if the array is too big...
        newCapacity = hugeCapacity(minCapacity);

    // minCapacity is usually close to size, so this is a win:
    elementData = Arrays.copyOf(elementData, newCapacity);
    //create an object array(a new one!) with all value from
    //elementData and a capacity of newCapacity 


private static int hugeCapacity(int minCapacity) {
    if (minCapacity < 0) 
        // Capacity can't be a negative number,anyway
        throw new OutOfMemoryError();
    return (minCapacity > MAX_ARRAY_SIZE) ?
        //if capacity is too big
        Integer.MAX_VALUE :

Both remove(int index) and remove(Object o) calls fastRemove(int index):

 private void fastRemove(int index) {
        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
        elementData[--size] = null; // clear to let GC do its work

As you can see, capacity of array does not change when elements are removed. This is because if you clear the unused memory immediately, then you need to take extra time to allocate them when you eventually decide to use them, which is low-efficient.


Concrete and detailed analysis is in annotation of the code above.

Three core private methods of ArrayList, ensureCapacityInternal(), ensureExplicitCapacity(), and grow() (all parameters ignored) explain why it is dynamic. Capacity in the very beginning is 0 after newed by defualt constructor until add() is called. When the programmer calls either add(int index, Object o) or add(Object o), and it happens that the size of the list is larger than the present capacity, ensureCapacityInternal() will be called. It first decides how much memory to allocate , then JVM calls ensureExplicitCapacity(), and finally grow() method is called. Method grow() directly expands the capacity by 1.5 times and in the end System.copyOf() copy everything of the array with small capacity to the new 1.5 larger one, and returns the new array to programmers. Since these core methods are private and encapsulated, ArrayList shows up in a dynamic appearance.

Magic Methods in Python

This first post of programming is about Python’s Magic Method

Magic Method is not marked as an essential point in the book Python Crash Course, which actually is. To distinguish it from other ordinary methods, magic methods start with two underscores.

Magic Methods are mainly used in a class. The magic methods I use frequently(and recently) are those below:

def __init__(self): It is just like the Constructor in Java,the use of which is to initialize the attribute of the class.

Keep in mind that there is no this keyword in Python.Instead of this, Python use self keyword.You cannot claim an attribute outside like Java do. Claim it in the method.

Here’s an example:

class Car():
    def __init__(self,brand):

Here’s the Java’s way of doing this:

class Car{
    private String brand;
    public Car(String brand){


def __str__(self):The method is just like the toString() method in Java,enabling you to print the object in your customized way.You should override it when you need it.

Still taking the class car as example:

class Car():
    def __init__(self,brand):
    def __str__(self):
        return "The brand of the car is %s."%self.brand

And it’s rather obvious when you run the file and make the parameter brand to Toyota,you get the result The brand of the car is Toyota.


Don’t miss this one:__dict__

The method is not put in the class. This function returns a dictionary of a certain entity (or instance in Java terms).

class Car():
    def __init__(self,brand,price):
    def __str__(self):
        return "This %s car costs about $%d "%(,self.price)

The result will be:


This is a super useful method.