Steps to follow for developing Struts Application using Eclipse :-

We must have to follow some of the basic steps to develop any struts application using the Eclipse IDE.

1. Create Dynamic Web Project.

2. Copy all the 10 jars provided by the struts vendor to the WEB-INF/lib folder + 1 servlet-api.jar.

3. Copy all the 6 tld files to the WEB-INF folder ( not necessary )
    (Only when if developer want to use some pre-defined tags given by struts vendor in JSP page )

4. Copy struts-config.xml file to WEB-INF Folder. (struts-config is the default name,
                                                                                if we want we can change also ) .

5. Configure web.xml with "ActionServlet" class and init-param-value with " config "  and
     init-param-name with  " /WEB-INF/struts-config.xml " .

6. We need to create a form beam and a action class within a package named as " com.def.Struts ".

7. Now create a package  " com.def.Struts " under which place two java files.
     a) LoginForm.java ( Form bean class)       b) LoginAction.java (Action class )

8. Place one Properties file under " com.def.Struts " package which contain message regarding validation of        the form. ( message.properties  file ) --> Optional ( Only needed if validation is there ).

9. Mapping the struts-config file with action and form bean classes. How ?
    ( We will see in details later )

10. Now deploy the project and run successfully.

Files required for this simple Struts Project.

1. A Login jsp page ( login.jsp )
2. Login Success page ( home.jsp)
3. web.xml
4. struts-config.xml
5. LoginForm.java
6. LoginAction.java

login.jsp

<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<html> 
<body>

<html:form action="/loginSubmit">

UserName: <html:text property="name"></html:text><br/>
Password: <html:password property="password"></html:password><br/><br/>
<html:submit>Login</html:submit>

</html:form>

</body>
</html>

home.jsp


<html>
<body>

    Logged in successfully and Welcome to your home page..!

</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/
javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5">

  <display-name>Struts1</display-name>
 
<servlet>
  
  <servlet-name>login</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>login</servlet-name>
   <url-pattern>*.do</url-pattern>

</servlet-mapping>
  
</web-app>


struts-config.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//
DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/
struts/dtds/struts-config_1_1.dtd">

<struts-config>
    <form-beans>
 <form-bean name="loginForm" type="com.def.Struts.LoginForm"/>              </form-beans>
 
<action-mappings>
 <action path="/loginSubmit" 
  name="loginForm" 
  type="com.def.Struts.LoginAction" 
  input="/login.jsp">
    
 <forward name="success" path="/home.jsp"></forward>
 <forward name="failed" path="/login.jsp"></forward>
 </action>
 
</action-mappings>
    
</struts-config>

LoginForm.java

package com.def.Struts;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class LoginForm extends ActionForm{
 
 private String name;
 private String password;
 
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 
 
     public void reset(ActionMapping mp, HttpServletRequest req){
  
  this.name="java";
  this.password=null;
  
 }
}

LoginAction.java

package com.def.Struts;

import org.apache.struts.action.*;

import javax.servlet.http.*;

public class LoginAction extends Action{

 
public ActionForward execute(ActionMapping mp, ActionForm af,
  HttpServletRequest req, HttpServletResponse res)
   throws Exception {
  
 LoginForm lf=(LoginForm)af;
 String name=lf.getName();
 String pass=lf.getPassword();
 String result="";
 if(name.equals("sushant") && pass.equals("sushant")){
  result="success";
 }else{
  result="failed";
 }
    return mp.findForward(result);
  
 }
}





1 comment:

  1. Get your hands on the best convection microwave oven in India under 10000 wide range
    available. Buy now! best microwave convection oven in india

    ReplyDelete

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic