Thursday, June 30, 2011

struts2+Spring workflow

No comments:

struts2+Spring workflow

1. An initial request goes to the Servlet container (such as Jetty or Resin) which is passed through a standard filter chain.

2. the required FilterDispatcher is called, which in turn consults the ActionMapper to determine if the request should invoke an action.


3. The ActionMapper interface provides a mapping between HTTP requests and action invocation requests and vice-versa.

When given an HttpServletRequest, the ActionMapper may return null if no action invocation request matches, or it may return an ActionMapping that describes an action invocation for the framework to try.

If the ActionMapper determines that an Action should be invoked, the FilterDispatcher delegates control to theActionProxy.

4. The ActionProxy consults the framework Configuration Files manager (initialized from the struts.xml file).

5. the ActionProxy creates an ActionInvocation, which is responsible for the command pattern implementation. This includes invoking any Interceptors (the before clause) in advance of invoking the Action itself.

6. Once the Action returns, the ActionInvocation is responsible for looking up the proper result associated with the Action result code mapped in struts.xml.

7. The result is then executed, which often (but not always, as is the case for Action Chaining) involves a template written in JSP or FreeMarker to be rendered.


8. While rendering, the templates can use the Struts Tags provided by the framework.

9. All objects in this architecture (Actions, Results, Interceptors, and so forth) are created by anObjectFactory.

This ObjectFactory is pluggable. We can provide our own ObjectFactory
A popular ObjectFactory implementation uses Spring as provided by the Spring Plugin.

Normally, in struts.xml you specify the class for each Action. When using the default SpringObjectFactory, the framework will ask Spring to create the Action and wire up dependencies as specified by the default auto-wire behavior.


10. Interceptors are executed again (in reverse order, calling the after clause).


11. Finally, the response returns through the filters configured in the web.xml.


N.B:
don't know how to setup simple struts2 project? see simple setup
Read More

How to import NetBeans project into Eclipse

No comments:

How to import NetBeans project into Eclipse

netbeansFew days ago, when I faced some issues with NetBeans IDE, I switched to Eclips. Now the problem was my work. Placing java files from one place to another seems easy. But preparing configuration file is really time-consuming. And as usual I avoid manual work. SO let's see what I did to import NetBeans workspace into Eclipse without wasting time
1


  1. There is a dist folder inside NetBeans workspace, containing a .war file. Import this war file into Eclipse. For this;
    1. Open eclips IDE > Go to File Menu => Import => War
  2. Above step will make package structure in Eclipse workspace similar to NetBeans. It'll also prepare configuration file. Now you need to place source files only. For this
    1. Go to src folder inside NetBeans folder => Copy all contents => Place them to src folder inside NetBeans folder.
    2. Right click on Project name in right side panel of Eclipse IDE => Refresh

Your Eclipse workspace is prepared. If above trick doesn't work for you refer another way;

2


OVERVIEW OF THE PROCESS

  1. Lets say you already have a NetBeans project created called MyProjectand located at <PATH>\MyProject
  2. open NetBeans and change the project settings
  3. create a ZIP file of the <PATH>\MyProject\MyProject.zip
  4. create a new folder <PATH>\NEW (or your choice), this is where we are going to create the Eclipse Project
  5. open Eclipse create a new project and import the ZIP file
  6. create a new folder <PATH>\NEW\MyProject\dist(necessary forNetBeans)
  7. Now you can DECIDE when to work with Eclipse or NetBeans

DETAILED PROCESS

  1. Let's say you already have a NetBeans project created called MyProjectand located at <PATH>\MyProject
  2. open NetBeans
    • open MyProject
    • Right click MyProject and select Clean Project (this deletes thebuild/classed and dist folders)
    • open the file project.properties located in the folder<PATH>\MyProject\nbproject
    • change the build.dir to bin
    • change the build.classed.dir to ${build.dir}
    • save and close the file

close NetBeans

  1. create a ZIP file of the <PATH>\MyProject\MyProject.zip that will contain this folders:
    • nbproject
    • src
    • Any other folder or file that is located at <PATH>\MyProject
  2. create a new folder <PATH>\NEW (or your choice), this is where we are going to create the Eclipse Project
  3. open Eclipse
    • go to Window, Open Perspective, Java
    • go to File, New, Project, Java Project
    • click NEXT
    • type MyProject
    • select Create project at external location
    • type <PATH>\NEW\MyProject (this folder will be created by ECLIPSE)
    • select Create separate source and output folders
    • click NEXT
    • click FINISH
    • Right click on MyProject (at the Package Explorer)
    • select IMPORT
    • select ZIP file (usually the last option on the list)
    • click NEXT
    • BROWSE and look for the <PATH>\MyProject\MyProject.zip created onSTEP 3
    • click FINISH

close Eclipse

  1. create a new folder <PATH>\NEW\MyProject\dist(necessary forNetBeans)
  2. Now you can DECIDE when to work with Eclipse or NetBeans



Read More

Choosing a date time picker UI – jQuery

No comments:

Choosing a date time picker UI – jQuery

by VEERA on JUNE 19, 2011

in WEB

I had a requirement to add an input control for users to select date time range. User will be selecting two values – start time and end time. For both the values, the date is same. Only the time differs. For example, the values could be 6/19/2011 10:30 to 6/19/2011 11:30.

I am using jQuery UI library for my web-app. So, I started searching for a date time picker widget with an option to choose a time range. The built-indatepicker does not have the ability to choose a time. But thanks to Trent, there's a plug-in I found to add timepicker to jQuery datepicker. With that, I came up with the design which initially looked like this:

datetime picker ui - jquery

The control functioned as I expected. I deployed the app with this UI and tried it for a week to find out if there's an usability issue. And, yes! There was some difficulties I found while using this date time picker.

  1. It was too many clicks – one to choose a date, one to choose a hour and one to choose the minutes. Additionally, there's a second input date too for which I had to repeat these clicks again – so a total of six clicks just to enter two input values. Not good!
  2. On the time picker slider, the hour and minute slides are disconnected. Means, you need to separately choose these two. I found this a lot more confusing. Also, while in a hurry, this confusion will lead to dissatisfaction with my application. Again, not good.

So, I decided to come up with some other way to enter the start time and end time. After googling a while and self brainstorming, I decided to go with a single date control and single time control. Hence, my current UI looks like this:

date time picker - improved ui - jQuery

Now it is a lot more simplified – just a textbox and a slider. And, the number of clicks user has to make is just three (compare it with the six of previous approach). It looks more elegant and fit too on my form.

So, now a question to you – which datetime picker UI would you prefer?First one or the second one?


--
Thank to http://veerasundar.com/




Read More

How to create Struts2 project in NetBeans

No comments:

How to create Struts2 project in NetBeans

If you decide to start learning and using struts2, probably one of the first things you think about is which IDE to use, and what you need in order to create and run a Struts2 project...

If you search the web , you'll find many tutorials on how to create a struts2 project, and start developing.
This is what I did when I was starting, and soon find out that nothing I found on the web worked for me in proper manner...

So, I decided to create a small tutorial on how to create an empty struts2 project , in order to help others, and save their time.
Not to teach you Struts2, but to help you create a working Struts2 project!

Ok, I will use NetBeans 6.5, and a Tomcat 6 that comes with it...

First of all, go to http://struts.apache.org/2.x/index.html and download the latest version of Struts2.
I downloaded struts-2.1.6-all.zip
You won't be needing all the libraries in it. Just 6 of them. Because of this, you can download essential dependencies only.

When you download one of these files, extract it to some location, for example: c:\Struts2
It is now time to start a new project. Start NetBeans.

Click File -> New project -> Java web -> Web application

 

Let's name our project "Struts2Example". Click "next".
Choose which server you wish to use ( I selected Tomcat 6 ), set JavaEE to version 5, and leave ContextPath to be /Struts2Example

You can click "finish" now.
If you click "next" DO NOT, I repeat DO NOT choose available ( older version ) of struts under "frameworks".

Your project tree will look this:




Now, in order to use Struts2 libraries, let's add those essential dependencies to project:

Right click on "Libraries" folder and then click "Add JAR/folder". A dialog will open.
Go to folder where you extracted Struts2 libraries and select ( add to project ) following files:

- struts2-core-2.1.6.jar
- freemarker-2.3.13.jar
- ognl-2.6.11.jar
- commons-fileupload-1.2.1.jar
- commons-logging-1.0.4.jar
- xwork-2.1.2.jar


Most of the tutorials on the web are written for older version of struts2, at the time when commons-fileupload-1.2.1.jar was not mandatory, so even when following all the steps in tutorial , you just couldn't get your project to work ( with newer version of Struts2 ) !

Now, your project will loke like this:

 

Now, in the root of WEB-INF folder create a new folder , and name it "lib".
Copy these six JARs into this new folder.




These files will be uploaded to App Server , and a deployed application will use it.
We could put this files into main lib folder of application server, but it is most probably better this way...

We need to create one more folder in the root of the WEB-INF , and name it "classes".
This is where you will create struts.xml, and if needed - a struts.properties file.




I'll put all my JSP's into new folder, "jsp" , which I will create in the root of "Web Pages" folder.

All Java code that we write, we will place into some package in the "Source Packages" folder.
I'll create one now, called "Struts2Example" and a "Hello" Class in it.
If you use validaton, put all Validation XMLs inside class's package too...




This is how, at minimum, your Struts2 project structure should look like.


I'll create one small Struts2 application now. Nothing complicated.
User will enter his name, and press "submit" and other page will open , saying Hello to this user.

So, let's first create a Java class extending ActionSupport class , named hello, inside Struts2Example package.
Let's make it to look something like this:

package Struts2Example;

import com.opensymphony.xwork2.ActionSupport;

/**
*
* @author Darko
*/
public class Hello extends ActionSupport
{
private String name, message;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getMessage() {
return "Hello " + getName();
}

public void setMessage(String message) {
this.message = message;
}

}

We now need two JSPs, for example "nameinput.jsp" and a "response.jsp"
Each JSP needs to contain this line, in order to use struts2 custom tags in it:

<%@ taglib prefix="s" uri="/struts-tags" %>

nameinput.jsp:

 

response.jsp:

 

Let's create an XML validation file for Hello class, and make a name property mandatory. 
Hello-validation.xml:

<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>
<field name="name">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>Please enter name</message>
</field-validator>
</field>
</validators>

There's only 2 things left for us to do:

Edit struts.xml, and web.xml:


struts.xml:


web.xml:
 

and if you deploy this project to server, and open 
http://localhost:8084/Struts2Example/entername.action , you'll get something like this: 

 

If we do not enter name:

 

and if we do:

 

So, I hope I helped you to understand how to create an empty struts2 project in NetBeans.
At the end, our project looked like this:





There's more to it. For example, if you wish to override some theme's behaviour ( for example "simple" ) you create folders
template/theme_name ( for example template/simple ) and it you copy all the original files from this theme ( from struts2-core-X.X.X.jar ) and edit them in this folder...



But, some other time about that and some other usefull things...

Enjoy Struts2!

--
Thank you.


Regards,
Kaleeswaran.S



Read More

Eclipse Galileo and Struts2

No comments:

Eclipse Galileo and Struts2

I was quite surprised with how many people came to my blog and read my earlier post on how to create a blank Struts2 project and start using this framework in NetBeans 6.5

With a release of new Eclipse, I thought it would be interesting to do the same thing, but now in this IDE.
As you probably already know, Eclipse is one of the famous and certainly one of the best Java IDEs.
Well, about a month ago people from Eclipse foundation released a 3.5 version of Eclipse, called Galileo.

I develop almost all my project using NetBeans, and have never done any Struts2 programming using Eclipse.
That is why I decided to write a post on how to create a Struts2 example in new Eclipse release: Galileo. That's quite fun because that way I (we) get to learn on how to use Struts2 in Eclipse, and get to see Galileo in action a little bit closer…

Note: In this post, I'll use much of the text , entire source code and application logic from my previous post and concetrate only on the differences between these two IDEs. 


So, let's get started:

First of all we need Eclipse Galileo. Go to the http://www.eclipse.org/downloads/ and download Eclipse IDE for Java EE Developers.

