JSP PAGE :
=========
<%@ taglib uri="struts/bean-el" prefix="bean" %>
<%@ taglib uri="struts/html-el" prefix="html" %>
<%@ taglib uri="struts/logic-el" prefix="logic" %>
<%@ taglib uri="jstl/c" prefix="c" %>
<html>
<head>
<link href="<html:rewrite page="/rr.css" />" rel="stylesheet" type="text/css">
<title><bean:message key="title.employee.employeeform"/></title>
</head>
<body>
<c:choose>
<c:when test="${employeeForm.map.methodToCall == 'insert'}">
<h1><bean:message key="title.employee.insert"/></h1>
</c:when>
<c:otherwise>
<h1><bean:message key="title.employee.update"/></h1>
</c:otherwise>
</c:choose>
<logic:messagesPresent>
<span id="errorsHeader"><bean:message key="errors.validation.header"/></span>
<html:messages id="error">
<li><c:out value="${error}"/></li>
</html:messages>
<hr>
</logic:messagesPresent>
<logic:messagesPresent message="true">
<html:messages id="message" message="true">
<span id="success"><c:out value="${message}"/></span><br>
</html:messages>
</logic:messagesPresent>
<html:form action="employeeAction" focus="name">
<table>
<tr>
<td >Name:</td>
<td><html:text property="name"/></td>
</tr>
<tr>
<td>Age:</td>
<td><html:text property="age"/></td>
</tr>
<tr>
<td>Department:</td>
<td>
<html:select name="employeeForm" property="department">
<html:options collection="departments" property="id" labelProperty="description"/>
</html:select>
</td>
</tr>
<tr>
<td>Favorite ice cream flavors:</td>
<td>
<c:forEach var="flavor" items="${flavors}">
<html:multibox name="employeeForm" property="flavorIDs">
<c:out value="${flavor.flavorID}"/>
</html:multibox>
<c:out value="${flavor.description}"/>
</c:forEach>
</td>
</tr>
</table>
<html:hidden name="employeeForm" property="methodToCall"/>
<c:choose>
<c:when test="${employeeForm.map.methodToCall == 'insert'}">
<html:submit><bean:message key="button.submit"/></html:submit>
</c:when>
<c:otherwise>
<html:submit><bean:message key="button.update"/></html:submit>
</c:otherwise>
</c:choose>
</html:form>
</body>
</html>
ACTION CLASS (DISPATCH):
=======================
package com.validate.action;
import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;
import org.apache.commons.beanutils.BeanUtils;
import com.validate.databasebean.EmployeeDTO;
import com.validate.handler.EmployeeService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public final class EmployeeDispatchAction extends DispatchAction {
public ActionForward insert(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
System.out.println("DISPATCHER INSERT ");
EmployeeService service = new EmployeeService();
DynaActionForm employeeForm = (DynaActionForm)form;
EmployeeDTO employeeDTO = new EmployeeDTO();
BeanUtils.copyProperties( employeeDTO, employeeForm );
service.insertEmployee( employeeDTO );
ActionMessages messages = new ActionMessages();
ActionMessage message = new ActionMessage("message.employee.insert.success");
messages.add( ActionMessages.GLOBAL_MESSAGE, message );
saveMessages( request, messages );
employeeForm.set("methodToCall","update");
return (mapping.findForward("insertSuccess"));
}
public ActionForward update(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
System.out.println("DISPATCHER Update");
EmployeeService service = new EmployeeService();
DynaActionForm employeeForm = (DynaActionForm)form;
EmployeeDTO employeeDTO = new EmployeeDTO();
BeanUtils.copyProperties( employeeDTO, employeeForm );
service.updateEmployee( employeeDTO );
ActionMessages messages = new ActionMessages();
ActionMessage message = new ActionMessage("message.employee.update.success");
messages.add( ActionMessages.GLOBAL_MESSAGE, message );
saveMessages( request, messages );
return (mapping.findForward("updateSuccess"));
}
}
=======================================================================
package com.validate.databasebean;
public class EmployeeDTO {
private String name;
private int age;
private int department;
private String[] flavorIDs;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setDepartment(int department) {
this.department = department;
}
public void setFlavorIDs(String[] flavorIDs) {
this.flavorIDs = flavorIDs;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getDepartment() {
return department;
}
public String[] getFlavorIDs() {
return flavorIDs;
}
}
======================================================
package com.validate.handler;
import java.util.ArrayList;
import java.util.Collection;
import com.validate.databasebean.DepartmentBean;
import com.validate.databasebean.EmployeeDTO;
import com.validate.databasebean.FlavorBean;
public class EmployeeService {
public EmployeeDTO insertEmployee( EmployeeDTO employee ) {
//in real life call other business classes to do insert
//ie:
//EmployeeDAO.insertEmployee( employee );
System.out.println("INSERT");
return employee;
}
public EmployeeDTO updateEmployee( EmployeeDTO employee ) {
//in real life call other business classes to do insert
//ie:
//EmployeeDAO.insertEmployee( employee );
System.out.println("UPDATE");
return employee;
}
}
====================================================
No comments:
Post a Comment