Tuesday, 19 April 2011

Creating struts application in eclipse

http://viralpatel.net/blogs/2008/12/tutorial-creating-struts-application-in-eclipse.html
Note: If you looking for tutorial “Create Struts2 Application in EclipseClick here.
In this tutorial we will create a hello world Struts application in Eclipse editor. First let us see what are the tools required to create our hello world Struts application.
  1. JDK 1.5 above (download)
  2. Tomcat 5.x above or any other container (Glassfish, JBoss, Websphere, Weblogic etc) (download)
  3. Eclipse 3.2.x above (download)
  4. Apache Struts JAR files:(download). Following are the list of JAR files required for this application.
    • struts.jar
    • common-logging.jar
    • common-beanutils.jar
    • common-collections.jar
    • common-digester.jar
We will implement a web application using Struts framework which will have a login screen. Once the user is authenticated, (s)he will be redirected to a welcome page.
Let us start with our first struts based web application.
Open Eclipse and goto File -> New -> Project and select Dynamic Web Project in the New Project wizard screen.

After selecting Dynamic Web Project, press Next.

Write the name of the project. For example StrutsHelloWorld. Once this is done, select the target runtime environment (e.g. Apache Tomcat v6.0). This is to run the project inside Eclipse environment. After this press Finish.
Once the project is created, you can see its structure in Project Explorer.

Now copy all the required JAR files in WebContent -> WEB-INF -> lib folder. Create this folder if it does not exists.
Next step is to create a servlet entry in web.xml which points to org.apache.struts.action.ActionServlet class of struts framework. Open web.xml file which is there under WEB-INF folder and copy paste following code.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
<servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>
            org.apache.struts.action.ActionServlet
        </servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

Here we have mapped url *.do with the ActionServlet, hence all the requests from *.do url will be routed to ActionServlet; which will handle the flow of Struts after that.
We will create package strutcures for your project source. Here we will create two packages, one for Action classes (net.viralpatel.struts.helloworld.action) and other for Form  beans(net.viralpatel.struts.helloworld.action).

Also create a class LoginForm in net.viralpatel.struts.helloworld.action with following content.

package net.viralpatel.struts.helloworld.form;
 
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
 
public class LoginForm extends ActionForm {
 
    private String userName;
    private String password;
 
    public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {
 
        ActionErrors actionErrors = new ActionErrors();
 
        if(userName == null || userName.trim().equals("")) {
            actionErrors.add("userName", new ActionMessage("error.username"));
        }
        try {
        if(password == null || password.trim().equals("")) {
            actionErrors.add("password", new ActionMessage("error.password"));
        }
        }catch(Exception e) {
            e.printStackTrace();
        }
        return actionErrors ;
    }
 
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

No comments:

Post a Comment