Step 1: develop a index.jsp form

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:form action="r">
    <html:errors/>
    Enter Name:<html:text property="name"/><br>
    Enter address:<html:textarea property="address"/><br>
    Enter City:<html:text property="city"/><br>
    <html:submit/>
   
</html:form>

Step 2: Develop a Form bean class and override validate and reset methods for validations

package com.dustbin.formbean;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class StrudentForm extends org.apache.struts.action.ActionForm {
   
    private String name;
   private String address;
   private String city;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
 
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (getName() == null || getName().length() < 1) {
            errors.add("name", new ActionMessage("error.name.required"));
            // TODO: add 'error.name.required' key to your resources
        }
       
         if (getAddress() == null || getAddress().length() < 1) {
            errors.add("address", new ActionMessage("error.address.required"));
            // TODO: add 'error.name.required' key to your resources
        }
       
         if (getCity() == null || getCity().length() < 1) {
            errors.add("city", new ActionMessage("error.city.required"));
            // TODO: add 'error.name.required' key to your resources
        }
        return errors;
    }
}

Step 3: Configure a class into struts-config.xml file like

<form-beans>
        <form-bean name="StrudentForm" type="com.dustbin.formbean.StrudentForm"/>
    </form-beans>

Step 4: develop action class and configuring a struts-config.xml file

package com.dustbin.formbean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class NewStrutsAction extends org.apache.struts.action.Action {


    private static final String SUCCESS = "success";

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
       
        StrudentForm sf=(StrudentForm)form;
        String n=sf.getName();
         String a= sf.getAddress();
         String c= sf.getCity();
       
         request.setAttribute("n",n);
         request.setAttribute("n1",a);
         request.setAttribute("n2",c);
        return mapping.findForward(SUCCESS);
    }
}

and confiring into struts-config.xml file and using global-forward tag

<global-forwards>
        <forward name="success" path="/success.jsp"/> 
        <forward name="welcome"  path="/Welcome.do"/>
    </global-forwards>

    <action-mappings>
        <action input="/index.jsp" name="StrudentForm" path="/r" validate="true" scope="request" type="com.dustbin.formbean.NewStrutsAction">
            
            </action>
        
        <action path="/Welcome" forward="/welcomeStruts.jsp"/>
    </action-mappings>

Step 6: develop a success for name success.jsp

Hi!&nbsp;&nbsp;<font color="red">
<%=request.getAttribute("n")%></font>Your Registration Details are <br><br>
<%=request.getAttribute("n1")%>
<%=request.getAttribute("n2")%><br><br>

Click Here<a href="index.jsp">Register</a>

Output:







Download for application with jar files Click Here (netbeans)

0 comments:

Post a Comment