Hello World Web Application In Struts 2.x

Struts2Logo

 Hello World Web Application


1.  Create Dynamic Web Project:

In Eclipse go with File -> New -> Dynamic Web Project .It should be look like this:


 




2. Now give the name of project as HelloWordExample and give the Target runtime (Tomcat installation directory path if you are using eclipse workspace first time) , and leave the other options as it. It should be look like this:


 

Now click next -> next finally you will get  screen shot as below :


 

3. you can change content directory  name , checked Generate web.xml deployment descriptor ( by default it check ) , now click on finish.Finally your project directory structure is ready to use.Now select 



Windows -> Show View -> Navigator .  It should be look like this:

4. Now copy following jar file  to project's  WebContent ->WEB-INF ->lib  .





5.  web.xml is used to configure the servlet container properties of the HelloWorldExample application.  The filter and the filter-mapping elements are used to setup the Struts 2 Filter. Url-pattern map with filter-name and filter-name with filter-class , StrutsPrepareAndExecuteFilter , so all incoming request will be handled by  filter-class (Here  is StrutsPrepareAndExecuteFilter  ).Here Welcome file is index.jsp. Here is the code:

6. struts.xml,The Struts 2 Framework uses a configutration file (struts.xml) to initialize its own resources (like package, action class, interceptors etc).Here  struts.xml  file contains the following action mapping.

7. index.jsp is use for view and it is entry point of our application. Here i am using struts UI tags  because use of this tags are simple and powerful. taglib directive should be included if you want to use struts UI tags.create index.jsp inside WebContent folder.  Here is the code:

8.  HelloWorldAction.java (Action class) is used for Business Logic . Here i am creating  this file in src folder with package name com.ourtechspace.struts2 . This class will call when helloWorld action will call.  In the execute() method of the HelloWorldAction class we compose the message to be displayed . Here is the code:

9. helloWorld.jsp is use for displaying message which has construct in execute() method of HelloWoldAction class. Create  helloWorld.jsp inside WebContent folder. The s:property tag displays the value of HelloWorldAction class property helloMessage  through the getHelloMessage(). Here is the code:

10. Here is the directory structure  for  HelloWorldExample application .

11.  Now run the project, index page will  be displayed.

Enter the name and press submit button helloWorld.jsp output will be displayed.

Download Source Code 
Click here to download complete source code
Thank you for reading this tutorial.




Share on Google Plus

About JK STACK

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment