Send HTML Form data to struts2 action by Object Backed Method

HTML form data can be transferred to Struts2 Action in three ways.

1. Normal Method with simple getters() and setters()
2. Object Backed Method
3. Model Driven Method

We will see the second method now.

Object Backed Method

Create a simple jsp file index.jsp with struts2 form tags as below. The only difference from normal method and Object backed method is while naming the fields the name should be defined deeply. See below.

The meaning of the above code is all the values will go inside the user object. So As per the above page when the form is getting submitted it will look for an action with name register.

Now See our struts.xml below.

In our Struts.xml we pointed the register action to register() method in the Register class. So when the form is getting submitted the control will execute the register() method. As per struts2 design the control will search the methods for all available data fields. In our case we named all the fields deeply like user.fname,user.lname.

So the control will look for method setUser() in the action class that takes an object as parameter which have variables matching to the data fields. (i.e) An object contains lname, fname, age and email as members will be the parameter for setUser() method.

Also the parameter object class should contains setters() and getters() method for required variables. So while executing the setUser() method the control will call the setters inside the User object for corresponding data fields.

Now see our Register class and User Class below.

As we know the Struts2 framework will execute the setUser() method before executing the actual action method register(). So while executing the setUser() method it will call the setter methods inside the User Object. Here user.setLname(), user.setFname(), user.setAge() and user.setEmail(). So all the variables will be assigned to the user object.

In our action method we just displayed the values in the console, then returning success message as result name.
So as we mentioned the result page is success.jsp for string “SUCCESS” struts2 will load the page success.jsp to the user. See our success.jsp below.

What will happen for those tags like <s:property value ="user.fname"/> . Struts2 will look for get methods in the corresponding action class. As we discussed earlier due to the name of the field it will look for method getUser() ninside the action class.  As we have getUser() method inside the class it will return the object with all values. Then all the fields will be filled then returned to the user.

For the above method I have created a complete .war file with source code. Download and just place under Apache web apps folder and run.

If you have any issues while execution please post in comments or send mail to rajeshmepco@gmail.com.

Posted in Struts2 | Tagged , | Leave a comment

Send HTML Form data to struts2 action by Normal Method

HTML form data can be transferred to Struts2 Action in three ways.

1. Normal Method with simple getters() and setters()
2. Object Backed Method
3. Model Driven Method

We will see the first method now.

Normal Method with simple getters() and setters()

Create a simple jsp file index.jsp with form (struts2 tags) as below

So As per the jsp page when the form is getting submitted it will look for an action with name register. See out struts.xml below.

In our Struts.xml we pointed the register action to register() method in the Register class. So when the form is getting submitted the control will go the register() method. As per struts2 design the control will look methods for all available values. In out case for fname,lname,email and age. It will search for methods setFname(),setLname(),setEmail() and setAge() respectively. Now see our Register class in below.

As we have all the required methods, the Struts2 framework will execute all those methods before executing the actual action method, in our case register(). So after executing all the methods the control will go to register method. So all the variables are available now for operation.

In our action method we just displayed the values in the console, then returning success message as result name.
stdata3So as we mentioned the result page is success.jsp for string “SUCCESS” struts2 will load the page success.jsp to the user. See our success.jsp below.

What will happen for those tags like <s:property value ="fname"/> . Struts2 will look for get methods in the corresponding action class. As we have all get methods getEmail(), getLname(), getFname() and getAge() in the action class those will get executed and the value will be placed there.

stdata1 stdata2 For the above method I have created a complete .war file with source code. Download and just place under Apache web apps folder and run.

If you have any issues while execution please post in comments or send mail to rajeshmepco@gmail.com.

Posted in Struts2 | Tagged , | Leave a comment

Send JSON object to Struts 2 Action by JQuery AJAX

To send data from client side to server side, one of the best way is send as JSON object.
Please follow the steps below to achive this.

To do this first we have to enable json compatibility in the struts2 framework. For this modify the struts.xml file as below.

In the above code we added the json interceptor to the defaultStack. If you have your own stack add the lines below to your stack.

Next extend the json package to the default package as below

And the last change in the struts.xml file is set the return type as json for the request action.

That’s all the change for struts2.xml

In our example we are going to send data from the client side to server side. So we will create a json data object with javascript as below.

The JSON object syntax will be

In the above code, we created a variable dataObj which has only one variable data. But the variable “data” contains 4 objects. Each object has four variables namely “active”,”color”,”date”, “id” and “name”.

Now we will see how this is getting called in detail. First convert the created object into json object with the below code.

This line of code will convert the object to json string. If you want to know the difference before and after in detail display it in the console and view in the browser console window like below.

Then to send this data to the action write the javascript code below.

When we execute the above code, the struts framework will look the data object then it will find a variable with name “data”. So it will look for the method setData() method in the action class ReadJSON.java. If it is there it will run the code and try to parse the data.

See the action class ReadJSON.java below.

In the above code we have setData method. So it will try to put our JSON object as parameter for that method. But the variable “data” has array of objects as value. So we will create a java class with name Report and it will have “active”,”color”,”date”, “id” and “name” as it’s members. See below.

In Report.java we added getter and setter methods for each of the variables why because the struts framework by default will look for set method for each variable. As we mentioned the parameter as List<Report> data in the setData() method it will create an object of Report, then it will try to call setMethod for each variable like setColor(), setDate(),etc.,

That’s all. Now we will initiate the action from the html file as below.

To check the output see the output in the Eclipse console. In our example we just displayed in the console. If you want to process just replace those portion as per the need.

Download the complete eclipse project below.

Just import in eclipse by “Existing Project into Workspace” and run.

If you have any queries just send a mail to rajeshmepco@gmail.com.  We will solve the problem.

Posted in Struts2 | Tagged , , , | 15 Comments

Read JSON object from Struts 2 Action by JQuery AJAX

To read java objects from action class as JSON object please follow the steps as below.
To understand completely please read this article first.

Modify the struts.xml as mentioned in the post.
To read the json just add the return type as JSON in the struts.xml action as below.

When we put the above code, while calling the action from javascript it will call all the getter methods inside that class and bid as object then it will return to the call.

See the javascript code below.

When we execute the above code all the getters methods inside the action class will be invoked. (i.e) All the methods starting with word ‘get’ will be called. Apart from this the method that is mentioned in the struts.xml will also be called. In our example the method readJSON() will be called. No need to return any value. Just return the string “SUCCESS”. See the readJSON method in the below ReadJSON.java .

In the above code inside readJSON() method we are creating an object for Report class and assigned it to the local variable data. So when we call this action, this will set the data object and return success. As we mentioned the result type as json the getData methos will be called and the data object will be returned.

As mentioned in the javascript code we can access the data objects by the name like below.

To check the output see the output in the browser console. In our example we just displayed in the console. If you want to process just replace those portion as per the need.

Download the complete eclipse project below.

Just import in eclipse by “Existing Project into Workspace” and run.

If you have any queries just send a mail to rajeshmepco@gmail.com.  We will solve the problem.

Posted in Struts2 | Tagged , , , | 3 Comments

Install adobe flash player on Linux 6.5 for firefox

If your Firefox browser doesn’t play videos in youtube or any other flash content please follow the steps below to fix the issue.Go to adobe website https://get.adobe.com/flashplayer/ and download the flash player file with name ending .tar.gz.
flash

Extract the downloaded tar.gz file by tar -zxvf filename.tar.gz

linux

It will create a file called libflashplayer.so (See above screenshot). Then copy that file to users home directory/.mozilla/plugins folder.

linux2

Restart firefox.

Posted in Others | Tagged , | Leave a comment

Firebug Tutorial Part 1 – Introduction

Firebug is a free open source tool that allows debugging, editing and monitoring any website’s CSS, HTML, DOM and JavaScript. It can also be used for Performance analysis.

How to install firebug?

Installing on Firefox browser

Firebug is developed as a Firefox add-on. It can be added to Firefox browser as like other add-ons. The add-on can be downloaded from Firefox add-on page https://addons.mozilla.org/en-US/firefox/

Installing on Non-Firefox browsers

For non-Firefox browsers there is a lite version available to use. It can be downloaded from here.https://getfirebug.com/firebuglite

It can be imported like a normal script in any html page. See code below.

Once the firebug installed while pressing the F12 button you will get a window like this.

Continue reading

Posted in Firebug | Tagged , | Leave a comment
hello