天天看點

CRUD using Spring MVC 4.0 RESTful Web Services and AngularJS

内部邀請碼:C8E245J (不寫邀請碼,沒有現金送)

國内私募機構九鼎控股打造,九鼎投資是在全國股份轉讓系統挂牌的公衆公司,股票代碼為430719,為“中國PE第一股”,市值超1000億元。 

------------------------------------------------------------------------------------------------------------------------------------------------------------------

CRUD using Spring MVC 4.0 RESTful Web Services and AngularJS

Based on the requests from many readers, I am now presenting an article on how to make CRUD operations using Spring MVC 4.0 RESTFul web services and AngularJS. I had already written few articles on Spring MVC 4.0 RESTFul Web Services in case you are new to this.

For the sake of best understanding, I came up with a small task manager AngularJS application powered by Spring MVC 4.0 RESTFul Web Services. With this application, you can list all existing tasks, create a new task, mark completion of an existing task and archive completed tasks. I had tried to keep it very simple as this is not for real time use but for the best understanding of the concept.

-- MySql Server

-- Eclipse J2EE

-- Tomcat Server v7.0

1. Let us start by creating task table in MySql Server.

Execute the below commands in MySql Server. This will create a dedicated database for our application and will create a task_list table with dummy values to start with initially,

2.  Download

.

             (1) jackson-annotations-x.x.x.jar

             (2) jackson-core-x.x.x.jar

             (3) jackson-databind-x.x.x.jar

        -- Download mysql java connector library.

3. Create a dynamic web project in eclipse and add the above downloaded jar files to your application WEN-INF\lib folder.

4. Now edit web.xml file under WebContent folder to notify the application container about the spring configuration file. Add below code before </web-app>

Note that in the above code,we have named Spring Dispatcher servlet class as "rest" and the url pattern is given as "/*" which means any uri with the root of this web application will call DispatcherServlet. So what's next? DispatcherServlet will look for configuration files following this naming convention -  [servlet-name]-servlet.xml . In this example, I have named dispatcher servlet class as "rest" and hence it will look for file named 'rest-servlet.xml'.

5. Create a file under WEB-INF folder and name it as "rest-servlet.xml". Add below spring configuration code to it,

I have already explained component-scan element and mvc:annotation-driven element in my previous article, so I am not going to repeat it here again.

6. Create a Java class and name it "Task.java". This is the model class and it represents the fields of a single task in the database.

7. Create a utility class to handle connections to database. The connection string properties are kept in a configuration file called "db.properties" in the src folder.

Properties configuration file should have contents such as this,

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/databasename

user=username

password=xxxxxx

8. Create a service class that performs data access operations to get data from database,

9. Create Spring Controller class that maps the incoming request to appropriate methods and returns response in json format. We are going to use @RestController annotation which has @Controller and @Responsebody annotated within itself.

Let us now take a closer look into the methods we have in the Spring Controller class.

-- Initially we use getAllTasks () method to fetch all tasks from database. This will fetch all tasks that are not archived (task_archived = '0').

-- Then we give an option to the user via archiveTasks() method, to archive all completed tasks so that it won't show up on the dashboard. For this purpose, we have a field in task_list table called 'task_archived' with values 0 or 1 marking whether a task is archived or not.

-- An option to update the status of a task from 'ACTIVE' to 'COMPLETED' or vice-versa is provided in the changeStatus() method

-- AddTask() method enables one to add a new task to the database.

10. Let us now create the jsp file that sends requests to the Spring controller to fetch data and to update data in the server,