⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 day05_2.txt

📁 网络购物系统开发详细步骤
💻 TXT
📖 第 1 页 / 共 2 页
字号:
       </tr>
         <input type="hidden" name="prductId" 
                 value='<%=p.getProductId()%>'>
</form>
<%
		}


	}
%>
</table>
<br>
<a href = "addProduct.do" >add a product</a>

</body>
</html>


==========================================================

15 为addProduct.do 创建Action

   file --> new --> web --> action

   package  : ec_port_web
   Action   : AddProductAction
   --> next

   action path   : addProduct
   formbean name :
   scope         :
   validate      :
   input jsp     :

==========================================================

16,修改 perform() 方法

package ec_port_web;

import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.util.Collection;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import exceptions.ServiceLocatorException;
import dto.CategoryDTO;
import delegates.ProductMgmDelegate;

public class AddProductAction extends Action {
  public ActionForward perform(
        ActionMapping actionMapping, 
        ActionForm actionForm, 
        HttpServletRequest request, 
        HttpServletResponse httpServletResponse) {
    
    try{
      
      ProductMgmDelegate pd = new ProductMgmDelegate();
      Collection col = pd.getAllCategory();
      request.getSession().
            setAttribute( "ALL_CATEGORY" , col );
      return actionMapping.findForward( "success" );
    }catch( ServiceLocatorException se ){
      se.printStackTrace();
      return actionMapping.findForward( "systemError" );
    }
     catch( FinderException fe ){
       fe.printStackTrace();
       return actionMapping.findForward( "systemError" );
    }
     catch( CreateException ce ){
       ce.printStackTrace();
       return actionMapping.findForward( "systemError" );
     }
  }
}

==========================================================

17, 为AddProductAction 创建forward

     a) when get all category success
        path : /addProduct.jsp
        name : success

     b) when some exception be caught
        path : /
        name : systemError

=========================================================

18, 创建addProduct.jsp

<%@ page contentType="text/html; charset=GBK" %>
<%@page import = "java.util.*,dto.CategoryDTO"%>
<html>
<head>
<title>
addProduct
</title>
</head>
<%
    Collection col = 
      (Collection)session.getAttribute( "ALL_CATEGORY" );
%>

<body bgcolor="#ffffff">

<form action="addPrductSubmit.do" method="post">
<%
	Iterator i = col.iterator();
%>
<p align=center>
   <table width="600" border="0" cellspacing="1" 
          cellpadding="4" bgcolor="3399ff">
      <tr>
          <td colspan="2" align="center">
             <font color="#ffffff" class="title">
             添加产品</font>
          </td>
      </tr>
      <tr>
      <td bgcolor="#ececec" >产品类别</td>
      <td bgcolor="#ececec" >
         <select name="category">
<%
	  while( i.hasNext() ){
		CategoryDTO category=                            (CategoryDTO)i.next();
%>
		<option value='<%=category.getId()%>'                         selected>
		    <%=category.getName()%>
		</option>
<%
	  }
%>
        </select>
       </td>
     </tr>
     <tr>
        <td bgcolor="#ececec" >产品名称</td>
        <td bgcolor="#ececec" >
            <input type="text" name="name"></td>
     </tr>
     <tr>
        <td bgcolor="#ececec" >产品单价</td>
        <td bgcolor="#ececec" >
              <input type="text" name="basePrice"></td>
     </tr>
     <tr>
        <td bgcolor="#ececec" >详细信息</td>
        <td bgcolor="#ececec" >
             <textarea name="desc"></textarea></td>
     </tr>
     <tr>
       <td colspan="2" bgcolor="#ececec" align="center">
           <input type="submit" value="添加"></td>
     </tr>
   </table>
</form>
</body>
</html>

==========================================================

19, 为addProduct.jsp 创建ActionForm

    file --> new --> web --> Action Form
    
    package    : ec_port_web
    ActionForm : AddProductSubmitForm

    ---> add from jsp --> addProduct.jsp

    ---> add 
    name: category
    type: java.lang.String
    ---> finish

==========================================================

package ec_port_web;

import org.apache.struts.action.*;
import javax.servlet.http.*;

public class AddPrductSubmitForm extends ActionForm {
  private String category;
  private String desc;
  private String basePrice;
  private String name;

  public String getCategory() {
    return category;
  }
  public void setCategory(String category) {
    this.category = category;
  }
  public String getDesc() {
    return desc;
  }
  public void setDesc(String desc) {
    this.desc = desc;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getbasePrice() {
   return basePrice;
 }
 public void setBasePrice(String basePrice) {
   this.basePrice = basePrice;
 }

  public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
    /**@todo: finish this method, this is just the skeleton.*/
    return null;
  }
  public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
  }
}
==========================================================

20, 为addProduct.jsp创建Action

     file --> new --> web --> action

     package : ec_port_web
     Action  : AddProductSubmitAction
     --> next

     action path   : /addPrductSubmit
     formbean name : addProductSubmitForm
     scope         : session
     validate      : true
     input jsp     : addProduct.jsp

     ---> finish

==========================================================

21, 修改perform方法

package ec_port_web;

import org.apache.struts.action.*;
import javax.servlet.http.*;
import delegates.ProductMgmDelegate;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import exceptions.*;
import dto.*;
import java.util.Collection;

public class AddPrductSubmitAction extends Action {
  public ActionForward perform(
        ActionMapping actionMapping, 
        ActionForm actionForm, 
        HttpServletRequest request, 
        HttpServletResponse httpServletResponse) {
    
    AddPrductSubmitForm form = 
            (AddPrductSubmitForm) actionForm;
    try{
      ProductMgmDelegate pd = new ProductMgmDelegate();
      CategoryDTO category = new CategoryDTO(
                      new Integer(form.getCategory()),
                      null,null);

      ProductDTO product = new ProductDTO(
               null,
               form.getName(),
               Double.parseDouble(form.getbasePrice()),
               form.getDesc(),category);

      pd.addProduct( product );
      Collection col = pd.getAllCategory();
      Collection pCol = pd.getAllProduct();

      request.getSession()
                 .setAttribute( "ALL_CATEGORY" , col );
      request.getSession()
                   .setAttribute( "ALL_PRODUCT" ,pCol);

      return actionMapping.findForward( "success" );
    }catch( ServiceLocatorException se ){
      se.printStackTrace();
      return actionMapping.findForward( "systemError" );
    }
     catch( CreateException ce ){
       ce.printStackTrace();
      return actionMapping.findForward( "systemError" );
    }
     catch( FinderException fe ){
       fe.printStackTrace();
      return actionMapping.findForward( "systemError" );
    }

  }
}

==========================================================

22,为AddProductSubmitAction 添加 forward

   a) when success
      path : /productMgm.jsp
      name : success

   b) when some exceptions be caught
      path : /fail.jsp
      name : systemError

==========================================================











































⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -