what is Switch Action: 

The SwitchAction class provides a means to switch from a resource in one module to another resource in a different module. SwitchAction is useful only if you have multiple modules in your Struts application and maintain multiple struts-config.xml. The SwitchAction class can be used as is, without extending.  

Step1: developa index.jsp form and write the code like..

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:link action="switch?prefix=/student&page=/student.jsp">Student</html:link>&nbsp;&nbsp;
<html:link action="switch?prefix=/college&page=/college.jsp">College</html:link>&nbsp;&nbsp;&nbsp;
<html:link action="switch?prefix=/company&page=/company.jsp">Company</html:link>

Step2:  develop a student module
a).create Action class for Student and create FormBean object in action class

package com.dustbin.student.controller;
import com.dustbin.student.formbean.StudentFormBean;
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;

public class StudentAction 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 {
        StudentFormBean sfb=(StudentFormBean)form;
        return mapping.findForward(SUCCESS); }}

 struts-config.xml file

<action-mappings>
 
<action path="/switch" scope="request" type="org.apache.struts.actions.SwitchAction"/>
</action-mappings>


Configuring the action class and formbean class into struts-config-student.xml file
<struts-config>
    <form-beans>
    <form-bean name="stu" type="com.dustbin.student.formbean.StudentFormBean"/>
    </form-beans>
<action-mappings>
        <action path="/student" name="stu" type="com.dustbin.student.controller.StudentAction">
            <forward name="success" path="/success.jsp"/>
            </action>
        <action path="/Welcome" forward="/welcomeStruts.jsp"/>
    </action-mappings>
    </struts-config>

And this struts-config-student.xml ,struts-config-college.xml and struts-config-company.xml file configured into web.xml as shown below

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <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>
<init-param>
            <param-name>config/company</param-name>
            <param-value>/WEB-INF/struts-config-company.xml</param-value>
        </init-param>
<init-param>
            <param-name>config/student</param-name>
            <param-value>/WEB-INF/struts-config-student.xml</param-value>
        </init-param>
<init-param>
            <param-name>config/college</param-name>
            <param-value>/WEB-INF/struts-config-college.xml</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>2</param-value>
        </init-param>
        <init-param>
            <param-name>detail</param-name>
            <param-value>2</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
        </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
        </web-app>


b).developa formbean class for form

package com.dustbin.student.formbean;
import com.dustbin.college.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 StudentFormBean extends org.apache.struts.action.ActionForm {
 private String sid;
 private String sname;
 private String address;
public String getAddress() {
        return address;
    }public void setAddress(String address) {
        this.address = address;
    }
public String getSid() {
        return sid;
    }
public void setSid(String sid) {  this.sid = sid; }
public String getSname() {
        return sname;
    }public void setSname(String sname) {
        this.sname = sname;
    }

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (getSname() == null || getSname().length() < 1) {
            errors.add("cname", new ActionMessage("error.name.required"));
            // TODO: add 'error.name.required' key to your resources
        }
        return errors;
    }}

c).develop a student form student.jsp .it was created in student folder must.

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<h3>Student Form</h3><br>
<html:form action="student">
   Student ID:<html:text property="sid"/><br><br>
    Student Name:<html:text property="sname"/><br><br>
   Address:<html:text property="address"/><br><br>
 <html:submit></html:submit></html:form>

d).success.jsp file for student success

success student...

step 3:develop a college module
a).Action class for college 

package com.dustbin.college.controller;
import com.dustbin.college.formbean.CollegeFormBean;
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;
public class CollegeAction 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 {
        CollegeFormBean cfb=(CollegeFormBean)form;
        return mapping.findForward(SUCCESS);
    }
}
Configuration about college action and formbean class into struts-config-college.xml  

<struts-config>
    <form-beans>
    <form-bean name="c" type="com.dustbin.college.formbean.CollegeFormBean"/>
    </form-beans>
<action-mappings>
        <action path="/college" name="c" type="com.dustbin.college.controller.CollegeAction">
            <forward name="success" path="/success.jsp"/>
            </action> <action path="/Welcome" forward="/welcomeStruts.jsp"/>
    </action-mappings>
</struts-config>
   

Note:already struts-config-college.xml  confired into web.xml
b).develop a formbean class for college

package com.dustbin.college.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 CollegeFormBean extends org.apache.struts.action.ActionForm {
private String cid;
    private String cname;
private String address;
 public String getAddress() {
        return address;
    } public void setAddress(String address) {
        this.address = address;
    }
public String getCid() {
        return cid;
    } public void setCid(String cid) {
        this.cid = cid;
    } public String getCname() {
        return cname;
    }public void setCname(String cname) {
        this.cname = cname;
    }public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (getCname() == null || getCname().length() < 1) {
            errors.add("sname", new ActionMessage("error.name.required"));
            // TODO: add 'error.name.required' key to your resources
        }
        return errors;}}
c).develop a college form college.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<h3>College Form</h3><br>
<html:form action="college">
   College ID:<html:text property="cid"/><br><br>
    College Name:<html:text property="cname"/><br><br>
   Address:<html:text property="address"/><br><br>
<html:submit></html:submit></html:form>

d).success page for college form

success college...

step3:develop a company module
a)company module action class

package com.dustbin.company.controller;
import com.dustbin.company.formbean.CompanyFormBean;
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;
public class CompanyAction 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 {
        CompanyFormBean cfb=(CompanyFormBean)form;
        return mapping.findForward(SUCCESS);
    }
}
b).develop a form bean class for company 

package com.dustbin.company.formbean;
import com.dustbin.college.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 CompanyFormBean extends org.apache.struts.action.ActionForm {
private String cid;
    private String cname;
private String address;
  public String getAddress() {
        return address;
    } public void setAddress(String address) {
        this.address = address;
    }
public String getCid() {
        return cid;
    }public void setCid(String cid) {
        this.cid = cid;
    }
public String getCname() {
        return cname;
    }public void setCname(String cname) {
        this.cname = cname;
    }
       public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (getCname() == null || getCname().length() < 1) {
            errors.add("cname", new ActionMessage("error.name.required"));
            // TODO: add 'error.name.required' key to your resources
        }
        return errors;
    }}

Configuration about company action and formbean class into struts-config-company.xml
 
<struts-config>
    <form-beans>
    <form-bean name="com" type="com.dustbin.company.formbean.CompanyFormBean"/>
    </form-beans>
<action-mappings>
<action path="/company" name="com" type="com.dustbin.company.controller.CompanyAction">
                <forward name="success" path="/success.jsp"/>
                </action>
        <action path="/Welcome" forward="/welcomeStruts.jsp"/>
    </action-mappings>
    </struts-config>

Note:already struts-config-company.xml  confired into web.xml 

c).develop a company form name company.jsp
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<h3>Company Form</h3><br>
<html:form action="company">
   Company ID:<html:text property="cid"/><br><br>
    Company Name:<html:text property="cname"/><br><br>
   Address:<html:text property="address"/><br><br>
<html:submit></html:submit>
</html:form>

d).success page for company form

success company...


 Output:
This is style of creating a .jsp programs like fig..student module jsp's are placed into student folder..simillarly college and company....










CLICK HERE FOR WAR FILE  DOWNLOAD
CLICK HERE FOR RAR FILE  DOWNLOAD


0 comments:

Post a Comment