Next thing we need is Struts2 itself. Go to http://struts.apache.org/2.x/index.html and download the latest version of Struts2.
I downloaded struts-2.1.6-all.zip
You won't be needing all the libraries in it. Just 6 of them. Because of this, you can download essential dependencies only.

When you download one of these archives, extract it to some location, for example: c:\Struts2

It is now time to start a new project. Start Eclipse Galileo.


Click file --> new --> Dynamic web project



Let's call this project GalileoStruts2Example
I'll use Tomcat 6 . You can use some other server that you have available , it is completely up to you.






Click Finish.

Your project tree will look like this:







Now, in order to use Struts2 libraries, let's add those essential dependencies to project:

Right-click on the project --> properties --> Java Build Path , select Libraries tab and click on the "Add External JARs" button.

Go to folder where you extracted Struts2 libraries and select ( add to project ) following files:

- struts2-core-2.1.6.jar
- freemarker-2.3.13.jar
- ognl-2.6.11.jar
- commons-fileupload-1.2.1.jar
- commons-logging-1.0.4.jar
- xwork-2.1.2.jar


Most of the tutorials on the web are written for older version of struts2, at the time when commons-fileupload-1.2.1.jar was not mandatory, so even when following all the steps in tutorial , you just couldn't get your project to work ( with newer version of Struts2 ) !

Now, your project will look like this:



Now, copy these six JARs into the "lib" folder located in the root of WEB-INF folder.






These files will be uploaded to App Server , and a deployed application will use it.
We could put this files into main lib folder of application server, but it is most probably better this way...

We need to create one more folder in the root of the WEB-INF , and name it "classes".
This is where you will create struts.xml, and if needed - a struts.properties file.

I'll put all my JSP's into new folder, "jsp" , which I will create in the root of "WebContent" folder.

All Java code that we write, we will place into some package in the "Java Resources: src" folder.
I'll create one now, called "struts2Example" and a "Hello" Class in it.
If you use validaton, put all Validation XMLs inside class's package too...






This is how, at minimum, your Struts2 project structure should look like.


I'll create one small Struts2 application now. Nothing complicated.
User will enter his name, and press "submit" and other page will open , saying Hello to this user.

So, let's first create a Java class extending ActionSupport class , named Hello, inside Struts2Example package.
Let's make it to look something like this:

package struts2Example;
import com.opensymphony.xwork2.ActionSupport;


public class Hello extends ActionSupport
{
private String namemessage;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return "Hello " + getName();
}
public void setMessage(String message) {
this.message = message;
}
}

We now need two JSPs, for example "nameinput.jsp" and a "response.jsp"
Each JSP needs to contain this line, in order to use struts2 custom tags in it:

<%@ taglib prefix="s" uri="/struts-tags" %>

nameinput.jsp:
 

response.jsp:

 

Let's create an XML validation file for Hello class, and make a name property mandatory.

Hello-validation.xml:

<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>
<field name="name">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>Please enter name</message>
</field-validator>
</field>
</validators>


Let's see how our project tree looks at this stage:

 

There's only 2 things left for us to do:

Edit struts.xml, and web.xml:

struts.xml must be located in "classes" folder located in the root of WEB-INF folder:




 

This is where place a file struts.properties if we need to use it, as well…

web.xml:






And if we press "Run" button, and choose a server on which it is supposed to run


 

we'll get something looking like this:




But don't worry. Everything is OK, we just didin't handle default page...
If we append entername.action on the URL , our new application will open:

 
If we do not enter name and press "submit":

 

but if we do enter name and press "Submit":

 

So, I hope I helped you to understand how to create an empty struts2 project in Eclipse Galileo.

At the end, our project looked like this:


 


A small conclusion of mine: great thing about Eclipse is that you can do just about everything with it , with just a little few extra settings and installations...
Unfortunately, for me - many things and options remained a little bit more complicated and unintuitive regarding to some other IDEs. That is such a shame!
Nevertheless , for me , Eclipse continues to be one of the best choices!.

Enjoy Struts2 and Galileo! 

--
Thanks to .



Read More