reference : http://j2ee.masslight.com/Chapter7.html
//----------------------------------------------------------------------
//------------------------------------------------------------------------
package com.masslight.Employee;
import com.masslight.City.City;
import com.masslight.City.CityHome;
import com.masslight.Department.Department;
import com.masslight.Department.DepartmentHome;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public class EmployeeAddForm extends ActionForm {
private Integer employeeid = new Integer(0);
private String firstname = new String();
private String lastname = new String();
private String extension = new String();
private Integer cityid = new Integer(0);
private Integer departmentid = new Integer(0);
public EmployeeAddForm() {
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
// validate fields here
/*
try {
if (getEmployeeid().intValue() == 0) {
errors.add("employeeid", new ActionError("employee.employeeidrequired"));
}
Employee employee = new Employee();
employee.setEmployeeid(getEmployeeid());
if (employee.fetch() != false) {
errors.add("employeeid", new ActionError("employee.employeeidalreadyused"));
}
} catch (Exception e) {
errors.add("employeeid", new ActionError("employee.employeeidrequired"));
}
if (getFirstname() == null || getFirstname().length() < 1) {
errors.add("firstname", new ActionError("employee.firstnamerequired"));
}
if (getLastname() == null || getLastname().length() < 1) {
errors.add("lastname", new ActionError("employee.lastnamerequired"));
}
*/
return errors;
}
public Integer getEmployeeid() { return employeeid; }
public void setEmployeeid(Integer value) { employeeid = value; }
public String getLastname() { return lastname; }
public void setLastname(String value) { lastname = new String(value); }
public String getFirstname() { return firstname; }
public void setFirstname(String value) { firstname = new String(value); }
public String getExtension() { return extension; }
public void setExtension(String value) { extension = new String(value); }
public Integer getDepartmentid() { return departmentid; }
public void setDepartmentid(Integer value) { departmentid = value; }
public Integer getCityid() { return cityid; }
public void setCityid(Integer value) { cityid = value; }
}
//----------------------------------------------------------------------
package com.masslight.Employee;
import com.masslight.City.City;
import com.masslight.City.CityHome;
import com.masslight.Department.Department;
import com.masslight.Department.DepartmentHome;
import java.util.Vector;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Locale;
import java.util.Hashtable;
import java.util.Collection;
import java.util.Properties;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.util.*;
import javax.rmi.PortableRemoteObject;
import javax.naming.*;
public final class EmployeeAddSetupAction extends Action {
public EmployeeAddSetupAction() {
}
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
Locale locale = getLocale(request);
MessageResources messages = getResources();
HttpSession session = request.getSession();
ActionErrors errors = new ActionErrors();
// insert logic here
Properties env = new Properties();
env.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
env.setProperty("java.naming.provider.url", "localhost:1099");
env.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
try {
InitialContext jndiContext = new InitialContext(env);
Object ref = jndiContext.lookup("City");
CityHome home = (CityHome)PortableRemoteObject.narrow(ref, CityHome.class);
Collection cities = home.findAll();
session.setAttribute("cities", cities);
} catch (Exception e1) {
return (mapping.findForward("employeeaddsetupfailure"));
}
try {
InitialContext jndiContext = new InitialContext(env);
Object ref = jndiContext.lookup("Department");
DepartmentHome home = (DepartmentHome)PortableRemoteObject.narrow(
ref, DepartmentHome.class);
Collection departments = home.findAll();
session.setAttribute("departments", departments);
} catch (Exception e2) {
return (mapping.findForward("employeeaddsetupfailure"));
}
if (mapping.getAttribute() != null) {
if ("request".equals(mapping.getScope()))
request.removeAttribute(mapping.getAttribute());
else
session.removeAttribute(mapping.getAttribute());
}
return (mapping.findForward("employeeaddsetupsuccess"));
}
}
//------------------------------------------------------------------------package com.masslight.Employee;
import com.masslight.City.City;
import com.masslight.City.CityHome;
import com.masslight.Department.Department;
import com.masslight.Department.DepartmentHome;
import java.util.Vector;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Locale;
import java.util.Hashtable;
import java.util.Properties;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.util.*;
import javax.rmi.PortableRemoteObject;
import javax.naming.*;
public final class EmployeeDeleteAction extends Action {
public EmployeeDeleteAction() {
}
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
Locale locale = getLocale(request);
MessageResources messages = getResources();
HttpSession session = request.getSession();
EmployeeDeleteForm employeeDeleteForm = (EmployeeDeleteForm)form;
ActionErrors errors = new ActionErrors();
// insert logic here
Properties env = new Properties();
env.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
env.setProperty("java.naming.provider.url", "localhost:1099");
env.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
try {
InitialContext jndiContext = new InitialContext(env);
Object ref = jndiContext.lookup("Employee");
EmployeeHome home = (EmployeeHome)PortableRemoteObject.narrow(ref, EmployeeHome.class);
Employee employee = home.findByPrimaryKey(employeeDeleteForm.getId());
employee.remove();
} catch (Exception e) {
return (mapping.findForward("employeedeletefailure"));
}
if (mapping.getAttribute() != null) {
if ("request".equals(mapping.getScope()))
request.removeAttribute(mapping.getAttribute());
else
session.removeAttribute(mapping.getAttribute());
}
return (mapping.findForward("employeedeletesuccess"));
}
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------------package com.masslight.Employee;
import com.masslight.City.City;
import com.masslight.City.CityHome;
import com.masslight.Department.Department;
import com.masslight.Department.DepartmentHome;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public class EmployeeDeleteForm extends ActionForm {
private Integer id;
public EmployeeDeleteForm() {
}
public Integer getId() { return id; }
public void setId(Integer value) { id = value; }
public void reset(ActionMapping mapping, HttpServletRequest request) {
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
// validate fields here
return errors;
}
//---------------------------------------------------------------------package com.masslight.Employee;
import com.masslight.City.*;
import com.masslight.Department.*;
import java.util.Vector;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Locale;
import java.util.Hashtable;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.util.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;
import javax.naming.*;
import java.util.Collection;
public final class EmployeeViewAction extends Action {
public EmployeeViewAction() {
}
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
Locale locale = getLocale(request);
MessageResources messages = getResources();
HttpSession session = request.getSession();
ActionErrors errors = new ActionErrors();
// insert logic here
Properties env = new Properties();
env.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
env.setProperty("java.naming.provider.url", "localhost:1099");
env.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
try {
InitialContext jndiContext = new InitialContext(env);
Object ref = jndiContext.lookup("Employee");
EmployeeHome home = (EmployeeHome)PortableRemoteObject.narrow(ref, EmployeeHome.class);
Collection employees = home.findAll();
session.setAttribute("employees", employees);
} catch (Exception e) {
return (mapping.findForward("employeeviewfailure"));
}
if (mapping.getAttribute() != null) {
if ("request".equals(mapping.getScope()))
request.removeAttribute(mapping.getAttribute());
else
session.removeAttribute(mapping.getAttribute());
}
return (mapping.findForward("employeeviewsuccess"));
}
}
}
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
<struts-config>
<!-- ========== Form Bean Definitions =================================== -->
<form-beans>
<form-bean name="employeeModifySetupForm"
type="com.masslight.Employee.EmployeeModifySetupForm"/>
<form-bean name="employeeModifyForm" type="com.masslight.Employee.EmployeeModifyForm"/>
<form-bean name="employeeAddForm" type="com.masslight.Employee.EmployeeAddForm"/>
<form-bean name="employeeDeleteForm" type="com.masslight.Employee.EmployeeDeleteForm"/>
</form-beans>
<!-- ========== Global Forward Definitions ============================== -->
<global-forwards>
<forward name="employeeaddsetupsuccess" path="/employeeadd.jsp"/>
<forward name="employeeaddsuccess" path="/employeeaddsuccess.jsp"/>
<forward name="employeeaddfailure" path="/employeeaddfailure.jsp"/>
<forward name="employeeviewsuccess" path="/employeeviewsuccess.jsp"/>
<forward name="employeeviewfailure" path="/employeeviewfailure.jsp"/>
<forward name="employeedeletesuccess" path="/employeeview.do"/>
<forward name="employeedeletefailure" path="/employeedeletefailure.jsp"/>
<forward name="employeemodifysetupsuccess" path="/employeemodify.jsp"/>
<forward name="employeemodifysuccess" path="/employeeview.do"/>
<forward name="employeemodifyfailure" path="/employeemodifyfailure.jsp"/>
<forward name="cityviewsuccess" path="/cityviewsuccess.jsp"/>
<forward name="cityviewfailure" path="/cityviewfailure.jsp"/>
</global-forwards>
<!-- ========== Action Mapping Definitions ============================== -->
<action-mappings>
<action path="/employeeaddsetup" type="com.masslight.Employee.EmployeeAddSetupAction"/>
<action path="/employeeadd" type="com.masslight.Employee.EmployeeAddAction"
name="employeeAddForm" scope="request" input="/employeeadd.jsp"/>
<action path="/employeeview" type="com.masslight.Employee.EmployeeViewAction"/>
<action path="/employeedelete" type="com.masslight.Employee.EmployeeDeleteAction"
name="employeeDeleteForm" scope="request" input="employeeviewsuccess.jsp"/>
<action path="/employeemodifysetup"
type="com.masslight.Employee.EmployeeModifySetupAction"
name="employeeModifySetupForm" scope="request" input="/employeeviewsuccess.jsp"/>
<action path="/employeemodify" type="com.masslight.Employee.EmployeeModifyAction"
name="employeeModifyForm" scope="request" input="/employeemodify.jsp"/>
<action path="/cityview" type="com.masslight.City.CityViewAction"/>
</action-mappings>
//-------------------------------------------------------------------------------<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<!-- Action Servlet Configuration -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>ApplicationResources</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.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>
<init-param>
<param-name>validate</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- The Welcome File List -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Application Tag Library Descriptor -->
<taglib>
<taglib-uri>/WEB-INF/application.tld</taglib-uri>
<taglib-location>/WEB-INF/app.tld</taglib-location>
</taglib>
<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
</web-app>
</struts-config>