Friday, September 21, 2012

How to create a Struts,Spring,Hibernate project


Sometime you all know about row J2EE. But when you are developing web based application according to MVC you should need to know about some kind of frameworks because those are so easy for developing and maintaining which are Strut, spring, EJB and Hibernate and etc.

Following diagram show the overall idea about where this frameworks can be used in your project.


 At first,I would like to tell you how to create struts project. 

Strut project

Why we use strut and where we use ? actually Strut is running according to url patterns. It can handle the all the requests and redirect this request according to its rules. Strut is worked as a controller. Probably this request sends to business logic which means EJB finally it know where this request need to go.

Before developing application it needs some library and there are so many struts version but I plan to use 2.06 versions. In this version there is not strut file because all are defined in web XML.
  1.   Create a dynamic web project in your eclipse.
  2.    Add the needed library for this project.
  3.   Change the index.jsp file like this.


This test.action is the url pattern and when this button click this request is going to web.xml and It is defined where the action classes are according to requests.

    4. Add the following code in to web xml.

      In web xml 



If request are coming with “ *.action ” pattern check the filter name in filter mapping and then go to filter again check the filter name and load the action classes to memory finally get the prefix of request pattern and check the functions in equal prefix pattern name class.  

If there are more than one packages of action classes add package with “,” in param value tag.

com.strut.test” is package of action classes.


      5. Create action class in com.strut.test package.




When action classes are created post fix of class name should be “Action” and prefix of class name depend of created request pattern of project.

According to my example this action class name should be “TestAction” This class is extended from “ServletRequestAware” and there are override methods which are
  1. void setServletRequest(HttpServletRequest request)
  2. String execute()

When request are come to this class assigned to request variable and then execute the execute function. In execute function, identify the which one is click and send it to business logic and get the response from that request and redirect to another jsp page or java Servlet.

Return String this execute function and define the String pattern according to string inside the action class where this request needs to redirect.



If there are more return String, you can add another result with “,” inside the Results like this.



Then this request is going to web xml file check the string pattern for showing results.


      6. Add the following codes in side web xml.




Finally check the string pattern “/test” and get the servlet name inside servlet mapping then go to find equal servlet name inside the servlet then find destination.(jsp page ,It may be change according to requirement ).







1 comment:

  1. Thank you for sharing the excellent post about JAVA programming. you helped me to gain more information on the JAVA language.
    spring training in chennai | java training in velachery

    ReplyDelete