SSH_框架整合3-删除

一、普通删除

1 完善src中 类:

(1)EmployeeDao.java中: 

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

(2)EmployeeService.java中:

 //2删除
public void delete(Integer id){
employeeDao.delete(id);
}

(3)EmployeeAction.java中:

 //2 删除
private Integer id;
public void setId(Integer id) {
this.id = id;
}
public String delete(){
employeeService.delete(id);
return SUCCESS;
}

2 完善struts.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default">
<action name="emp-*" class="employeeAction"
method="{1}">
<result name="list">/WEB-INF/views/emp-list.jsp</result>
<result name="success" type="redirect">/emp-list</result>
</action>
</package> </struts>

3 完善emp-list.jsp

 <td>
<a href="emp-delete?id=${id }">Delete</a>
</td>

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

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

 <script type="text/javascript">
//删除, 使用 ajax 的方式
$(function(){
$(".delete").click(function(){
var lastName=$(this).next(":input").val();
var flag=confirm("是否要删除"+lastName+"的信息吗?");
//确认删除, 使用 ajax 的方式
if(flag){
//获取要删除的行
var $tr=$(this).parent().parent();
var url=this.href;
var args={"time":new Date()};
$.post(url,args,function(data){
//若 data 的返回值为 1, 则提示 删除成功, 且把当前行删除
if(data==1){
alert("删除成功!");
$tr.remove();
}else{
//若 data 的返回值不是 1, 提示删除失败.
alert("删除失败!");
}
});
}
return false;
});
})
</script>

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

 <s:iterator value="#request.employees">
<tr>
<td>${id}</td>
<td>${lastName}</td>
<td>${email }</td>
<td>${birth}</td>
<td>${createTime}</td>
<td>${department.departmentName}</td>
<td>
<a href="emp-delete?id=${id }" class="delete">Delete</a>
<input type="hidden" value="${lastName}"/>
</td>
</tr>
</s:iterator>

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

 //2-1 使用Ajax方式删除
private InputStream inputStream; public InputStream getInputStream() {
return inputStream;
} public String delete(){
employeeService.delete(id);
try {
//删除成功
inputStream=new ByteArrayInputStream("1".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
//删除失败
try {
inputStream=new ByteArrayInputStream("0".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
return "ajax-delete-success";
}

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

  <package name="default" namespace="/" extends="struts-default">
<action name="emp-*" class="employeeAction"
method="{1}">
<result name="list">/WEB-INF/views/emp-list.jsp</result>
<!-- <result name="success" type="redirect">/emp-list</result> -->
<result type="stream" name="ajax-delete-success">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>
</package>

上传下此时的文件code:

1Struts.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default">
<action name="emp-*" class="employeeAction"
method="{1}">
<result name="list">/WEB-INF/views/emp-list.jsp</result>
<!-- <result name="success" type="redirect">/emp-list</result> -->
<result type="stream" name="ajax-delete-success">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>
</package> </struts>

2 emp-list.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" SRC="scripts/jquery-1.7.2.js"></script>
<script type="text/javascript">
//删除, 使用 ajax 的方式
$(function(){
$(".delete").click(function(){
var lastName=$(this).next(":input").val();
var flag=confirm("是否要删除"+lastName+"的信息吗?");
//确认删除, 使用 ajax 的方式
if(flag){
//获取要删除的行
var $tr=$(this).parent().parent();
var url=this.href;
var args={"time":new Date()};
$.post(url,args,function(data){
//若 data 的返回值为 1, 则提示 删除成功, 且把当前行删除
if(data==1){
alert("删除成功!");
$tr.remove();
}else{
//若 data 的返回值不是 1, 提示删除失败.
alert("删除失败!");
}
});
}
return false;
});
})
</script>
</head>
<body>
<h3>Employee List Page</h3> <s:if test="#request.employees == null ||#request.size()==0">
没有员工信息
</s:if>
<s:else>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<td>ID</td>
<td>LASTNAME</td>
<td>EMAIL</td>
<td>BIRTH</td>
<td>CREATETIME</td>
<td>DEPT</td>
</tr>
<s:iterator value="#request.employees">
<tr>
<td>${id}</td>
<td>${lastName}</td>
<td>${email }</td>
<td>${birth}</td>
<td>${createTime}</td>
<td>${department.departmentName}</td>
<td>
<a href="emp-delete?id=${id }" class="delete">Delete</a>
<input type="hidden" value="${lastName}"/>
</td>
</tr>
</s:iterator>
</table>
</s:else>
</body>
</html>

3 EmployeeAction:

 package com.atguigu.ssh.actions;

 import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map; import org.apache.struts2.interceptor.RequestAware; import com.atguigu.ssh.service.EmployeeService;
import com.opensymphony.xwork2.ActionSupport; public class EmployeeAction extends ActionSupport implements RequestAware{
/**
*
*/
private static final long serialVersionUID = 1L; private EmployeeService employeeService; public void setEmployeeService(EmployeeService employeeService){
this.employeeService=employeeService;
} //2 删除
private Integer id;
public void setId(Integer id) {
this.id = id;
}
/*public String delete(){
employeeService.delete(id);
return SUCCESS;
}*/
//2-1 使用Ajax方式删除
private InputStream inputStream; public InputStream getInputStream() {
return inputStream;
} public String delete(){
employeeService.delete(id);
try {
//删除成功
inputStream=new ByteArrayInputStream("1".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
//删除失败
try {
inputStream=new ByteArrayInputStream("0".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
return "ajax-delete-success";
}
//1 查询
public String list(){
request.put("employees", employeeService.getAll());
return "list";
} //放到页面里
private Map<String,Object> request;
@Override
public void setRequest(Map<String, Object> arg0) {
this.request=arg0;
} }

  

上一篇:SSH_框架整合7--整个项目CODE


下一篇:JavaWeb_day03_员工信息添加修改删除