我写的一个jsp,servlet, 和ajax(xml)的例子
Code
<%--
Document : searchSupplier
Created on : 7-Oct-2008, 8:25:59 PM
Author : Joey
--%>
<%@page session="true" contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="hairsalon.core.*" %>
<%@page import="hairsalon.objects.*" %>
<%@page import="hairsalon.presentation.*" %>
<%@page import="java.util.*" %>
<% String page_title = "Search supplier";%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<% UserSession userSession = (UserSession) session.getAttribute("user_session");%>
<% userSession.setCurrentPosition(SessionPositions.Suppliers);%>
<%@ include file="WEB-INF/jspf/header.jspf" %>
<a href="supplier?supplier_action=load">Create supplier</a>
<form method="Post" action="/HairSalon/supplier">
<table>
<tr>
<td>
<span>
Address One:
</span>
</td>
<td>
<input type="text" name="txtAddressOne" />
</td>
<td>
<span>
Address Two:
</span>
</td>
<td>
<input type="text" name="txtAddressTwo" />
</td>
</tr>
<tr>
<td>
<span>
City:
</span>
</td>
<td>
<input type="text" name="txtCity" />
</td>
<td>
<span>
Province:
</span>
</td>
<td>
<input type="text" name="txtProvince" />
</td>
</tr>
<tr>
<td>
<span>
Country:
</span>
</td>
<td>
<input type="text" name="txtCountry" />
</td>
<td>
<span>
Postal Code:
</span>
</td>
<td>
<input type="text" name="txtPostalCode" />
</td>
</tr>
<tr>
<td>
<span>
Description:
</span>
</td>
<td>
<input type="text" name="txtDescription" />
</td>
<td>
<span>
Phone Number:
</span>
</td>
<td>
<input type="text" name="txtPhoneNumber" />
</td>
</tr>
<tr>
<td>
<span>
Supplier Name:
</span>
</td>
<td>
<input type="text" name="txtSupplierName" />
</td>
<td>
<span>
Email:
</span>
</td>
<td>
<input type="text" name="txtEmail" />
</td>
</tr>
<tr>
<td>
<input type="submit" value="Search" />
</td>
</tr>
</table>
<input type="hidden" name="supplier_action" value="" />
</form>
<%
SupplierBean[] sbs = (SupplierBean[]) userSession.getAttribute("supplier_search_result");
if (sbs != null) {
if (sbs.length != 0) {
%>
<table>
<tr>
<td>
Supplier Name
</td>
<td>
Address Number
</td>
<td>
Description
</td>
<td>
Phone Number
</td>
</tr>
<%
for (SupplierBean sb : sbs) {
%>
<tr>
<td><a href='supplier?supplier_action=load&id=<%=sb.getSupplierNo()%>'><%=FormatController.display(sb.getSupplierName())%></a></td>
<td><%=FormatController.display(sb.getAddress().getAddressNo())%></td>
<td><%=FormatController.display(sb.getDescription())%></td>
<td><%=FormatController.display(sb.getPhoneNumber())%></td>
</tr>
<%
}
%>
</table>
<%
userSession.removeAttribute("supplier_search_result");
}
}
%>
<%@ include file="WEB-INF/jspf/footer.jspf" %>
<%--
Document : searchSupplier
Created on : 7-Oct-2008, 8:25:59 PM
Author : Joey
--%>
<%@page session="true" contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="hairsalon.core.*" %>
<%@page import="hairsalon.objects.*" %>
<%@page import="hairsalon.presentation.*" %>
<%@page import="java.util.*" %>
<% String page_title = "Search supplier";%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<% UserSession userSession = (UserSession) session.getAttribute("user_session");%>
<% userSession.setCurrentPosition(SessionPositions.Suppliers);%>
<%@ include file="WEB-INF/jspf/header.jspf" %>
<a href="supplier?supplier_action=load">Create supplier</a>
<form method="Post" action="/HairSalon/supplier">
<table>
<tr>
<td>
<span>
Address One:
</span>
</td>
<td>
<input type="text" name="txtAddressOne" />
</td>
<td>
<span>
Address Two:
</span>
</td>
<td>
<input type="text" name="txtAddressTwo" />
</td>
</tr>
<tr>
<td>
<span>
City:
</span>
</td>
<td>
<input type="text" name="txtCity" />
</td>
<td>
<span>
Province:
</span>
</td>
<td>
<input type="text" name="txtProvince" />
</td>
</tr>
<tr>
<td>
<span>
Country:
</span>
</td>
<td>
<input type="text" name="txtCountry" />
</td>
<td>
<span>
Postal Code:
</span>
</td>
<td>
<input type="text" name="txtPostalCode" />
</td>
</tr>
<tr>
<td>
<span>
Description:
</span>
</td>
<td>
<input type="text" name="txtDescription" />
</td>
<td>
<span>
Phone Number:
</span>
</td>
<td>
<input type="text" name="txtPhoneNumber" />
</td>
</tr>
<tr>
<td>
<span>
Supplier Name:
</span>
</td>
<td>
<input type="text" name="txtSupplierName" />
</td>
<td>
<span>
Email:
</span>
</td>
<td>
<input type="text" name="txtEmail" />
</td>
</tr>
<tr>
<td>
<input type="submit" value="Search" />
</td>
</tr>
</table>
<input type="hidden" name="supplier_action" value="" />
</form>
<%
SupplierBean[] sbs = (SupplierBean[]) userSession.getAttribute("supplier_search_result");
if (sbs != null) {
if (sbs.length != 0) {
%>
<table>
<tr>
<td>
Supplier Name
</td>
<td>
Address Number
</td>
<td>
Description
</td>
<td>
Phone Number
</td>
</tr>
<%
for (SupplierBean sb : sbs) {
%>
<tr>
<td><a href='supplier?supplier_action=load&id=<%=sb.getSupplierNo()%>'><%=FormatController.display(sb.getSupplierName())%></a></td>
<td><%=FormatController.display(sb.getAddress().getAddressNo())%></td>
<td><%=FormatController.display(sb.getDescription())%></td>
<td><%=FormatController.display(sb.getPhoneNumber())%></td>
</tr>
<%
}
%>
</table>
<%
userSession.removeAttribute("supplier_search_result");
}
}
%>
<%@ include file="WEB-INF/jspf/footer.jspf" %>
Code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hairsalon.presentation.servlets;
import hairsalon.presentation.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import hairsalon.core.*;
import hairsalon.objects.*;
import hairsalon.application.*;
import hairsalon.persistence.PersistenceController;
import java.util.ArrayList;
/**
*
* @author Joey
*/
public class SupplierServlet extends DispatcherServlet {
@Override
public void setupActionMethods()
throws NoSuchMethodException {
setActionAttribute("supplier_action");
setDefaultExternalAction("performSearch");
addExternalAction("load", "performLoad");
addExternalAction("commit", "performCommit");
}
public void performLoad(UserSession userSession, HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
SupplierBean bean = new SupplierBean();
try {
if (request.getParameter("id") != null && request.getParameter("id") != "") {
bean.setSupplierNo(Integer.parseInt(request.getParameter("id")));
bean = (SupplierBean) SessionController.loadSupplier(userSession, bean);
} else {
bean.setAddress(new AddressBean());
bean.setProducts(new ArrayList<ProductBean>());
}
userSession.setAttribute("supplier_load_result", bean);
forward("/maintain-supplier.jsp", request, response);
} catch (Exception e) {
response.sendRedirect(response.encodeRedirectURL("login-employee.jsp"));
}
}
public void performCommit(UserSession userSession, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SupplierBean bean = new SupplierBean();
String[] products = request.getParameterValues("products");
ArrayList<ProductBean> productBeans = new ArrayList<ProductBean>();
if (products != null) {
for (String product : products) {
try {
ProductBean pb = new ProductBean();
pb.setProductNo(Integer.parseInt(product));
pb = (ProductBean) SessionController.loadProduct(userSession, pb);
productBeans.add(pb);
} catch (Exception e) {
}
}
}
bean.setProducts(productBeans);
if (request.getParameter("txtSupplierNo").trim() != "") {
try {
bean.setSupplierNo(Integer.parseInt(request.getParameter("txtSupplierNo").trim()));
} catch (Exception omfg) {
}
}
AddressBean address = new AddressBean();
if (request.getParameter("txtAddressNo").trim() != "") {
address.setAddressNo(Integer.parseInt(request.getParameter("txtAddressNo").trim()));
}
if (request.getParameter("txtAddressOne").trim() != "") {
address.setAddress1(request.getParameter("txtAddressOne").trim());
}
if (request.getParameter("txtAddressTwo").trim() != "") {
address.setAddress2(request.getParameter("txtAddressTwo").trim());
}
if (request.getParameter("txtCity").trim() != "") {
address.setCity(request.getParameter("txtCity").trim());
}
if (request.getParameter("txtProvince").trim() != "") {
address.setProvince(request.getParameter("txtProvince").trim());
}
if (request.getParameter("txtCountry").trim() != "") {
address.setCountry(request.getParameter("txtCountry").trim());
}
if (request.getParameter("txtPostalCode").trim() != "") {
address.setPostalCode(request.getParameter("txtPostalCode").trim());
}
if (request.getParameter("txtEmail").trim() != "") {
address.setEmail(request.getParameter("txtEmail").trim());
}
bean.setAddress(address);
if (request.getParameter("txtSupplierName").trim() != "") {
bean.setSupplierName(request.getParameter("txtSupplierName").trim());
}
if (request.getParameter("txtDescription").trim() != "") {
bean.setDescription(request.getParameter("txtDescription").trim());
}
if (request.getParameter("txtPhoneNumber").trim() != "") {
bean.setPhoneNumber(request.getParameter("txtPhoneNumber").trim());
}
SessionController.saveSupplier(userSession, bean);
redirect("supplier?supplier_action=load&id=" + bean.getSupplierNo(), request, response);
}
public void performSearch(UserSession userSession, HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
SupplierBean bean = new SupplierBean();
AddressBean address = new AddressBean();
if (request.getParameter("txtAddressOne").trim() != "") {
address.setAddress1(request.getParameter("txtAddressOne").trim());
}
if (request.getParameter("txtAddressTwo").trim() != "") {
address.setAddress2(request.getParameter("txtAddressTwo").trim());
}
if (request.getParameter("txtCity").trim() != "") {
address.setCity(request.getParameter("txtCity").trim());
}
if (request.getParameter("txtProvince").trim() != "") {
address.setProvince(request.getParameter("txtProvince").trim());
}
if (request.getParameter("txtCountry").trim() != "") {
address.setCountry(request.getParameter("txtCountry").trim());
}
if (request.getParameter("txtPostalCode").trim() != "") {
address.setPostalCode(request.getParameter("txtPostalCode").trim());
}
if (request.getParameter("txtEmail").trim() != "") {
address.setEmail(request.getParameter("txtEmail").trim());
}
bean.setAddress(address);
if (request.getParameter("txtSupplierName").trim() != "") {
bean.setSupplierName(request.getParameter("txtSupplierName").trim());
}
if (request.getParameter("txtDescription").trim() != "") {
bean.setDescription(request.getParameter("txtDescription").trim());
}
if (request.getParameter("txtPhoneNumber").trim() != "") {
bean.setPhoneNumber(request.getParameter("txtPhoneNumber").trim());
}
SupplierBean[] dbs = SessionController.searchSuppliers(userSession, bean);
userSession.setAttribute("supplier_search_result", dbs);
response.sendRedirect(response.encodeRedirectURL("/HairSalon/search-suppliers.jsp"));
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hairsalon.presentation.servlets;
import hairsalon.presentation.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import hairsalon.core.*;
import hairsalon.objects.*;
import hairsalon.application.*;
import hairsalon.persistence.PersistenceController;
import java.util.ArrayList;
/**
*
* @author Joey
*/
public class SupplierServlet extends DispatcherServlet {
@Override
public void setupActionMethods()
throws NoSuchMethodException {
setActionAttribute("supplier_action");
setDefaultExternalAction("performSearch");
addExternalAction("load", "performLoad");
addExternalAction("commit", "performCommit");
}
public void performLoad(UserSession userSession, HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
SupplierBean bean = new SupplierBean();
try {
if (request.getParameter("id") != null && request.getParameter("id") != "") {
bean.setSupplierNo(Integer.parseInt(request.getParameter("id")));
bean = (SupplierBean) SessionController.loadSupplier(userSession, bean);
} else {
bean.setAddress(new AddressBean());
bean.setProducts(new ArrayList<ProductBean>());
}
userSession.setAttribute("supplier_load_result", bean);
forward("/maintain-supplier.jsp", request, response);
} catch (Exception e) {
response.sendRedirect(response.encodeRedirectURL("login-employee.jsp"));
}
}
public void performCommit(UserSession userSession, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SupplierBean bean = new SupplierBean();
String[] products = request.getParameterValues("products");
ArrayList<ProductBean> productBeans = new ArrayList<ProductBean>();
if (products != null) {
for (String product : products) {
try {
ProductBean pb = new ProductBean();
pb.setProductNo(Integer.parseInt(product));
pb = (ProductBean) SessionController.loadProduct(userSession, pb);
productBeans.add(pb);
} catch (Exception e) {
}
}
}
bean.setProducts(productBeans);
if (request.getParameter("txtSupplierNo").trim() != "") {
try {
bean.setSupplierNo(Integer.parseInt(request.getParameter("txtSupplierNo").trim()));
} catch (Exception omfg) {
}
}
AddressBean address = new AddressBean();
if (request.getParameter("txtAddressNo").trim() != "") {
address.setAddressNo(Integer.parseInt(request.getParameter("txtAddressNo").trim()));
}
if (request.getParameter("txtAddressOne").trim() != "") {
address.setAddress1(request.getParameter("txtAddressOne").trim());
}
if (request.getParameter("txtAddressTwo").trim() != "") {
address.setAddress2(request.getParameter("txtAddressTwo").trim());
}
if (request.getParameter("txtCity").trim() != "") {
address.setCity(request.getParameter("txtCity").trim());
}
if (request.getParameter("txtProvince").trim() != "") {
address.setProvince(request.getParameter("txtProvince").trim());
}
if (request.getParameter("txtCountry").trim() != "") {
address.setCountry(request.getParameter("txtCountry").trim());
}
if (request.getParameter("txtPostalCode").trim() != "") {
address.setPostalCode(request.getParameter("txtPostalCode").trim());
}
if (request.getParameter("txtEmail").trim() != "") {
address.setEmail(request.getParameter("txtEmail").trim());
}
bean.setAddress(address);
if (request.getParameter("txtSupplierName").trim() != "") {
bean.setSupplierName(request.getParameter("txtSupplierName").trim());
}
if (request.getParameter("txtDescription").trim() != "") {
bean.setDescription(request.getParameter("txtDescription").trim());
}
if (request.getParameter("txtPhoneNumber").trim() != "") {
bean.setPhoneNumber(request.getParameter("txtPhoneNumber").trim());
}
SessionController.saveSupplier(userSession, bean);
redirect("supplier?supplier_action=load&id=" + bean.getSupplierNo(), request, response);
}
public void performSearch(UserSession userSession, HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
SupplierBean bean = new SupplierBean();
AddressBean address = new AddressBean();
if (request.getParameter("txtAddressOne").trim() != "") {
address.setAddress1(request.getParameter("txtAddressOne").trim());
}
if (request.getParameter("txtAddressTwo").trim() != "") {
address.setAddress2(request.getParameter("txtAddressTwo").trim());
}
if (request.getParameter("txtCity").trim() != "") {
address.setCity(request.getParameter("txtCity").trim());
}
if (request.getParameter("txtProvince").trim() != "") {
address.setProvince(request.getParameter("txtProvince").trim());
}
if (request.getParameter("txtCountry").trim() != "") {
address.setCountry(request.getParameter("txtCountry").trim());
}
if (request.getParameter("txtPostalCode").trim() != "") {
address.setPostalCode(request.getParameter("txtPostalCode").trim());
}
if (request.getParameter("txtEmail").trim() != "") {
address.setEmail(request.getParameter("txtEmail").trim());
}
bean.setAddress(address);
if (request.getParameter("txtSupplierName").trim() != "") {
bean.setSupplierName(request.getParameter("txtSupplierName").trim());
}
if (request.getParameter("txtDescription").trim() != "") {
bean.setDescription(request.getParameter("txtDescription").trim());
}
if (request.getParameter("txtPhoneNumber").trim() != "") {
bean.setPhoneNumber(request.getParameter("txtPhoneNumber").trim());
}
SupplierBean[] dbs = SessionController.searchSuppliers(userSession, bean);
userSession.setAttribute("supplier_search_result", dbs);
response.sendRedirect(response.encodeRedirectURL("/HairSalon/search-suppliers.jsp"));
}
}
Code
<%--
Document : maintainSupplier
Created on : 7-Oct-2008, 8:26:37 PM
Author : Joey
--%>
<%@page session="true" contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="hairsalon.core.*" %>
<%@page import="hairsalon.objects.*" %>
<%@page import="hairsalon.presentation.*" %>
<%@page import="java.util.*" %>
<% String page_title = "Maintain supplier";%>
<% UserSession userSession = (UserSession) session.getAttribute("user_session");%>
<% userSession.setCurrentPosition(SessionPositions.Suppliers);%>
<%@ taglib prefix="taglib" uri="/WEB-INF/taglib.tld"%>
<% SupplierBean sb = (SupplierBean) userSession.moveAttribute("supplier_load_result");%>
<script>
function product(id,name){
this.name=name;
this.id=id;
}
var products = new Array();
<%
if (sb.getProducts() != null) {
for (ProductBean pb : sb.getProducts()) {
%>
products.push(new product(<%=FormatController.display(pb.getProductNo())%>,"<%=FormatController.display(pb.getName())%>"));
<% }
}
%>
function refillProductsList(){
if(products.length==0){
document.getElementById("productsList").style.display="none";
document.getElementById("productsList").innerHTML="";
}
else{
document.getElementById("productsList").style.display="block";
var content = "";
content+="<table>";
content+="<tr>";
content+="<td>";
content+="Product Name";
content+="</td>";
content+="<td>";
content+="Delete";
content+="</td>";
content+="</tr>";
for(var i=0; i < products.length;i++){
var product = products[i];
content+="<tr>";
content+="<td>";
content+=product.name;
content+="</td>";
content+="<td>";
content+="<input type=\"button\" id=\""+product.id+"\" onclick=\"deleteProduct("+product.id+")\" value=\"Delete\" />";
content+="</td>";
content+="</tr>";
}
content+="</table>";
document.getElementById("productsList").innerHTML = content;
}
}
function addProduct(id,name){
var index = findIndex(id);
if(index==-1){
products.push(new product(id, name));
refillProductsList();
}
}
function deleteProduct(id){
var index = findIndex(id);
if(index!=-1){
products.splice(index, 1);
refillProductsList();
}
}
function findIndex(id){
for(var i=0;i<products.length;i++){
var product = products[i];
if(product.id==id){
return i;
}
}
return -1;
}
function showAddProduct(){
document.getElementById("blackout").style.display="block";
document.getElementById("focus").style.display="block";
document.getElementById("searchResult").innerHTML="";
}
function closeAddProduct(){
document.getElementById("blackout").style.display="none";
document.getElementById("focus").style.display="none";
}
var request;
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP.5.0");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = false;
}
}
}
if(!request){
alert("Oh my, are you using safari?Come on, use a normal browser");//needs to change later:P
}
function builtResultTable(xml){
if(xml.getElementsByTagName("name").length==0){
document.getElementById("searchResult").innerHTML="";
return;
}
var content = "";
content += "<table>";
content += "<tr>";
content += "<td>";
content += "Name";
content += "</td>";
content += "<td>";
content += "Brand";
content += "</td>";
content += "<td>";
content += "Type";
content += "</td>";
content += "<td>";
content += "Add";
content += "</td>";
content += "</tr>";
for(var i=0;i<xml.getElementsByTagName("name").length;i++){
content += "<tr>";
content += "<td>";
content += xml.getElementsByTagName("name")[i].childNodes[0].nodeValue;
content += "</td>";
content += "<td>";
content += xml.getElementsByTagName("brand")[i].childNodes[0].nodeValue;
content += "</td>";
content += "<td>";
content += xml.getElementsByTagName("type")[i].childNodes[0].nodeValue;
content += "</td>";
content += "<td>";
content += "<input type=\"button\" onclick=\"addProduct('"+xml.getElementsByTagName("id")[i].childNodes[0].nodeValue+"','"+xml.getElementsByTagName("name")[i].childNodes[0].nodeValue+"')\" value=\"Add\" />";
content += "</td>";
content += "</tr>";
}
content += "</table>";
document.getElementById("searchResult").innerHTML=content;
}
function xmlProcess() {
if (request.readyState == 4){
if (request.status == 200){
try{
var xml = request.responseXML.documentElement;
builtResultTable(xml);
}
catch(e){
}
}
}
}
function textProcess(){
if (request.readyState == 4){
if (request.status == 200){
try{
var text=request.responseText;
}
catch(e){
alert("it must be randy's fault");
}
}
}
}
function executionProcess(){
if (request.readyState == 4){
if (request.status == 200){
try{
eval(request.responseText);
}
catch(e){
alert("it must be randy's fault");
}
}
}
}
function sendXMLRequest(queryString){
var url = escape("ajax-supplier.jsp") + "?random=" + Math.random();
request.open("Post", url, true);
request.onreadystatechange = xmlProcess;
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
request.send(queryString);
}
function sendTextRequest(queryString){
var url = escape("ajax-supplier.jsp") + "?random=" + Math.random();
request.open("Post", url, true);
request.onreadystatechange = textProcess;
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
request.send(queryString);
}
function sendExecutionRequest(queryString){
var url = escape("ajax-supplier.jsp") + "?random=" + Math.random();
request.open("Post", url, true);
request.onreadystatechange = executionProcess;
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
request.send(queryString);
}
function searchProducts(){
var queryString="";
queryString+="productName="+escape(document.getElementById("productName").value)+"&";
queryString+="brand="+escape(document.getElementById("brand").value)+"&";
queryString+="productType="+escape(document.getElementById("productType").value);
sendXMLRequest(queryString);
}
function doSubmit(){
for(var i=0;i<products.length;i++){
var product = products[i];
createProductInput(product.id);
}
}
function createProductInput(value){
var form = document.getElementById("postForm");
var input = document.createElement('input');
input.value = value;
input.type = 'hidden';
input.name = "products";
form.appendChild(input);
}
</script>
<%@ include file="WEB-INF/jspf/header.jspf" %>
<form id="postForm" onsubmit="doSubmit()" method="Post" action="supplier">
<input type="hidden" name="txtSupplierNo" value='<%=FormatController.display(sb.getSupplierNo())%>' />
<input type="hidden" name="txtAddressNo" value='<%=FormatController.display(sb.getAddress().getAddressNo())%>' />
<table>
<tr>
<td>
<span>
Address One:
</span>
</td>
<td>
<input type="text" name="txtAddressOne" value='<%=FormatController.display(sb.getAddress().getAddress1())%>' />
</td>
<td>
<span>
Address Two:
</span>
</td>
<td>
<input type="text" name="txtAddressTwo" value='<%=FormatController.display(sb.getAddress().getAddress2())%>' />
</td>
</tr>
<tr>
<td>
<span>
City:
</span>
</td>
<td>
<input type="text" name="txtCity" value='<%=FormatController.display(sb.getAddress().getCity())%>' />
</td>
<td>
<span>
Province:
</span>
</td>
<td>
<input type="text" name="txtProvince" value='<%=FormatController.display(sb.getAddress().getProvince())%>' />
</td>
</tr>
<tr>
<td>
<span>
Country:
</span>
</td>
<td>
<input type="text" name="txtCountry" value='<%=FormatController.display(sb.getAddress().getCountry())%>' />
</td>
<td>
<span>
Postal Code:
</span>
</td>
<td>
<input type="text" name="txtPostalCode" value='<%=FormatController.display(sb.getAddress().getPostalCode())%>' />
</td>
</tr>
<tr>
<td>
<span>
Description:
</span>
</td>
<td>
<input type="text" name="txtDescription" value='<%=FormatController.display(sb.getDescription())%>' />
</td>
<td>
<span>
Phone Number:
</span>
</td>
<td>
<input type="text" name="txtPhoneNumber" value='<%=FormatController.display(sb.getPhoneNumber())%>' />
</td>
</tr>
<tr>
<td>
<span>Supplier Name:</span>
</td>
<td>
<input type="text" name="txtSupplierName" value='<%=FormatController.display(sb.getSupplierName())%>' />
</td>
<td>
<span>
Email:
</span>
</td>
<td>
<input type="text" name="txtEmail" value='<%=FormatController.display(sb.getAddress().getEmail())%>' />
</td>
</tr>
<tr>
<td>
<input type="submit" value="Commit" />
</td>
</tr>
</table>
<input type="hidden" name="supplier_action" value="commit" />
<div id="productsList">
<table>
<%if (sb.getProducts() != null) {
if (sb.getProducts().size() != 0) {%>
<tr>
<td>
Product Name
</td>
<td>
Delete
</td>
</tr>
<%}
}
%>
<%
if (sb.getProducts() != null) {
for (ProductBean pb : sb.getProducts()) {
%>
<tr>
<td>
<%=FormatController.display(pb.getName())%>
</td>
<td>
<input type="button" id="<%=FormatController.display(pb.getProductNo())%>" onclick="deleteProduct('<%=FormatController.display(pb.getProductNo())%>')" value="Delete" />
</td>
</tr>
<%
}
}
%>
</table>
</div>
<div>
<input type="button" onclick="showAddProduct()" value="Add Product" />
</div>
<div id="blackout" style="display:none; position: absolute;left: 0px; top: 0px; width: 1280px; height: 800px; background-color: #666666;opacity: .7; filter: alpha(opacity=70); z-index: 1000;"></div>
<div id="focus" style="display:none; position: absolute;left: 250px; top: 100px; width: 800px; height: 400px; background-color: #ffffff; z-index: 2008;">
<input type="button" onclick="closeAddProduct()" value="Close" />
<table>
<tr>
<td>
Product Name
</td>
<td>
<input id="productName" name="productName" type="text" />
</td>
</tr>
<tr>
<td>
brand
</td>
<td>
<input id="brand" name="brand" type="text" />
</td>
</tr>
<tr>
<td>
Product Type
</td>
<td>
<input id="productType" name="productType" type="text" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="button" onclick="searchProducts()" value="Search" />
</td>
</tr>
</table>
<div id="searchResult">
</div>
</div>
</form>
<%@ include file="WEB-INF/jspf/footer.jspf" %>
<%--
Document : maintainSupplier
Created on : 7-Oct-2008, 8:26:37 PM
Author : Joey
--%>
<%@page session="true" contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="hairsalon.core.*" %>
<%@page import="hairsalon.objects.*" %>
<%@page import="hairsalon.presentation.*" %>
<%@page import="java.util.*" %>
<% String page_title = "Maintain supplier";%>
<% UserSession userSession = (UserSession) session.getAttribute("user_session");%>
<% userSession.setCurrentPosition(SessionPositions.Suppliers);%>
<%@ taglib prefix="taglib" uri="/WEB-INF/taglib.tld"%>
<% SupplierBean sb = (SupplierBean) userSession.moveAttribute("supplier_load_result");%>
<script>
function product(id,name){
this.name=name;
this.id=id;
}
var products = new Array();
<%
if (sb.getProducts() != null) {
for (ProductBean pb : sb.getProducts()) {
%>
products.push(new product(<%=FormatController.display(pb.getProductNo())%>,"<%=FormatController.display(pb.getName())%>"));
<% }
}
%>
function refillProductsList(){
if(products.length==0){
document.getElementById("productsList").style.display="none";
document.getElementById("productsList").innerHTML="";
}
else{
document.getElementById("productsList").style.display="block";
var content = "";
content+="<table>";
content+="<tr>";
content+="<td>";
content+="Product Name";
content+="</td>";
content+="<td>";
content+="Delete";
content+="</td>";
content+="</tr>";
for(var i=0; i < products.length;i++){
var product = products[i];
content+="<tr>";
content+="<td>";
content+=product.name;
content+="</td>";
content+="<td>";
content+="<input type=\"button\" id=\""+product.id+"\" onclick=\"deleteProduct("+product.id+")\" value=\"Delete\" />";
content+="</td>";
content+="</tr>";
}
content+="</table>";
document.getElementById("productsList").innerHTML = content;
}
}
function addProduct(id,name){
var index = findIndex(id);
if(index==-1){
products.push(new product(id, name));
refillProductsList();
}
}
function deleteProduct(id){
var index = findIndex(id);
if(index!=-1){
products.splice(index, 1);
refillProductsList();
}
}
function findIndex(id){
for(var i=0;i<products.length;i++){
var product = products[i];
if(product.id==id){
return i;
}
}
return -1;
}
function showAddProduct(){
document.getElementById("blackout").style.display="block";
document.getElementById("focus").style.display="block";
document.getElementById("searchResult").innerHTML="";
}
function closeAddProduct(){
document.getElementById("blackout").style.display="none";
document.getElementById("focus").style.display="none";
}
var request;
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP.5.0");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = false;
}
}
}
if(!request){
alert("Oh my, are you using safari?Come on, use a normal browser");//needs to change later:P
}
function builtResultTable(xml){
if(xml.getElementsByTagName("name").length==0){
document.getElementById("searchResult").innerHTML="";
return;
}
var content = "";
content += "<table>";
content += "<tr>";
content += "<td>";
content += "Name";
content += "</td>";
content += "<td>";
content += "Brand";
content += "</td>";
content += "<td>";
content += "Type";
content += "</td>";
content += "<td>";
content += "Add";
content += "</td>";
content += "</tr>";
for(var i=0;i<xml.getElementsByTagName("name").length;i++){
content += "<tr>";
content += "<td>";
content += xml.getElementsByTagName("name")[i].childNodes[0].nodeValue;
content += "</td>";
content += "<td>";
content += xml.getElementsByTagName("brand")[i].childNodes[0].nodeValue;
content += "</td>";
content += "<td>";
content += xml.getElementsByTagName("type")[i].childNodes[0].nodeValue;
content += "</td>";
content += "<td>";
content += "<input type=\"button\" onclick=\"addProduct('"+xml.getElementsByTagName("id")[i].childNodes[0].nodeValue+"','"+xml.getElementsByTagName("name")[i].childNodes[0].nodeValue+"')\" value=\"Add\" />";
content += "</td>";
content += "</tr>";
}
content += "</table>";
document.getElementById("searchResult").innerHTML=content;
}
function xmlProcess() {
if (request.readyState == 4){
if (request.status == 200){
try{
var xml = request.responseXML.documentElement;
builtResultTable(xml);
}
catch(e){
}
}
}
}
function textProcess(){
if (request.readyState == 4){
if (request.status == 200){
try{
var text=request.responseText;
}
catch(e){
alert("it must be randy's fault");
}
}
}
}
function executionProcess(){
if (request.readyState == 4){
if (request.status == 200){
try{
eval(request.responseText);
}
catch(e){
alert("it must be randy's fault");
}
}
}
}
function sendXMLRequest(queryString){
var url = escape("ajax-supplier.jsp") + "?random=" + Math.random();
request.open("Post", url, true);
request.onreadystatechange = xmlProcess;
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
request.send(queryString);
}
function sendTextRequest(queryString){
var url = escape("ajax-supplier.jsp") + "?random=" + Math.random();
request.open("Post", url, true);
request.onreadystatechange = textProcess;
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
request.send(queryString);
}
function sendExecutionRequest(queryString){
var url = escape("ajax-supplier.jsp") + "?random=" + Math.random();
request.open("Post", url, true);
request.onreadystatechange = executionProcess;
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
request.send(queryString);
}
function searchProducts(){
var queryString="";
queryString+="productName="+escape(document.getElementById("productName").value)+"&";
queryString+="brand="+escape(document.getElementById("brand").value)+"&";
queryString+="productType="+escape(document.getElementById("productType").value);
sendXMLRequest(queryString);
}
function doSubmit(){
for(var i=0;i<products.length;i++){
var product = products[i];
createProductInput(product.id);
}
}
function createProductInput(value){
var form = document.getElementById("postForm");
var input = document.createElement('input');
input.value = value;
input.type = 'hidden';
input.name = "products";
form.appendChild(input);
}
</script>
<%@ include file="WEB-INF/jspf/header.jspf" %>
<form id="postForm" onsubmit="doSubmit()" method="Post" action="supplier">
<input type="hidden" name="txtSupplierNo" value='<%=FormatController.display(sb.getSupplierNo())%>' />
<input type="hidden" name="txtAddressNo" value='<%=FormatController.display(sb.getAddress().getAddressNo())%>' />
<table>
<tr>
<td>
<span>
Address One:
</span>
</td>
<td>
<input type="text" name="txtAddressOne" value='<%=FormatController.display(sb.getAddress().getAddress1())%>' />
</td>
<td>
<span>
Address Two:
</span>
</td>
<td>
<input type="text" name="txtAddressTwo" value='<%=FormatController.display(sb.getAddress().getAddress2())%>' />
</td>
</tr>
<tr>
<td>
<span>
City:
</span>
</td>
<td>
<input type="text" name="txtCity" value='<%=FormatController.display(sb.getAddress().getCity())%>' />
</td>
<td>
<span>
Province:
</span>
</td>
<td>
<input type="text" name="txtProvince" value='<%=FormatController.display(sb.getAddress().getProvince())%>' />
</td>
</tr>
<tr>
<td>
<span>
Country:
</span>
</td>
<td>
<input type="text" name="txtCountry" value='<%=FormatController.display(sb.getAddress().getCountry())%>' />
</td>
<td>
<span>
Postal Code:
</span>
</td>
<td>
<input type="text" name="txtPostalCode" value='<%=FormatController.display(sb.getAddress().getPostalCode())%>' />
</td>
</tr>
<tr>
<td>
<span>
Description:
</span>
</td>
<td>
<input type="text" name="txtDescription" value='<%=FormatController.display(sb.getDescription())%>' />
</td>
<td>
<span>
Phone Number:
</span>
</td>
<td>
<input type="text" name="txtPhoneNumber" value='<%=FormatController.display(sb.getPhoneNumber())%>' />
</td>
</tr>
<tr>
<td>
<span>Supplier Name:</span>
</td>
<td>
<input type="text" name="txtSupplierName" value='<%=FormatController.display(sb.getSupplierName())%>' />
</td>
<td>
<span>
Email:
</span>
</td>
<td>
<input type="text" name="txtEmail" value='<%=FormatController.display(sb.getAddress().getEmail())%>' />
</td>
</tr>
<tr>
<td>
<input type="submit" value="Commit" />
</td>
</tr>
</table>
<input type="hidden" name="supplier_action" value="commit" />
<div id="productsList">
<table>
<%if (sb.getProducts() != null) {
if (sb.getProducts().size() != 0) {%>
<tr>
<td>
Product Name
</td>
<td>
Delete
</td>
</tr>
<%}
}
%>
<%
if (sb.getProducts() != null) {
for (ProductBean pb : sb.getProducts()) {
%>
<tr>
<td>
<%=FormatController.display(pb.getName())%>
</td>
<td>
<input type="button" id="<%=FormatController.display(pb.getProductNo())%>" onclick="deleteProduct('<%=FormatController.display(pb.getProductNo())%>')" value="Delete" />
</td>
</tr>
<%
}
}
%>
</table>
</div>
<div>
<input type="button" onclick="showAddProduct()" value="Add Product" />
</div>
<div id="blackout" style="display:none; position: absolute;left: 0px; top: 0px; width: 1280px; height: 800px; background-color: #666666;opacity: .7; filter: alpha(opacity=70); z-index: 1000;"></div>
<div id="focus" style="display:none; position: absolute;left: 250px; top: 100px; width: 800px; height: 400px; background-color: #ffffff; z-index: 2008;">
<input type="button" onclick="closeAddProduct()" value="Close" />
<table>
<tr>
<td>
Product Name
</td>
<td>
<input id="productName" name="productName" type="text" />
</td>
</tr>
<tr>
<td>
brand
</td>
<td>
<input id="brand" name="brand" type="text" />
</td>
</tr>
<tr>
<td>
Product Type
</td>
<td>
<input id="productType" name="productType" type="text" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="button" onclick="searchProducts()" value="Search" />
</td>
</tr>
</table>
<div id="searchResult">
</div>
</div>
</form>
<%@ include file="WEB-INF/jspf/footer.jspf" %>
Code
<%--
Document : ajax-supplier
Created on : 14-Oct-2008, 8:08:36 PM
Author : Joey
--%>
<%@page session="true"%>
<%@page import="java.io.*,java.text.*,hairsalon.objects.*,hairsalon.presentation.*,hairsalon.application.*"%>
<%@page import="hairsalon.core.*" %>
<% UserSession userSession = (UserSession) session.getAttribute("user_session");%>
<% ProductBean pb = new ProductBean();%>
<% ProductBean[] productBeans;%>
<%
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
if (request.getParameter("productName") != "" && request.getParameter("productName") != null) {
pb.setName(request.getParameter("productName"));
}
if (request.getParameter("brand") != "" && request.getParameter("brand") != null) {
pb.setBrand(request.getParameter("brand"));
}
if (request.getParameter("productType") != "" && request.getParameter("productType") != null) {
pb.setType(request.getParameter("productType"));
}
productBeans = SessionController.searchProducts(userSession, pb);
PrintWriter pw = response.getWriter();
pw.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
pw.append("<products>");
for (ProductBean bean : productBeans) {
pw.append("<id>");
pw.append(FormatController.display(bean.getProductNo()));
pw.append("</id>");
pw.append("<name>");
pw.append(FormatController.display(bean.getName()));
pw.append("</name>");
pw.append("<brand>");
pw.append(FormatController.display(bean.getBrand()));
pw.append("</brand>");
pw.append("<type>");
pw.append(FormatController.display(bean.getType()));
pw.append("</type>");
}
pw.append("</products>");
pw.close();
%>
<%--
Document : ajax-supplier
Created on : 14-Oct-2008, 8:08:36 PM
Author : Joey
--%>
<%@page session="true"%>
<%@page import="java.io.*,java.text.*,hairsalon.objects.*,hairsalon.presentation.*,hairsalon.application.*"%>
<%@page import="hairsalon.core.*" %>
<% UserSession userSession = (UserSession) session.getAttribute("user_session");%>
<% ProductBean pb = new ProductBean();%>
<% ProductBean[] productBeans;%>
<%
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
if (request.getParameter("productName") != "" && request.getParameter("productName") != null) {
pb.setName(request.getParameter("productName"));
}
if (request.getParameter("brand") != "" && request.getParameter("brand") != null) {
pb.setBrand(request.getParameter("brand"));
}
if (request.getParameter("productType") != "" && request.getParameter("productType") != null) {
pb.setType(request.getParameter("productType"));
}
productBeans = SessionController.searchProducts(userSession, pb);
PrintWriter pw = response.getWriter();
pw.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
pw.append("<products>");
for (ProductBean bean : productBeans) {
pw.append("<id>");
pw.append(FormatController.display(bean.getProductNo()));
pw.append("</id>");
pw.append("<name>");
pw.append(FormatController.display(bean.getName()));
pw.append("</name>");
pw.append("<brand>");
pw.append(FormatController.display(bean.getBrand()));
pw.append("</brand>");
pw.append("<type>");
pw.append(FormatController.display(bean.getType()));
pw.append("</type>");
}
pw.append("</products>");
pw.close();
%>