zoukankan      html  css  js  c++  java
  • SSH_框架整合3-删除

    一、普通删除

    1 完善src中 类:

    (1)EmployeeDao.java中: 

    1 //2 删除
    2     public void delete(Integer id){
    3         String hql="DELETE FROM Employee e WHERE e.id=?";
    4         getSession().createQuery(hql).setInteger(0,id).executeUpdate();
    5     }
    View Code

    (2)EmployeeService.java中:

    1 //2删除
    2     public void delete(Integer id){
    3         employeeDao.delete(id);
    4     }
    View Code

    (3)EmployeeAction.java中:

    1 //2 删除
    2     private Integer id;
    3     public void setId(Integer id) {
    4         this.id = id;
    5     }
    6     public String delete(){
    7         employeeService.delete(id);
    8         return SUCCESS;
    9     }
    View Code

    2 完善struts.xml

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">
     5 
     6 <struts>
     7 
     8     <constant name="struts.enable.DynamicMethodInvocation" value="false" />
     9     <constant name="struts.devMode" value="true" />
    10 
    11     <package name="default" namespace="/" extends="struts-default">
    12         <action name="emp-*" class="employeeAction"
    13                 method="{1}">
    14                 <result name="list">/WEB-INF/views/emp-list.jsp</result>
    15                  <result name="success" type="redirect">/emp-list</result>
    16         </action>
    17     </package>
    18 
    19 </struts>
    View Code

    3 完善emp-list.jsp

    1 <td>
    2                         <a href="emp-delete?id=${id }">Delete</a>
    3                     </td>
    View Code

    二、使用Ajax+jQuery方式实现删除

    1 emp-list.jsp中写出Ajax语句:

     1 <script type="text/javascript">
     2     //删除, 使用 ajax 的方式
     3     $(function(){
     4         $(".delete").click(function(){
     5             var lastName=$(this).next(":input").val();
     6             var flag=confirm("是否要删除"+lastName+"的信息吗?");
     7             //确认删除, 使用 ajax 的方式
     8             if(flag){
     9                 //获取要删除的行
    10                 var $tr=$(this).parent().parent();
    11                 var url=this.href;
    12                 var args={"time":new Date()};
    13                 $.post(url,args,function(data){
    14                     //若 data 的返回值为 1, 则提示 删除成功, 且把当前行删除
    15                     if(data==1){
    16                         alert("删除成功!");
    17                         $tr.remove();
    18                     }else{
    19                         //若 data 的返回值不是 1, 提示删除失败. 
    20                         alert("删除失败!");
    21                     }
    22                 });
    23             }
    24             return false;
    25         });
    26     })
    27 </script>
    View Code

      同时要想获取删除时提醒“删除XXX的信息”,要在下面写上一个隐藏域,点击要删除的人时可以获取此人的lastName:<input type="hidden" value="${lastName}"/>

     1 <s:iterator value="#request.employees">
     2                 <tr>
     3                     <td>${id}</td>
     4                     <td>${lastName}</td>
     5                     <td>${email }</td>
     6                     <td>${birth}</td>
     7                     <td>${createTime}</td>
     8                     <td>${department.departmentName}</td>
     9                     <td>
    10                         <a href="emp-delete?id=${id }" class="delete">Delete</a>
    11                         <input type="hidden" value="${lastName}"/>
    12                     </td>
    13                 </tr>
    14             </s:iterator>
    View Code

    2 重写EmployeeAction中的delete()方法:

     1 //2-1 使用Ajax方式删除
     2     private InputStream inputStream;
     3     
     4     public InputStream getInputStream() {
     5         return inputStream;
     6     }
     7     
     8     public String delete(){
     9         employeeService.delete(id);
    10         try {
    11             //删除成功
    12             inputStream=new ByteArrayInputStream("1".getBytes("UTF-8"));
    13         } catch (UnsupportedEncodingException e) {
    14             //删除失败
    15             try {
    16                 inputStream=new ByteArrayInputStream("0".getBytes("UTF-8"));
    17             } catch (UnsupportedEncodingException e1) {
    18                 // TODO Auto-generated catch block
    19                 e1.printStackTrace();
    20             }
    21             e.printStackTrace();
    22         }
    23         return "ajax-delete-success";
    24     }
    View Code

    3 Strusts.xml中去除以前的emp-list显示,重写显示删除后结果:

     1  <package name="default" namespace="/" extends="struts-default">
     2         <action name="emp-*" class="employeeAction"
     3                 method="{1}">
     4                 <result name="list">/WEB-INF/views/emp-list.jsp</result>
     5                 <!-- <result name="success" type="redirect">/emp-list</result> -->
     6                 <result type="stream" name="ajax-delete-success">
     7                 <param name="contentType">text/html</param>
     8                 <param name="inputName">inputStream</param>
     9             </result>    
    10         </action>
    11     </package>
    View Code

    上传下此时的文件code:

    1Struts.xml

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">
     5 
     6 <struts>
     7 
     8     <constant name="struts.enable.DynamicMethodInvocation" value="false" />
     9     <constant name="struts.devMode" value="true" />
    10 
    11     <package name="default" namespace="/" extends="struts-default">
    12         <action name="emp-*" class="employeeAction"
    13                 method="{1}">
    14                 <result name="list">/WEB-INF/views/emp-list.jsp</result>
    15                 <!-- <result name="success" type="redirect">/emp-list</result> -->
    16                 <result type="stream" name="ajax-delete-success">
    17                 <param name="contentType">text/html</param>
    18                 <param name="inputName">inputStream</param>
    19             </result>    
    20         </action>
    21     </package>
    22 
    23 </struts>
    View Code

    2 emp-list.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%@ taglib prefix="s" uri="/struts-tags" %>   
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     8 <title>Insert title here</title>
     9 <script type="text/javascript" SRC="scripts/jquery-1.7.2.js"></script>
    10 <script type="text/javascript">
    11     //删除, 使用 ajax 的方式
    12     $(function(){
    13         $(".delete").click(function(){
    14             var lastName=$(this).next(":input").val();
    15             var flag=confirm("是否要删除"+lastName+"的信息吗?");
    16             //确认删除, 使用 ajax 的方式
    17             if(flag){
    18                 //获取要删除的行
    19                 var $tr=$(this).parent().parent();
    20                 var url=this.href;
    21                 var args={"time":new Date()};
    22                 $.post(url,args,function(data){
    23                     //若 data 的返回值为 1, 则提示 删除成功, 且把当前行删除
    24                     if(data==1){
    25                         alert("删除成功!");
    26                         $tr.remove();
    27                     }else{
    28                         //若 data 的返回值不是 1, 提示删除失败. 
    29                         alert("删除失败!");
    30                     }
    31                 });
    32             }
    33             return false;
    34         });
    35     })
    36 </script>
    37 </head>
    38 <body>
    39     <h3>Employee List Page</h3>
    40     
    41     <s:if test="#request.employees == null ||#request.size()==0">
    42         没有员工信息
    43     </s:if>
    44     <s:else>
    45         <table border="1" cellpadding="10" cellspacing="0">
    46             <tr>
    47                 <td>ID</td>
    48                 <td>LASTNAME</td>
    49                 <td>EMAIL</td>
    50                 <td>BIRTH</td>
    51                 <td>CREATETIME</td>
    52                 <td>DEPT</td>
    53             </tr>
    54             <s:iterator value="#request.employees">
    55                 <tr>
    56                     <td>${id}</td>
    57                     <td>${lastName}</td>
    58                     <td>${email }</td>
    59                     <td>${birth}</td>
    60                     <td>${createTime}</td>
    61                     <td>${department.departmentName}</td>
    62                     <td>
    63                         <a href="emp-delete?id=${id }" class="delete">Delete</a>
    64                         <input type="hidden" value="${lastName}"/>
    65                     </td>
    66                 </tr>
    67             </s:iterator>
    68         </table>
    69     </s:else>
    70 </body>
    71 </html>
    View Code

    3 EmployeeAction:

     1 package com.atguigu.ssh.actions;
     2 
     3 import java.io.ByteArrayInputStream;
     4 import java.io.InputStream;
     5 import java.io.UnsupportedEncodingException;
     6 import java.util.Map;
     7 
     8 import org.apache.struts2.interceptor.RequestAware;
     9 
    10 import com.atguigu.ssh.service.EmployeeService;
    11 import com.opensymphony.xwork2.ActionSupport;
    12 
    13 public class EmployeeAction extends ActionSupport implements RequestAware{
    14     /**
    15      * 
    16      */
    17     private static final long serialVersionUID = 1L;
    18     
    19     private EmployeeService employeeService;
    20     
    21     public void setEmployeeService(EmployeeService employeeService){
    22         this.employeeService=employeeService;
    23     }
    24     
    25     //2 删除
    26     private Integer id;
    27     public void setId(Integer id) {
    28         this.id = id;
    29     }
    30     /*public String delete(){
    31         employeeService.delete(id);
    32         return SUCCESS;
    33     }*/
    34     //2-1 使用Ajax方式删除
    35     private InputStream inputStream;
    36     
    37     public InputStream getInputStream() {
    38         return inputStream;
    39     }
    40     
    41     public String delete(){
    42         employeeService.delete(id);
    43         try {
    44             //删除成功
    45             inputStream=new ByteArrayInputStream("1".getBytes("UTF-8"));
    46         } catch (UnsupportedEncodingException e) {
    47             //删除失败
    48             try {
    49                 inputStream=new ByteArrayInputStream("0".getBytes("UTF-8"));
    50             } catch (UnsupportedEncodingException e1) {
    51                 // TODO Auto-generated catch block
    52                 e1.printStackTrace();
    53             }
    54             e.printStackTrace();
    55         }
    56         return "ajax-delete-success";
    57     }
    58     //1 查询
    59     public String list(){
    60         request.put("employees", employeeService.getAll()); 
    61         return "list";
    62     }
    63 
    64     //放到页面里
    65     private Map<String,Object> request;
    66     @Override
    67     public void setRequest(Map<String, Object> arg0) {
    68         this.request=arg0;
    69     }
    70 
    71 }
    View Code

      

  • 相关阅读:
    SAP函数生成测试数据
    ABAP——编码规范
    展BOM清单——CS_BOM_EXPL_MAT_V2
    Java使用JCO实现调用SAP接口(建立采购单)
    ECN变更单建立——CCAP_ECN_CREATE
    SmartForms——插入复选框
    SmartForms——属性框被拖拽到左边不能复原
    SmartForms——实例
    SmartForms——基础知识
    SAP PP——生产订单的状态
  • 原文地址:https://www.cnblogs.com/noaman/p/5883638.html
Copyright © 2011-2022 走看看