Struts BasicStrutsApplication Example By Using MyEclipse


Directory Structure for BasicStrutsApplication:

Capture

// LoginAction.java

package com.chennarapu.action;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import com.chennarapu.form.LoginForm;

public class LoginAction extends Action {

      public ActionForward execute(ActionMapping mapping, ActionForm form,

                  HttpServletRequest request, HttpServletResponse response) {

            LoginForm LoginForm = (LoginForm) form;

            return mapping.findForward(“success”);

      }

}

// LoginForm.java

package com.chennarapu.form;

import org.apache.struts.action.ActionForm;

public class LoginForm extends ActionForm {

      private String userName;

      private String password;

      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;

      }

}

// struts-config.xml

<?xml version=“1.0” encoding=“UTF-8”?>

<!–DOCTYPE struts-config PUBLIC “-//Apache Software Foundation//DTD Struts Configuration 1.2//EN” “http://struts.apache.org/dtds/struts-config_1_2.dtd”&gt;

<struts-config>

      <form-beans>

            <form-bean name=“LoginForm” type=“com.javaflow.form.LoginForm” />

      </form-beans>

      <action-mappings>

            <action attribute=“LoginForm” input=“/login.jsp” name=“LoginForm”

                  path=“/LoginAction” scope=“request” type=“com.javaflow.action.LoginAction”>

                  <forward name=“success” path=“/success.jsp” />

            </action>

      </action-mappings>

</struts-config>

// index.jsp

<%@ page language=“java” import=“java.util.*” pageEncoding=“ISO-8859-1”%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;

%>

<!–DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>

<html>

          <center>

                 <body>

                       This is my Login page. <br>

              </body>

       </center>

</html>

// login.jsp

<%@ page language=“java” pageEncoding=“ISO-8859-1”%>

<%@ taglib uri=http://struts.apache.org/tags-bean&#8221; prefix=“bean”%>

<%@ taglib uri=http://struts.apache.org/tags-html&#8221; prefix=“html”%>

<html>

      <head>

            <title>LoginForm </title>

      </head>

      <body>

            <html:form action=“/LoginAction”>

                  userName : <html:text property=“userName”/><html:errors property=“userName”/><br/>

                  password : <html:text property=“password”/><html:errors property=“password”/><br/>

                  <html:submit/><html:cancel/>

            </html:form>

      </body>

</html>

// success.jsp

Login Successfully