How To Insert Data Using Struts2 & AngularJS
How To Insert Data Using Struts2 & AngularJS

AngularJS is a structural framework for dynamic web apps. It lets you use HTML as your template language and lets you extend HTML’s syntax to express your application’s components clearly and succinctly. AngularJS’s data binding and dependency injection eliminate much of the code you would otherwise have to write.

Every Java developers may be want to use AngularJS but limited tutorials present . Due to this i created this tutorial series and in this tutorial we discuss about insert data using Struts2 & AngularJS.

We have to create a database, then a table with the following table structure

Run the following SQL code on your PhpMyAdmin. This is to create our database table name products. By the way, the database name we used in this tutorial was named struts2angularcrud.

Struts2 AngularJS Insert Operation

I used jQuery and MaterializeCss for designing purpose in addition. You can use any thing for that.

What was in above code?

  1. Line 1: You can see ng-app=”myApp” .The ngapp directive tells AngularJS that this is the root element of the AngularJS application. All AngularJS applications must have a root element. You can only have one ngapp directive in your HTML document. If more than one ngapp directive appears, the first appearance will be used. You can use any name instead of myApp.
  2. Line 6-9: It is code of floating button to open modal with form and AngularJS perform click function using ng-click directive. Here we use showCreateForm() we discuss about this function implementation below.
  3. Line 17,22,28: It is normal textfield of form but one thing is different which is ng-model. The ngModel directive binds an input , select , textarea (or custom form control) to a property on the scope using NgModelController, which is created and exposed by this directive. ngModel is responsible for: Binding the view into the model, which other directives such as input , textarea or select require.
  4. Line 34: It is for Create button with ng-click=”createProduct()” .

Lets discuss about AngularJS part:

In this project i made separate js file for AngularJS named : product.js

What was in above code ?

  1. Line 2: You can initialized your Angular app.
  2. Line 3: After init the app you need to name the controller. A controller is defined using ng-controller directive. A controller is a JavaScript object containing attributes/properties and functions. Each controller accepts $scope as a parameter which refers to the application/module that controller is to control.
  3. Line 7-20: Here the implementation of click on floating button and perform showCreateForm() function click.
  4. Line 24-48: You can see we used $http services. The $http service is a core AngularJS service that facilitates communication with the remote HTTP servers via the browser’s XMLHttpRequest object. In this service we call post method which first parameter for Java action(addproduct.action) and second parameter for form data.

Now lets move on Struts2 part

Firstly we create class named Product.java

After that we define addproduct.action in struts.xml and your file will be:

And now create method insertProductData in ProductAction class

Finally your insertion tutorial is finished.

If you have any questions or corrections, or with other suggestions, please let us know.
We’ll try to solve it at the earliest possible time.
Also please share this post on social media, use the share icons just below the post.
Don’t forget to subscribe to our blog.
Thank you for reading.

11 COMMENTS

  1. It’s remarkable to pay a quick visit this site and reading the views of all colleagues on the topic of this post, while I am also keen of
    getting familiarity.

  2. Hi there Jimmy,Because of for obtaining out! Right here’s a website link towards the competitiveness indicator up web site. Simplest,Jillian

  3. 554692 748561Register a domain, search for available domains, renew and transfer domains, and decide on from a wide variety of domain extensions. 346025

  4. During this period, the seed loses most of the moisture that had. They offer a widespread variety of services for residential, commercial and industrial customers. This will assist in making cash, earning excellent reputation, obtaining many cases in hand etc. As a result, a lot of furniture houses extend their services towards the designing of community furniture and make way for better styling and look of the community spaces. Anytime your self contain their birthday and anniversary dates, on your own can ship them e mail and reminders, which will guidance your small business be thing to consider of inside a further personable direction.
    [url=http://www.cheapnfljerseyschinawholesaler.com/]Cheap Vintage Jerseys[/url]

  5. You might get frustrated by customer complaints, but they can be a huge benefit to your web marketing business. Work as closely as you can with experienced nurses in your field, and build your knowledge and base of hands on experience. The initial growth requires the use of the reserve substances which had previously been stored in the endosperm or cotyledons. All through my interactive world wide web, my sufferers can profit the content material they want every time they need to have it,” reported Dr. Not less than an having to do with going to be the mirrors action to take,all of which allows going to be the laser beam to scan a multi function html page Multiple mirrors can redirect the beam on the lots of directions.
    Wholesale Soccer Jerseys [url=http://www.wholesalecheapnflsportsjerseys.com/]Wholesale Soccer Jerseys[/url]

LEAVE A REPLY

Please enter your comment!
Please enter your name here