javaEE10(javabean课后题2,4)

java 复制代码
<%@ page  pageEncoding="UTF-8" import="java.util.*,entity.*,dao.*"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>

<head>

<style>

  body{

      text-align:center;

  }

  th,td{

  border:2px solid gray;

  text-align:center;

  padding:3px 10px;

  }

  table{

  border-collapse:collapse;

  margin:10px auto;

  }



  #form1{

  padding:20px;

  display:none;

  position:fixed;

  top:50%;

  left:50%;

  width:300px;

  height:200px;

  z-index:1000;

  margin-left:-200px;

  margin-top:-200px;

   background: #88f;

  }

 </style>

</head>

<body>

<%

StudentDao studentDao=new StudentDao();

request.setCharacterEncoding("UTF-8");

String op=request.getParameter("op");

if(op!=null){

if("add".equals(op)){

    String sno=request.getParameter("sno");

    String sname=request.getParameter("sname");

    String sex=request.getParameter("sex");

    Student student=new Student(sno,sname,sex);

    try{

    studentDao.add(student);

    }catch(Exception e){

        e.printStackTrace();

    }

}

else if("update".equals(op)){

    String sno=request.getParameter("sno");

    String sname=request.getParameter("sname");

    String sex=request.getParameter("sex");

    Student student=new Student(sno,sname,sex);

    try{

        studentDao.update(student);

        }catch(Exception e){

            e.printStackTrace();

        }

}

else if("delete".equals(op)){

    String sno=request.getParameter("sno");

    try{

        studentDao.delete(sno);

        }catch(Exception e){

            e.printStackTrace();

        }

}

}

List<Student> studentList=studentDao.query();

pageContext.setAttribute("studentList",studentList);

 %>

  <h3><a href="javascript:add()">添加记录</a></h3>

 <h2>学生信息</h2>

   <table>

   <tr><th>学号</th><th>姓名</th><th>性别</th><th>修改</th><th>删除</th></tr>

  <c:forEach items="${studentList}" var="student" varStatus="st">

  <tr>

   <td>${student.sno}</td>

   <td>${student.sname}</td>

   <td>${student.sex}</td>

   <td><a href="javascript:update(${student.id},'${student.sno}','${student.sname}','${student.sex}')">修改</a></td>

   <td><a href="yemian.jsp?op=delete&id=${student.id}" onclick="return confirm('确实要删除吗?')">删除</a></td>   

  </tr>

 </c:forEach>

 </table>

 <form method="post" id="form1">

学号<input type="text" name="sno" id="sno"/><br><br>

 姓名<input type="text" name="sname" id="sname"/><br><br>

性别<input type="radio" name="sex" value="男" checked/>男<input type="radio" name="sex" value="女" />女<br><br>

 <input type="hidden" name="op" value="" id="op"/>

 <input type="hidden" name="id" id="id"/>

 <input type="submit" value="" id="bt"/>

 <input type="reset" value="重置"/>

  <input type="button" value="关闭" onclick="document.getElementById('form1').style.display='none';"/>

 </form>

 <br>

 <script>

 function add(){

    document.getElementById("op").value="add";

    document.getElementById("bt").value="添加";

    document.getElementById('form1').style.display='block';

 }

 function update(id,sno,sname,sex){

     document.getElementById("op").value="update";

     document.getElementById("bt").value="修改";

     document.getElementById('form1').style.display='block';

     

 }

 </script>

</body>

</html>

java 复制代码
<%@ page   import="dao.CandidateDao,entity.*,java.util.*"  pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>

<html>

<script>

function f(){

    var cks=document.getElementsByName("ck");

    len=cks.length;

    sum=0;

    for(i=0;i<len;i++){

        if(cks[i].checked)

            sum++;

    }

    if(sum==5){

        document.getElementById("form1").submit();

    }

    else{

        alert("请选择5个候选人");

    }

}

</script>

<body>

<%

CandidateDao candidateDao=new CandidateDao();

String[] ids=request.getParameterValues("ck");

int[] ids1=null;

if(ids!=null){

    ids1=new int[ids.length];

    for(int i=0;i<ids.length;i++){

        ids1[i]=Integer.parseInt(ids[i]);

    }

    candidateDao.vote(ids1);

}



%>

<%

List<Candidate> cList=candidateDao.query();

pageContext.setAttribute("cList",cList);

%>

<form method="post" id="form1">

<c:forEach items="${cList}" var="c">

<input type="checkbox" name="ck" value="${c.id}">

<img src="image/${c.photo}"><br>

${c.name}<br>

${c.intro}<br>

${c.num}<hr>

</c:forEach>

<input type="button" value="投票" onclick="f()"/>

</form>

</body>

</html>
相关推荐
皮皮林5513 小时前
SpringBoot 加载外部 Jar,实现功能按需扩展!
java·spring boot
rocksun3 小时前
认识Embabel:一个使用Java构建AI Agent的框架
java·人工智能
Java中文社群5 小时前
AI实战:一键生成数字人视频!
java·人工智能·后端
王中阳Go5 小时前
从超市收银到航空调度:贪心算法如何破解生活中的最优决策谜题?
java·后端·算法
shepherd1115 小时前
谈谈TransmittableThreadLocal实现原理和在日志收集记录系统上下文实战应用
java·后端·开源
维基框架5 小时前
Spring Boot 项目整合Spring Security 进行身份验证
java·架构
日月星辰Ace6 小时前
Java JVM 垃圾回收器(四):现代垃圾回收器 之 Shenandoah GC
java·jvm
天天摸鱼的java工程师7 小时前
商品详情页 QPS 达 10 万,如何设计缓存架构降低数据库压力?
java·后端·面试
天天摸鱼的java工程师7 小时前
设计一个分布式 ID 生成器,要求全局唯一、趋势递增、支持每秒 10 万次生成,如何实现?
java·后端·面试
阿杆7 小时前
一个看似普通的定时任务,如何优雅地毁掉整台服务器
java·后端·代码规范