java實(shí)現(xiàn)選中刪除功能的實(shí)例代碼
分析:
通過(guò)form表單傳遞數(shù)據(jù),刪除數(shù)據(jù)根據(jù)id編號(hào)刪除。
前臺(tái)
<a class="btn btn-primary" href="javascript:void(0);" rel="external nofollow" id="delSelected">刪除選中</a>
</div>
<form id="form" action="${pageContext.request.contextPath}/delSelectedServlet" method="post">
<table border="1" class="table table-bordered table-hover">
<tr class="success">
<th><input type="checkbox" id="firstCb"></th>
<th>編號(hào)</th>
<th>姓名</th>
<th>性別</th>
<th>年齡</th>
<th>籍貫</th>
<th>QQ</th>
<th>郵箱</th>
<th>操作</th>
</tr>
<c:forEach items="${users}" var="user" varStatus="s">
<tr>
<td><input type="checkbox" name="uid" value="${user.id}"></td>
<td>${s.count}</td>
<td>${user.name}</td>
<td>${user.gender}</td>
<td>${user.age}</td>
<td>${user.address}</td>
<td>${user.qq}</td>
<td>${user.email}</td>
<td><a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/findUserServlet?id=${user.id}" rel="external nofollow" >修改</a>
<a class="btn btn-default btn-sm" href="javascript:deleteUser(${user.id});" rel="external nofollow" >刪除</a></td>
</tr>
</c:forEach>
</table>
</form>
Js
<script>
function deleteUser(id) {
//用戶安全提示
if (confirm("您確定要?jiǎng)h除嗎?")){
//訪問(wèn)的路徑
location.href=" ${pageContext.request.contextPath}/delUserServlet?id=" rel="external nofollow" +id;
}
}
window.οnlοad=function () {
//給刪除選中按鈕添加單擊事件
document.getElementById("delSelected").οnclick=function () {
if (confirm("您確定要?jiǎng)h除選中條目嗎?"))
//表單提交
document.getElementById("form").submit();
}
//1.獲取第一個(gè)cb
document.getElementById("firstCb").οnclick=function () {
//獲取下表所有的cb
var cbs=document.getElementsByName("uid");
//遍歷
for (var i=0;i<cbs.length;i++){
//設(shè)置這些cbs[i]的checked狀態(tài)=firstCb.checked
cbs[i].checked =this.checked;
}
}
}
</script>
后臺(tái)
Servlet
@WebServlet("/delSelectedServlet")
public class DelSelectedServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取所有id
String[] ids=request.getParameterValues(“uid”);
//調(diào)用service刪除
UserService service=new UserServiceImpl();
service.delSelectedUser(ids);
//跳轉(zhuǎn)查詢Servlet
response.sendRedirect(request.getContextPath()+"/userListServlet");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
Service
public void delSelectedUser(String[] ids) {
//遍歷數(shù)組
for (String id:ids){
//調(diào)用dao刪除
dao.deleteUser(Integer.parseInt(id));
}
}
Dao
public void updateUser(User user) {
String sql=“update userms set name=?,age=?,gender=?,address=?,qq=?,email=? where id=?”;
template.update(sql,user.getName(),user.getAge(),user.getGender(),user.getAddress(),user.getQq(),user.getEmail(),user.getId());
}
總結(jié)
以上所述是小編給大家介紹的java實(shí)現(xiàn)選中刪除功能的實(shí)例代碼,希望對(duì)大家有所幫助!
相關(guān)文章
AsyncHttpClient?ChannelPool線程池頻道池源碼流程解析
這篇文章主要為大家介紹了AsyncHttpClient ChannelPool線程池頻道池源碼流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(26)
下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你2021-07-07
Springboot容器級(jí)后置處理器BeanDefinitionRegistryPostProcessor
這篇文章主要介紹了Springboot容器級(jí)后置處理器BeanDefinitionRegistryPostProcessor,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01
java 后臺(tái)開(kāi)發(fā)中model與entity(實(shí)體類(lèi))的區(qū)別說(shuō)明
這篇文章主要介紹了java 后臺(tái)開(kāi)發(fā)中model與entity(實(shí)體類(lèi))的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
基于Bigdecimal科學(xué)計(jì)數(shù)問(wèn)題
這篇文章主要介紹了基于Bigdecimal科學(xué)計(jì)數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
基于Java?NIO編寫(xiě)一個(gè)簡(jiǎn)單版Netty服務(wù)端
基于?NIO?實(shí)現(xiàn)的網(wǎng)絡(luò)框架,可以用少量的線程,處理大量的連接,更適用于高并發(fā)場(chǎng)景,所以被就將利用NIO編寫(xiě)一個(gè)簡(jiǎn)單版Netty服務(wù)端,需要的可以參考下2024-04-04
深入理解Java設(shè)計(jì)模式之狀態(tài)模式
這篇文章主要介紹了JAVA設(shè)計(jì)模式之職責(zé)鏈模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解2021-11-11
聊聊在獲取方法參數(shù)名方面,Spring真的就比Mybatis強(qiáng)?
在獲取方法參數(shù)名方面,Spring真的就比Mybatis強(qiáng)嗎?今天就帶大家聊聊這個(gè)話題,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java 客戶端向服務(wù)端上傳mp3文件數(shù)據(jù)的實(shí)例代碼
這篇文章主要介紹了Java 客戶端向服務(wù)端上傳mp3文件數(shù)據(jù)的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09

