设置样式_添加商品

来源:互联网 发布:redis作为mysql的缓存 编辑:程序博客网 时间:2024/06/17 08:52

添加功能

main_manager.jsp中,

{添加}

<td>

<inputtype="button" value="添加" onClick="javascript:location.href='addProduct.jsp'">

</td>

 

ProductDao中,

booleanaddProduct(Product p)方法 // 添加商品

       // 注意:空字符串,主键也可以插进去

if (p.getProId() == null || p.getProId().equals("")) {

                     return flag;

       }

       // 数据库连接

       // 插入商品表SQL

       String sql = “insert into productvalues(?,?,?,?,?,?,?,?)”;

       // 操作对象

       // 设置参数

       pstmt.setString(1, p.getProId());

       ······

       pstmt.setFloat( , );

       // 执行插入操作,insert/delete/update

       int i = pstmt.executeUpdate(); // 返回所影响的行数

       // 判断是否插入成功

if (i > 0) {

       flag = true;

       syso;

       }

 

测试

 

页面:addProduct.jsp

添加商品

       引入页面文件

       <center>

              <h1>添加商品</h1>

              <form post>

              <table>

                     <tr>

                            <td>编&nbsp; &nbsp; &nbsp; &nbsp;号:<input type=”text” name=”proId”></td>

                     </tr>

                            ··· ···

                     <tr>

                            类型<select>

                     </tr>

                            销售量:

图片 type=”file”

                            描述 textarea rows=”8” cols=”20”

 

                     提交      重置(不是清空,是还原到页面的初始状况)   

              </table>

 

       </center>

 

样式控制

head前

具体为:

<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>

<%

String path =request.getContextPath();

String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPEHTMLPUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">

<html>

 <head>

   <basehref="<%=basePath%>">

   

   <title>添加商品</title>

   

    <metahttp-equiv="pragma"content="no-cache">

    <metahttp-equiv="cache-control"content="no-cache">

    <metahttp-equiv="expires"content="0">    

    <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

    <metahttp-equiv="description"content="This is my page">

    <!--

    <linkrel="stylesheet" type="text/css"href="styles.css">

    -->

    <styletype="text/css">

        input {

            width : 200px;

        }

    </style>

 </head>

 

 <bodybgcolor="Aliceblue">

   <center>

   <!-- 引入页面文件 -->

   <%@includefile="/head.jsp"%>

   <hr>

   <h1>添加商品</h1>

   <formaction="doAddProduct"method="post">

    <tablecellpadding="0"cellspacing="0">

        <tr>

            <td>&nbsp;&nbsp;号:<inputtype="text"name="proId"></td>

        </tr>

        <tr>

            <td>&nbsp;&nbsp;称:<inputtype="text"name="proName"></td>

        </tr>

        <tr>

            <td>&nbsp;&nbsp;型:<selectname="proType">

                        <option value="食物">食物</option>

                        <option value="时装">时装</option>

                        <option value="运动器材">运动器材</option>

                    </select>

                </td>

        </tr>

        <tr>

            <td>&nbsp;&nbsp;格:<inputtype="text"name="proPrice"></td>

        </tr>

        <tr>

            <td>&nbsp;&nbsp;存:<inputtype="text"name="proStore"/></td>

        </tr>

        <tr>

            <td>销售量:<inputtype="text"name="proSales"/></td>

        </tr>

        <tr>

            <td>&nbsp;&nbsp;片:<inputtype="file"name="proPicture"/></td>

        </tr>

        <tr>

            <td>&nbsp;&nbsp;述:<textarearows="8"cols="20" name="proDesc"></textarea></td>

        </tr>

        <tralign="center">

            <td>

                <input type="submit"value="添加"style="width:60px;"/>

                <input type="reset"value="重置"style="width:60px;"/>

            </td>

        </tr>

    </table>

   </form>

   </center>

 </body>

</html>

 

 

AddProductAction(Servlet)

// 获得页面信息

request.getParameter(“proId”);

// 构建实体Product

//

 

// 转换 价格 销售量 库存

float price =0;

if (null !=proPrice && !””.equals(proPrice)) {

       price = Float.parseFloat(proPrice);

}

p.setPrice();

// 商品数据库操作DAO

// 调用添加方法

 

if (flag) {

       // 跳转

       /doAllProduct

}else {

    // 绑定错误信息

       error.jsp

}

即:

publicvoiddoPost(HttpServletRequest request, HttpServletResponse response)

         throws ServletException,IOException {

      //获得页面信息

      StringproId = request.getParameter("proId");

      StringproName = request.getParameter("proName");

      StringproType = request.getParameter("proType");

      StringproPrice = request.getParameter("proPrice");

      StringproStore = request.getParameter("proStore");

      StringproSales =request.getParameter("proSales");

      StringproPicture = request.getParameter("proPicture");

      StringproDesc = request.getParameter("proDesc");

      //创建实体Product

      Productp = newProduct();

      p.setProId(proId);

      p.setProName(proName);

      p.setProType(proType);

      //转换价格销售量库存

      float price = 0;

      if (null != proPrice &&!"".equals(proPrice)){

         price= Float.parseFloat(proPrice);

      }

      p.setProPrice(price);

      int store = 0;

      if (null != proStore &&!"".equals(proStore)){

         store= Integer.parseInt(proStore);

      }

      p.setProStore(store);

      int sales = 0;

      if (null != proSales &&!"".equals(proSales)){

         sales= Integer.parseInt(proSales);

      }

      p.setProSales(sales);

      p.setProPicture(proPicture);

      p.setProDesc(proDesc);

      //商品数据库操作DAO

      ProductDaopd = newProductDao();

      boolean flag =pd.addProduct(p);

      if (flag) {

         // 跳转

         request.getRequestDispatcher("/doAllProduct").forward(request,response);

      }else {

          // 绑定错误信息

         request.setAttribute("errorMes","添加信息有误!");

         // 跳转到错误提示界面

         request.getRequestDispatcher("/error.jsp").forward(request,response);

      }

   }



0 0
原创粉丝点击